Order within contracts:
- Type declarations
- Constants
- Immutable state variables (Set only in constructor)
- State variables
- Events
- Modifiers
- Constructor
- Fallback function
- External functions (constant functions last)
- Public funcions (constant functions last)
- Internal functions (constant functions last)
- Private functions (constant functions last)
Order of operations within an external or public function:
pure
Validate input (require
)constant
Read state, compute and validate more (require
andassert
)- Write state (from here only
pure
operations allowed) (norequire
orassert
) - Call external functions (
assert
allowed again) - Write logs
- Return
The critical section is between the first read and the last write. In this region the control flow needs to be extremely reliable.
Abstract internal functions are used through a Mixin interface; a constract with an M
prefix containing only abstract internal functions.
Constants and immutable state variables are ALL_CAPS. Internal constants go before private ones. All constants are either internal or private.
Mutable state variables are _camelCase with a _
prefix. State variables should always be private
or, when intended to be access by a subclass, internal
. This is to avoid uncessary public functions cluthering the ABI, or accidental collision/overriding a state variable in a subclass.
Don't use uint
, always be explicit and use uint256
.
Only mutable state variables can start with _
. In particular functions, arguments and variables do not.
Log events start with Log
.
Mark contracts Trusted or Untrusted.
[]: https://github.com/ConsenSys/smart-contract-best-practices#mark-untrusted-contracts