Skip to main content
Learn professional coding standards, performance optimization techniques, and security best practices for Leo development.

Code Style and Standards

Follow these conventions from the Leo compiler codebase (based on CONTRIBUTING.md and AGENTS.md).

Comments

Write clear, complete comments:
  • Use line comments (//) over block comments
  • Start with capital letter, end with period
  • Place comments on their own line above the code
  • Keep comments concise and focused on “why”, not “what”

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 from CONTRIBUTING.md:

Memory Management

Pass by reference when ownership isn’t needed:
Avoid creating unnecessary intermediate values:
Balance efficiency with readability.
Choose the right type for your use case:

Loop Optimization

Loops are unrolled at compile time:
Excessive loop unrolling dramatically increases circuit size and compilation time. Keep iteration counts small.

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:
Follow the Checks-Effects-Interactions pattern: validate inputs, update state, then perform external interactions.

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