Code Organization
Use Clear Naming
Group Related Methods
Keep Methods Small
Each method should do one thing. If a method is getting long, break it into smaller private functions.Gas Optimization
Minimize Storage Writes
Storage operations are expensive. Batch updates when possible.Use Efficient Data Structures
| Use Case | Recommended |
|---|---|
| Key-value lookup | LookupMap |
| Iterable collection | Vector |
| Unique set | LookupSet |
| Small fixed data | LazyOption |
Avoid Large Return Values
Returning large amounts of data costs gas. Use pagination for lists.Security
Validate Inputs
Always check user inputs before processing.Check Caller Identity
For admin functions, verify the caller.Handle Errors Gracefully
Userequire! for expected failures, panic! for unexpected ones.
Be Careful with Cross-Contract Calls
Cross-contract calls can fail. Always handle the callback.Testing
Test Happy Path and Edge Cases
Use Descriptive Test Names
Quick Reference
Do
- Validate all inputs
- Use efficient data structures
- Write tests
- Keep methods focused
Don't
- Store unnecessary data
- Return large datasets
- Skip error handling
- Trust user input blindly