Skip to content

Solidity Best Practices

andrewgordstewart edited this page May 7, 2018 · 13 revisions

Solidity Best Practices

There's an underhanded solidity coding contest running 24/7, it's called 'Ethereum'

Overflows/Underflows

You should have come across the SafeMath library in the Cryptozombies course (Lesson 5, Chapter 9). Integer overflows are real! See:

If you use for (var i = 0; i < a.length; i ++) { a[i] = i; }, then the type of i will be inferred only from 0, whose type is uint8. This means that if a has more than 255 elements, your loop will not terminate because i can only hold values up to 255.

Better use for (uint i = 0; i < a.length...

Protect against integer overflows by always using SafeMath, and avoiding type inference with the var keyword.

See also: Overflows & Underflows and SafeMath to protect from overflows

External function calls

Special care must always be taken when calling functions external to a contract. TODO: Understand what's meant by this:

the recipient can call this function again as part of the receiving call before send returns

See: simple open auction

Integer division

TODO: Verify that this is true! (It's not in any recent release notes.)

Division on integer literals used to truncate in earlier versions, but it will now convert into a rational number, i.e. 5 / 2 is not equal to 2, but to 2.5.

See here.

Gotchas

Moved

Clone this wiki locally