Rainshower is a protocol that relies on being modular. Users can program and build their strategies, financial instruments, and more. Due to this, we must have a very high standard of code quality and readability. I am releasing the Rainshower solidity guidelines to gather feedback and suggestions. I hope that projects adopting the following guidelines will make DeFi more secure and auditable.
The following are some basic guidelines all contract code in this repository must follow. It is mostly copied from the JPL guide for defensive C/C++ programming, with added modifications for the EVM/Solidity.
- Do not stray outside the language definition.
- Practice using static source code analyzers.
- Do not ignore warnings.
- Use verifiable loop bounds for all loops.
- Do not use direct or indirect recursion.
- Declare variables at smallest possible level of scope.
- Check the return value of functions if relevant.
- Check the validity of values passed to functions if relevant.
- Use static and dynamic assertions as sanity checks.
- Make the order of evaluation in compound expressions explicit.
- Avoid using expressions with side effects.
- Avoid downcasting/narrow conversion; precision must not be lost.
- Avoid implicit type conversions.
- Avoid using Yul. Prioratise readability over gas.
- Do not place code above
- Place no more than one statement or declaration per line of text.
- Use short functions with a limited number of parameters.
- Avoid the use of modifiers.
- Do not use operator overloading.
- Do not use function overloading.
- Give variables and functions meaningful names.
- Try to pack structures into single storage slots, reducing width of fields where possible or giving up precision when unnecessary.
bytes, or other variable size types except when absolutely necessary.
- Don’t write to storage when data can be emitted as logs and queried out of band later if needed.
This document is inspired by the JPL C coding guidelines. A big thank you to everyone else who reviewed and gave feedback! It’s very much appreciated.