Skip to content

Commit 7d7db70

Browse files
committed
Updated best practices guide
1 parent 3c50bee commit 7d7db70

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

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

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ The Leo compiler rewrites if-else statements inside `transitions` into a sequenc
2020
This is because the underlying circuit construction does not support branching.
2121
For precise control over the circuit size, it is recommended to use ternary expressions directly.
2222

23-
```leo title="Example:"
23+
```leo title="If-Else:"
2424
if (condition) {
2525
return a;
2626
} else {
2727
return b;
2828
}
2929
```
3030

31-
```leo title="Alternative:"
31+
```leo title="Ternary:"
3232
return condition ? a : b;
3333
```
3434

@@ -56,25 +56,53 @@ This greatly increases the constraint numbers and slows down the circuit.
5656

5757
### Async Functions vs. Blocks
5858

59+
For code conciseness and readability, prefer using `async` blocks rather than a separately declared `async function`:
5960

60-
Use the `async` block more often.
6161

62-
### Modules
62+
```leo title="Async Function:"
63+
mapping accumulator: u8 => u64;
6364
64-
As of v3.2.0, Leo supports a module system. For
65+
async transition increment_accumulator() -> Future {
66+
return increment_state_onchain();
67+
}
68+
async function increment_accumulator_onchain(){
69+
let current_count: u64 = accumulator.get_or_use(0u8, 0u64);
70+
let new_count: u64 = current_count + 1u64;
71+
accumulator.set(0u8, new_count);
6572
73+
}
74+
```
6675

67-
Sounds good to me. Is this just for readability/cleanliness, or because of maximum file size?
76+
```leo title="Async Block:"
77+
mapping accumulator: u8 => u64;
6878
69-
Strictly readability/cleanliness. No impact on size since modules are flattened into a single program eventually anyways. I just think this makes reading a program much more pleasant.
79+
async transition increment_accumulator() -> Future {
80+
let f : Future = async {
81+
let current_count: u64 = accumulator.get_or_use(0u8, 0u64);
82+
let new_count: u64 = current_count + 1u64;
83+
accumulator.set(0u8, new_count);
84+
}
85+
return f;
86+
}
87+
```
7088

71-
Move all consts to a `constants.leo` module
72-
Move all inlines to a `utils.leo` module
73-
Consider moving some `struct`s to modules but that may not make sense in the general case.
7489

75-
The goal is to only have the interface of the program in `main.leo`. Every function I see should correspond to something I can call from an external context such as another program.
90+
### Modules
7691

92+
For maximal code cleanliness and readability, take full advantage of Leo's module system:
93+
```
94+
src
95+
β”œβ”€β”€ constants.leo
96+
β”œβ”€β”€ utils.leo
97+
β”œβ”€β”€ structs.leo
98+
└── main.leo
99+
```
100+
With the above structure, consider the following:
101+
- Move all `const`s to the `constants.leo` module
102+
- Move all `inline` functions to the `utils.leo` module
103+
- Move some `struct`s to modules (but this may not make sense in the general case)
77104

105+
The goal is to only have the interface of the program in `main.leo`. Every function should correspond to something than can be called from an external context such as another program. Note that there is no impact on final program size since modules are flattened into a single program eventually anyways.
78106

79107
## Layout
80108

0 commit comments

Comments
Β (0)