Code Style and Standards
Follow these conventions from the Leo compiler codebase (based onCONTRIBUTING.md and AGENTS.md).
Comments
Write clear, complete comments:Comment Guidelines
Comment Guidelines
Naming Conventions
1
Programs
Use descriptive names ending in
.aleo:2
Functions
Use snake_case with descriptive verbs:
3
Variables
Use snake_case with meaningful names:
4
Types
Use PascalCase for structs and records:
Type Annotations
Always use explicit type annotations:Explicit types improve readability and help catch type errors early.
Code Organization
Structure programs logically:Performance Optimization
Based on Leo’s internal coding conventions fromCONTRIBUTING.md:
Memory Management
Avoid Unnecessary Clones
Avoid Unnecessary Clones
Pass by reference when ownership isn’t needed:
Minimize Intermediate Allocations
Minimize Intermediate Allocations
Avoid creating unnecessary intermediate values:Balance efficiency with readability.
Use Appropriate Data Structures
Use Appropriate Data Structures
Choose the right type for your use case:
Loop Optimization
Loops are unrolled at compile time:Conditional Optimization
Minimize branching complexity:Security Best Practices
Input Validation
Always validate inputs:Validate all user inputs at function boundaries. Never trust external data.
Overflow Protection
Leo automatically checks for overflows, but be mindful:Access Control
Implement proper authorization:Reentrancy Protection
Be aware of cross-program calls:Private Data Handling
Use records for sensitive data:Error Handling
Fail Fast
Validate early and fail fast:Informative Assertions
Use assertions to document invariants:Testing Best Practices
Comprehensive Coverage
Test all code paths:tests/test_token.leo
Test Edge Cases
Include boundary condition tests:Documentation
Function Documentation
Document public functions:README Files
Include a README.md in your project:Testing
Commit Messages
Use clear, descriptive commit messages:Deployment Considerations
Constructor Configuration
Choose the right constructor policy:Gas Optimization
Minimize on-chain operations:Code Review Checklist
Before deploying, verify:1
Correctness
- All inputs validated
- Logic traced step-by-step
- Edge cases handled
- No overflow conditions
2
Security
- Access control implemented
- No information leakage
- Private data properly protected
- Fail-safe defaults used
3
Performance
- Loops are reasonably bounded
- No unnecessary allocations
- Efficient data structures used
- Minimal mapping operations
4
Testing
- All functions have tests
- Edge cases covered
- Failure cases tested
- Integration tests pass
5
Documentation
- Public functions documented
- Complex logic explained
- README present
- Version updated
Resources
From the Leo compiler development guidelines:- Follow coding conventions in
CONTRIBUTING.md - Review architectural patterns in
AGENTS.md - Study test examples in
tests/tests/compiler/ - Check error handling in
crates/errors/
Next Steps
- Review writing programs for code structure
- Learn testing strategies for comprehensive coverage
- Explore debugging techniques for troubleshooting
- Study package management for dependencies
//) over block comments