Skip to content
This repository was archived by the owner on May 4, 2026. It is now read-only.

Commit 9d42653

Browse files
authored
Merge pull request #581 from ProvableHQ/mohammadfawaz/578
Libraries in best practices
2 parents 10bf5c4 + acf30eb commit 9d42653

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

β€Ždocumentation/language/07_style.mdβ€Ž

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,48 @@ program example.aleo {
9292
}
9393
```
9494

95+
### Libraries
96+
97+
[Libraries](./06_libraries.md) are the right tool for sharing reusable code across programs. The following recommendations apply when authoring or consuming them.
98+
99+
#### Extract shared logic into a library
100+
101+
When the same helper functions, constants, or `struct` definitions appear in more than one program, move them into a library. This avoids duplicating constraints and makes maintenance easier.
102+
103+
```
104+
packages/
105+
β”œβ”€β”€ math_utils/ ← shared library
106+
β”‚ └── src/lib.leo
107+
β”œβ”€β”€ token_a.aleo/
108+
β”‚ └── src/main.leo
109+
└── token_b.aleo/
110+
└── src/main.leo
111+
```
112+
113+
#### Libraries are side-effect-free
114+
115+
Libraries should be **stateless**: no `program { }` block, no `mapping`, no `record`, no entry functions. All state belongs in a program. If you find yourself wanting on-chain state in a library, split the logic into a helper library and a thin program wrapper.
116+
117+
#### Prefer library functions over duplicating logic
118+
119+
Repeating a multi-step computation inline in several programs multiplies the constraint count across each circuit. Centralising that logic in a library function makes the constraint cost obvious and keeps each program smaller.
120+
121+
#### Use submodules for large libraries
122+
123+
When a library grows beyond a few hundred lines, split it across submodules named after their responsibility (`geometry.leo`, `encoding.leo`, etc.) and keep `lib.leo` as the public surface re-exporting common items.
124+
125+
```
126+
math_utils/
127+
β”œβ”€β”€ src/
128+
β”‚ β”œβ”€β”€ lib.leo ← public API
129+
β”‚ β”œβ”€β”€ geometry.leo ← math_utils::geometry::*
130+
β”‚ └── encoding.leo ← math_utils::encoding::*
131+
```
132+
133+
#### Name libraries clearly
134+
135+
Library package names follow the same snake_case rule as programs. Prefer a single descriptive noun when possible (`math`, `encoding`, `token_utils`).
136+
95137
### Modules
96138

97139
For maximal code cleanliness and readability, take full advantage of Leo's module system:

0 commit comments

Comments
Β (0)