You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/language/07_style.md
+42Lines changed: 42 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -92,6 +92,48 @@ program example.aleo {
92
92
}
93
93
```
94
94
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.
0 commit comments