Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best Practices and Anti-Patterns Updates #1471

Merged
merged 7 commits into from
Mar 22, 2022
Merged

Conversation

joshuahannan
Copy link
Member

@joshuahannan joshuahannan commented Mar 2, 2022

Closes onflow/flow#708

IMPORTANT: If there are any best practices or anti-patterns that you think are missing from here and should be included, please let me know and I will add them. I added all the ones I could think of, but I'm sure there are more.

Description

  • Reorganizes some of the best practices and anti-patterns
  • Adds new ones
  • Removes some that I disagree with. 😉
  • Fixes some links

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

docs/anti-patterns.mdx Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Mar 2, 2022

Codecov Report

Merging #1471 (edd743b) into master (5238cce) will not change coverage.
The diff coverage is n/a.

❗ Current head edd743b differs from pull request most recent head 685616c. Consider uploading reports for the commit 685616c to get more accurate results

@@           Coverage Diff           @@
##           master    #1471   +/-   ##
=======================================
  Coverage   74.68%   74.68%           
=======================================
  Files         289      289           
  Lines       55607    55607           
=======================================
  Hits        41528    41528           
  Misses      12584    12584           
  Partials     1495     1495           
Flag Coverage Δ
unittests 74.68% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5238cce...685616c. Read the comment docs.

@github-actions
Copy link

github-actions bot commented Mar 2, 2022

Cadence Benchstat comparison

This branch with compared with the base branch onflow:master commit 5238cce
The command for i in {1..N}; do go test ./... -run=XXX -bench=. -shuffle=on; done was used.
Bench tests were run a total of 7 times on each branch.

Results

old.txtnew.txt
time/opdelta
RuntimeResourceDictionaryValues-217.0ms ± 4%16.9ms ± 0%~(p=0.755 n=7+5)
RuntimeFungibleTokenTransfer-21.49ms ±22%1.54ms ±25%~(p=1.000 n=7+7)
ParseArray-218.1ms ± 8%17.6ms ± 4%~(p=0.209 n=7+7)
ParseInfix-210.1µs ± 2%10.1µs ± 3%~(p=0.971 n=7+7)
ParseDeploy/byte_array-230.1ms ± 7%28.8ms ± 7%~(p=0.165 n=7+7)
ParseDeploy/decode_hex-21.44ms ± 2%1.43ms ± 2%~(p=0.456 n=7+7)
ParseFungibleToken-2228µs ± 6%222µs ± 3%~(p=0.128 n=7+7)
QualifiedIdentifierCreation/Three_levels-2174ns ± 2%171ns ± 2%~(p=0.179 n=6+7)
CheckContractInterfaceFungibleTokenConformance-2162µs ± 2%160µs ± 0%~(p=0.052 n=6+5)
NewInterpreter/new_sub-interpreter-22.75µs ± 5%2.75µs ± 2%~(p=0.832 n=7+7)
NewInterpreter/new_interpreter-21.40µs ± 1%1.38µs ± 2%−1.45%(p=0.024 n=7+6)
InterpretRecursionFib-23.34ms ± 3%3.28ms ± 2%−1.65%(p=0.017 n=7+7)
QualifiedIdentifierCreation/One_level-22.86ns ± 5%2.79ns ± 0%−2.22%(p=0.003 n=7+5)
ContractInterfaceFungibleToken-249.7µs ± 3%48.3µs ± 1%−2.76%(p=0.001 n=7+6)
 
alloc/opdelta
RuntimeResourceDictionaryValues-24.05MB ± 0%4.05MB ± 0%~(p=0.435 n=7+7)
RuntimeFungibleTokenTransfer-2273kB ± 0%273kB ± 0%~(p=0.777 n=7+7)
QualifiedIdentifierCreation/One_level-20.00B 0.00B ~(all equal)
QualifiedIdentifierCreation/Three_levels-264.0B ± 0%64.0B ± 0%~(all equal)
CheckContractInterfaceFungibleTokenConformance-266.2kB ± 0%66.2kB ± 0%~(all equal)
ContractInterfaceFungibleToken-226.6kB ± 0%26.6kB ± 0%~(p=0.192 n=7+7)
NewInterpreter/new_interpreter-2848B ± 0%848B ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-21.34kB ± 0%1.34kB ± 0%~(all equal)
InterpretRecursionFib-21.26MB ± 0%1.26MB ± 0%~(p=0.385 n=7+6)
 
allocs/opdelta
RuntimeResourceDictionaryValues-2102k ± 0%102k ± 0%~(p=0.344 n=7+6)
RuntimeFungibleTokenTransfer-24.54k ± 0%4.54k ± 0%~(p=1.000 n=7+7)
QualifiedIdentifierCreation/One_level-20.00 0.00 ~(all equal)
QualifiedIdentifierCreation/Three_levels-22.00 ± 0%2.00 ± 0%~(all equal)
CheckContractInterfaceFungibleTokenConformance-21.07k ± 0%1.07k ± 0%~(all equal)
ContractInterfaceFungibleToken-2458 ± 0%458 ± 0%~(all equal)
NewInterpreter/new_interpreter-213.0 ± 0%13.0 ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-240.0 ± 0%40.0 ± 0%~(all equal)
InterpretRecursionFib-226.2k ± 0%26.2k ± 0%~(all equal)
 

@turbolent
Copy link
Member

Awesome work! 👏 I'll have a read :-)

If there are any best practices or anti-patterns that you think are missing from here and should be included, please let me know and I will add them.

I'd have a performance one: Prefer in-place mutations.

For example, if the task is to update some data structure, e.g. a dictionary in storage or a contract, don't copy the whole data onto the "stack", modify it, then overwrite the existing data with the updated copy.

Instead, use e.g a reference to mutate the data in-place.

Here is a real-world example of this: https://github.com/dapperlabs/nba-smart-contracts/pull/125/files#diff-1493a8d9c5e6feabf2be9290b17f904a9e89e871cfcb0c21fce75dfb524eb406L105-R108

@joshuahannan
Copy link
Member Author

joshuahannan commented Mar 8, 2022

@turbolent Thanks for the feedback!

I'd have a performance one: Prefer in-place mutations.

Yep, that one is in there!
https://github.com/onflow/cadence/blob/best-practices/docs/design-patterns.mdx#avoid-excessive-load-and-save-storage-operations-prefer-in-place-mutations

Copy link
Contributor

@satyamakgec satyamakgec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Good work @joshuahannan

docs/anti-patterns.mdx Show resolved Hide resolved

## Auth References should be avoided

### Problem
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pseudo code here as well, If possible

@kimcodeashian
Copy link
Contributor

kimcodeashian commented Mar 11, 2022

FYI - I'll be reviewing through the lens as the intended audience, and suggestions will be based on improving clarity & understanding for the reader. 😄

ALSO - love the new organization/presentation of content. Much cleaner than before.

I'll be continuing to review this next week 👍

@joshuahannan
Copy link
Member Author

Hey @turbolent, looks like I need an approval from a code owner to merge this. Can you approve it please?

Thanks!

docs/anti-patterns.mdx Outdated Show resolved Hide resolved
docs/anti-patterns.mdx Outdated Show resolved Hide resolved
docs/anti-patterns.mdx Outdated Show resolved Hide resolved
docs/anti-patterns.mdx Outdated Show resolved Hide resolved
docs/anti-patterns.mdx Outdated Show resolved Hide resolved
docs/anti-patterns.mdx Outdated Show resolved Hide resolved
docs/design-patterns.mdx Outdated Show resolved Hide resolved
docs/design-patterns.mdx Outdated Show resolved Hide resolved
docs/msg-sender.mdx Outdated Show resolved Hide resolved
docs/msg-sender.mdx Outdated Show resolved Hide resolved
Copy link
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

docs/anti-patterns.mdx Outdated Show resolved Hide resolved
docs/anti-patterns.mdx Outdated Show resolved Hide resolved
docs/anti-patterns.mdx Show resolved Hide resolved
docs/anti-patterns.mdx Outdated Show resolved Hide resolved
docs/anti-patterns.mdx Outdated Show resolved Hide resolved
docs/design-patterns.mdx Show resolved Hide resolved
docs/design-patterns.mdx Show resolved Hide resolved
docs/design-patterns.mdx Outdated Show resolved Hide resolved
docs/design-patterns.mdx Show resolved Hide resolved
docs/msg-sender.mdx Outdated Show resolved Hide resolved
@joshuahannan
Copy link
Member Author

Thank you so much for the reviews @turbolent and @psiemens ! I've addressed all your comments and pushed my latest changes.

Copy link
Contributor

@psiemens psiemens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Great work!

Copy link
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! 👏

@joshuahannan joshuahannan merged commit 668062c into master Mar 22, 2022
@joshuahannan joshuahannan deleted the best-practices branch March 22, 2022 18:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dev Portal Documentation Documentation Improvements or additions to documentation Improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cadence Docs: Update Best Practices
6 participants