Skip to content

Create initial "Solidity Tips" page #4

@BlockchainBud

Description

@BlockchainBud

@rxdizzle shared a few Solidity tips from working with gas optimization. Maybe we should use them as the starting point for section on Solidity tips.

https://dappsociety.slack.com/archives/C9CC2G2QP/p1519171164000278

  • Use constant vars where you can, the compiler can optimise them out for deployment-time savings and a 200 gas saving each time they're read
  • Use external function modifiers over public where you can, it's almost a net saving of half as Solidity doesn't need to write arguments to memory. https://ethereum.stackexchange.com/a/19391
  • Don't write needless Getter functions for anything you declare public, the compiler will do this for you, you'll just incur extra deployment time gas
  • Where possible try and optimise your storage patterns to use mappings, anything which uses an unbounded array runs the risk of completely gassing out. Dharma have a great example of removing a token from a user which you'd think would use an array but instead smartly uses mappings https://github.com/dharmaprotocol/NonFungibleToken/blob/master/contracts/NonFungibleToken.sol#L235
  • Make sure you tightly pack your struct values so they can fit into a single storage slot see http://solidity.readthedocs.io/en/develop/miscellaneous.html

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions