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
+73-54Lines changed: 73 additions & 54 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
id: style
3
-
title: Style Guide
4
-
sidebar: Style Guide
3
+
title: Best Practices
4
+
sidebar: Best Practices
5
5
---
6
6
[general tags]: #()
7
7
@@ -12,8 +12,71 @@ This guide is a living document.
12
12
As new Leo programming conventions arise and old ones become obsolete this guide should reflect the changes.
13
13
Feel free to add your comments and recommendations [here](#contributing).
14
14
15
+
## Content
15
16
16
-
## Code Layout
17
+
### Conditional Branches
18
+
19
+
The Leo compiler rewrites if-else statements inside `transitions` into a sequence of ternary expressions.
20
+
This is because the underlying circuit construction does not support branching.
21
+
For precise control over the circuit size, it is recommended to use ternary expressions directly.
22
+
23
+
```leo title="Example:"
24
+
if (condition) {
25
+
return a;
26
+
} else {
27
+
return b;
28
+
}
29
+
```
30
+
31
+
```leo title="Alternative:"
32
+
return condition ? a : b;
33
+
```
34
+
35
+
#### Why?
36
+
Ternary expressions are the cheapest form of conditional.
37
+
We can resolve the *first expression* and *second expression* values before evaluating the *condition*.
38
+
This is very easy to convert into a circuit because we know that each expression does not depend on information in later statements.
39
+
40
+
In the original `Example`,
41
+
We cannot resolve the return statements before evaluating the condition.
42
+
As a solution, Leo creates branches in the circuit so both paths can be evaluated.
43
+
44
+
```leo title="branch 1, condition = true"
45
+
return a;
46
+
```
47
+
48
+
```leo title="branch 2, condition = false"
49
+
return b;
50
+
```
51
+
When the input value `condition` is fetched at proving time, we select a branch of the circuit to evaluate.
52
+
Observe that the statement `return a` is repeated in both branches.
53
+
The cost of every computation within the conditional will be doubled.
54
+
This greatly increases the constraint numbers and slows down the circuit.
55
+
56
+
57
+
### Async Functions vs. Blocks
58
+
59
+
60
+
Use the `async` block more often.
61
+
62
+
### Modules
63
+
64
+
As of v3.2.0, Leo supports a module system. For
65
+
66
+
67
+
Sounds good to me. Is this just for readability/cleanliness, or because of maximum file size?
68
+
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.
70
+
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.
74
+
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.
76
+
77
+
78
+
79
+
## Layout
17
80
18
81
### Indentation
19
82
4 spaces per indentation level.
@@ -113,50 +176,6 @@ let a: A = A {
113
176
};
114
177
```
115
178
116
-
## Common Patterns
117
-
118
-
Building off of the style guide, here is a list of common patterns that a Leo developer may encounter
119
-
as well as the recommended code solution.
120
-
121
-
### Conditional Branches
122
-
123
-
The Leo compiler rewrites if-else statements inside `transitions` into a sequence of ternary expressions.
124
-
This is because the underlying circuit construction does not support branching.
125
-
For precise control over the circuit size, it is recommended to use ternary expressions directly.
126
-
127
-
```leo title="Example:"
128
-
if (condition) {
129
-
return a;
130
-
} else {
131
-
return b;
132
-
}
133
-
```
134
-
135
-
```leo title="Alternative:"
136
-
return condition ? a : b;
137
-
```
138
-
139
-
#### Why?
140
-
Ternary expressions are the cheapest form of conditional.
141
-
We can resolve the *first expression* and *second expression* values before evaluating the *condition*.
142
-
This is very easy to convert into a circuit because we know that each expression does not depend on information in later statements.
143
-
144
-
In the original `Example`,
145
-
We cannot resolve the return statements before evaluating the condition.
146
-
As a solution, Leo creates branches in the circuit so both paths can be evaluated.
147
-
148
-
```leo title="branch 1, condition = true"
149
-
return a;
150
-
```
151
-
152
-
```leo title="branch 2, condition = false"
153
-
return b;
154
-
```
155
-
When the input value `condition` is fetched at proving time, we select a branch of the circuit to evaluate.
156
-
Observe that the statement `return a` is repeated in both branches.
157
-
The cost of every computation within the conditional will be doubled.
158
-
This greatly increases the constraint numbers and slows down the circuit.
159
-
160
179
## Contributing
161
180
162
181
Thank you for helping make Leo better!
@@ -165,7 +184,7 @@ Before contributing, please view the [Contributor Code of Conduct](https://githu
165
184
By participating in this project - In the issues, pull requests, or Gitter channels -
166
185
you agree to abide by the terms.
167
186
168
-
## Report an Issue
187
+
###Report an Issue
169
188
170
189
To report an issue, please use the [GitHub issues tracker](https://github.com/ProvableHQ/leo/issues). When reporting issues, please mention the following details:
171
190
@@ -178,14 +197,14 @@ To report an issue, please use the [GitHub issues tracker](https://github.com/Pr
178
197
179
198
Reducing the source code that caused the issue to a bare minimum is always very helpful and sometimes clarifies a misunderstanding.
180
199
181
-
## Make a Pull Request
200
+
###Make a Pull Request
182
201
183
202
Start by forking off of the `mainnet` branch to make your changes. Commit messages should clearly explain why and what you changed.
184
203
185
204
If you need to pull in any changes from the `mainnet` branch after making your fork (for example, to resolve potential merge conflicts),
186
205
please avoid using git merge and instead, git rebase your branch. Rebasing will help us review your changes easily.
187
206
188
-
### Tools Required
207
+
####Tools Required
189
208
190
209
To build Leo from source you will need the following tools:
191
210
- The latest Rust stable version and nightly version.
@@ -195,23 +214,23 @@ To build Leo from source you will need the following tools:
195
214
- Clippy
196
215
- Via rustup, if you didn't do the default rustup install `rustup component add clippy`.
197
216
198
-
### Formatting
217
+
####Formatting
199
218
200
219
Please do the following before opening a PR.
201
220
-`cargo +nightly fmt --all` will format all your code.
If your code adds new functionality, please write tests to confirm the new features function as expected. Refer to existing tests for examples of how tests are expected to be written. Please read refer to the [parser tests section](#parser-tests). To run the tests please use the following command `cargo test --all --features ci_skip --no-fail-fast`.
207
226
208
-
#### **Parser Tests**
227
+
#####**Parser Tests**
209
228
210
229
In the root directory of the repository, there is a "tests" directory.
211
230
To add a parser test, look at the Example Leo files in the parser sub-directory.
212
231
Then when running the test command, make sure you have the environment variable `CLEAR_LEO_TEST_EXPECTATIONS` set to true. For example, on a UNIX environment, you could run the following command `CLEAR_LEO_TEST_EXPECTATIONS=true cargo test --all --features ci_skip --no-fail-fast`.
213
232
214
-
### Grammar
233
+
####Grammar
215
234
216
235
[The `grammars` repository](https://github.com/ProvableHQ/grammars) contains a file [`leo.abnf`](https://github.com/ProvableHQ/grammars/blob/master/leo.abnf) that has the Leo grammar rules in the ABNF format.
217
236
If your changes affect a grammar rule, we may ask you to modify it in that `.abnf` file.
0 commit comments