Skip to content

Commit 8c5fbc5

Browse files
committed
Changed headings on Style Guide to Best Practices
1 parent 6816190 commit 8c5fbc5

File tree

1 file changed

+73
-54
lines changed

1 file changed

+73
-54
lines changed

documentation/language/07_style.md

Lines changed: 73 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
id: style
3-
title: Style Guide
4-
sidebar: Style Guide
3+
title: Best Practices
4+
sidebar: Best Practices
55
---
66
[general tags]: # ()
77

@@ -12,8 +12,71 @@ This guide is a living document.
1212
As new Leo programming conventions arise and old ones become obsolete this guide should reflect the changes.
1313
Feel free to add your comments and recommendations [here](#contributing).
1414

15+
## Content
1516

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
1780

1881
### Indentation
1982
4 spaces per indentation level.
@@ -113,50 +176,6 @@ let a: A = A {
113176
};
114177
```
115178

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-
160179
## Contributing
161180

162181
Thank you for helping make Leo better!
@@ -165,7 +184,7 @@ Before contributing, please view the [Contributor Code of Conduct](https://githu
165184
By participating in this project - In the issues, pull requests, or Gitter channels -
166185
you agree to abide by the terms.
167186

168-
## Report an Issue
187+
### Report an Issue
169188

170189
To report an issue, please use the [GitHub issues tracker](https://github.com/ProvableHQ/leo/issues). When reporting issues, please mention the following details:
171190

@@ -178,14 +197,14 @@ To report an issue, please use the [GitHub issues tracker](https://github.com/Pr
178197

179198
Reducing the source code that caused the issue to a bare minimum is always very helpful and sometimes clarifies a misunderstanding.
180199

181-
## Make a Pull Request
200+
### Make a Pull Request
182201

183202
Start by forking off of the `mainnet` branch to make your changes. Commit messages should clearly explain why and what you changed.
184203

185204
If you need to pull in any changes from the `mainnet` branch after making your fork (for example, to resolve potential merge conflicts),
186205
please avoid using git merge and instead, git rebase your branch. Rebasing will help us review your changes easily.
187206

188-
### Tools Required
207+
#### Tools Required
189208

190209
To build Leo from source you will need the following tools:
191210
- The latest Rust stable version and nightly version.
@@ -195,23 +214,23 @@ To build Leo from source you will need the following tools:
195214
- Clippy
196215
- Via rustup, if you didn't do the default rustup install `rustup component add clippy`.
197216

198-
### Formatting
217+
#### Formatting
199218

200219
Please do the following before opening a PR.
201220
- `cargo +nightly fmt --all` will format all your code.
202221
- `cargo clippy --all-features --examples --all --benches`
203222

204-
### Tests
223+
#### Tests
205224

206225
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`.
207226

208-
#### **Parser Tests**
227+
##### **Parser Tests**
209228

210229
In the root directory of the repository, there is a "tests" directory.
211230
To add a parser test, look at the Example Leo files in the parser sub-directory.
212231
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`.
213232

214-
### Grammar
233+
#### Grammar
215234

216235
[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.
217236
If your changes affect a grammar rule, we may ask you to modify it in that `.abnf` file.

0 commit comments

Comments
 (0)