Skip to content

Commit aea5ed8

Browse files
Janthermattiaerre
authored andcommitted
adding a styleguide (#131)
* adding a styleguide * Adding tests for every scenario in the styleguides and added the necessary TODOs to be fully compliant. Scenarios like function order or enforcing visibility in every function where not included as it is not a task for prettier. * double checked every case where the plugin works accordingly to the Style Guide
1 parent 7cd752a commit aea5ed8

File tree

11 files changed

+1445
-0
lines changed

11 files changed

+1445
-0
lines changed

STYLEGUIDE.md

Lines changed: 382 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,382 @@
1+
# Style Guide
2+
3+
## Indentation
4+
5+
- [x] Use 4 spaces per indentation level.
6+
7+
## Tabs or Spaces
8+
9+
- [x] Spaces are the preferred indentation method.
10+
- [x] Mixing tabs and spaces should be avoided.
11+
12+
## Blank Lines
13+
14+
- [ ] Surround top level declarations in solidity source with two blank lines.
15+
16+
```Solidity
17+
pragma solidity >=0.4.0 <0.7.0;
18+
19+
contract A {
20+
// ...
21+
}
22+
23+
24+
contract B {
25+
// ...
26+
}
27+
28+
29+
contract C {
30+
// ...
31+
}
32+
```
33+
34+
- [ ] Within a contract surround function declarations with a single blank line.
35+
36+
Blank lines may be omitted between groups of related one-liners (such as stub functions for an abstract contract)
37+
38+
```Solidity
39+
pragma solidity >=0.4.0 <0.7.0;
40+
41+
contract A {
42+
function spam() public pure;
43+
function ham() public pure;
44+
}
45+
46+
47+
contract B is A {
48+
function spam() public pure {
49+
// ...
50+
}
51+
52+
function ham() public pure {
53+
// ...
54+
}
55+
}
56+
```
57+
58+
## Maximum Line Length
59+
60+
Keeping lines under the [PEP 8 recommendation](https://www.python.org/dev/peps/pep-0008/#maximum-line-length) to a maximum of 79 (or 99) characters helps readers easily parse the code.
61+
62+
Wrapped lines should conform to the following guidelines.
63+
64+
1. The first argument should not be attached to the opening parenthesis.
65+
2. One, and only one, indent should be used.
66+
3. Each argument should fall on its own line.
67+
4. The terminating element, :code:`);`, should be placed on the final line by itself.
68+
69+
- [x] Function Calls
70+
71+
```Solidity
72+
thisFunctionCallIsReallyLong(
73+
longArgument1,
74+
longArgument2,
75+
longArgument3
76+
);
77+
```
78+
79+
- [x] Assignment Statements
80+
81+
```Solidity
82+
thisIsALongNestedMapping[being][set][to_some_value] = someFunction(
83+
argument1,
84+
argument2,
85+
argument3,
86+
argument4
87+
);
88+
```
89+
90+
- [x] Event Definitions and Event Emitters
91+
92+
```Solidity
93+
event LongAndLotsOfArgs(
94+
address sender,
95+
address recipient,
96+
uint256 publicKey,
97+
uint256 amount,
98+
bytes32[] options
99+
);
100+
101+
LongAndLotsOfArgs(
102+
sender,
103+
recipient,
104+
publicKey,
105+
amount,
106+
options
107+
);
108+
```
109+
110+
## Source File Encoding
111+
112+
- [x] UTF-8 or ASCII encoding is preferred.
113+
114+
## Whitespace in Expressions
115+
116+
Avoid extraneous whitespace in the following situations:
117+
118+
- [x] Immediately inside parenthesis, brackets or braces, with the exception of single line function declarations.
119+
120+
```Solidity
121+
spam(ham[1], Coin({name: "ham"}));
122+
```
123+
124+
- [x] Immediately before a comma, semicolon:
125+
126+
```Solidity
127+
function spam(uint i, Coin coin) public;
128+
```
129+
130+
- [x] More than one space around an assignment or other operator to align with another:
131+
132+
```Solidity
133+
x = 1;
134+
y = 2;
135+
long_variable = 3;
136+
```
137+
138+
- [x] Don't include a whitespace in the fallback function:
139+
140+
```Solidity
141+
function() external {
142+
...
143+
}
144+
```
145+
146+
## Control Structures
147+
148+
- [x] The braces denoting the body of a contract, library, functions and structs should:
149+
150+
- open on the same line as the declaration
151+
- close on their own line at the same indentation level as the beginning of the declaration.
152+
- The opening brace should be proceeded by a single space.
153+
154+
```Solidity
155+
pragma solidity >=0.4.0 <0.7.0;
156+
157+
contract Coin {
158+
struct Bank {
159+
address owner;
160+
uint balance;
161+
}
162+
}
163+
```
164+
165+
- [x] The same recommendations apply to the control structures `if`, `else`, `while`, and `for`.
166+
167+
Additionally there should be a single space between the control structures `if`, `while`, and `for` and the parenthetic block representing the conditional, as well as a single space between the conditional parenthetic block and the opening brace.
168+
169+
```Solidity
170+
if (...) {
171+
...
172+
}
173+
174+
for (...) {
175+
...
176+
}
177+
```
178+
179+
- [ ] For control structures whose body contains a single statement, omitting the braces is ok _if_ the statement is contained on a single line.
180+
181+
```Solidity
182+
if (x < 10)
183+
x += 1;
184+
```
185+
186+
- [x] For `if` blocks which have an `else` or `else if` clause, the `else` should be placed on the same line as the `if`'s closing brace. This is an exception compared to the rules of other block-like structures.
187+
188+
```Solidity
189+
if (x < 3) {
190+
x += 1;
191+
} else if (x > 7) {
192+
x -= 1;
193+
} else {
194+
x = 5;
195+
}
196+
197+
198+
if (x < 3)
199+
x += 1;
200+
else
201+
x -= 1;
202+
```
203+
204+
## Function Declaration
205+
206+
- [x] For short function declarations, it is recommended for the opening brace of the function body to be kept on the same line as the function declaration.
207+
208+
The closing brace should be at the same indentation level as the function declaration.
209+
210+
The opening brace should be preceded by a single space.
211+
212+
```Solidity
213+
function increment(uint x) public pure returns (uint) {
214+
return x + 1;
215+
}
216+
217+
function increment(uint x) public pure onlyowner returns (uint) {
218+
return x + 1;
219+
}
220+
```
221+
222+
[x] The visibility modifier for a function should come before any custom modifiers.
223+
224+
```Solidity
225+
function kill() public onlyowner {
226+
selfdestruct(owner);
227+
}
228+
```
229+
230+
- [x] For long function declarations, it is recommended to drop each argument onto it's own line at the same indentation level as the function body. The closing parenthesis and opening bracket should be placed on their own line as well at the same indentation level as the function declaration.
231+
232+
```Solidity
233+
function thisFunctionHasLotsOfArguments(
234+
address a,
235+
address b,
236+
address c,
237+
address d,
238+
address e,
239+
address f
240+
)
241+
public
242+
{
243+
doSomething();
244+
}
245+
```
246+
247+
- [x] If a long function declaration has modifiers, then each modifier should be dropped to its own line.
248+
249+
```Solidity
250+
function thisFunctionNameIsReallyLong(address x, address y, address z)
251+
public
252+
onlyowner
253+
priced
254+
returns (address)
255+
{
256+
doSomething();
257+
}
258+
259+
function thisFunctionNameIsReallyLong(
260+
address x,
261+
address y,
262+
address z,
263+
)
264+
public
265+
onlyowner
266+
priced
267+
returns (address)
268+
{
269+
doSomething();
270+
}
271+
```
272+
273+
- [x] Multiline output parameters and return statements should follow the same style recommended for wrapping long lines found in the [Maximum Line Length](#maximum_line_length) section.
274+
275+
```Solidity
276+
function thisFunctionNameIsReallyLong(
277+
address a,
278+
address b,
279+
address c
280+
)
281+
public
282+
returns (
283+
address someAddressName,
284+
uint256 LongArgument,
285+
uint256 Argument
286+
)
287+
{
288+
doSomething()
289+
290+
return (
291+
veryLongReturnArg1,
292+
veryLongReturnArg2,
293+
veryLongReturnArg3
294+
);
295+
}
296+
```
297+
298+
- [ ] For constructor functions on inherited contracts whose bases require arguments, it is recommended to drop the base constructors onto new lines in the same manner as modifiers if the function declaration is long or hard to read.
299+
300+
```Solidity
301+
pragma solidity >=0.4.0 <0.7.0;
302+
303+
// Base contracts just to make this compile
304+
contract B {
305+
constructor(uint) public {
306+
}
307+
}
308+
309+
310+
contract C {
311+
constructor(uint, uint) public {
312+
}
313+
}
314+
315+
316+
contract D {
317+
constructor(uint) public {
318+
}
319+
}
320+
321+
322+
contract A is B, C, D {
323+
uint x;
324+
325+
constructor(uint param1, uint param2, uint param3, uint param4, uint param5)
326+
B(param1)
327+
C(param2, param3)
328+
D(param4)
329+
public
330+
{
331+
// do something with param5
332+
x = param5;
333+
}
334+
}
335+
```
336+
337+
These guidelines for function declarations are intended to improve readability. Authors should use their best judgement as this guide does not try to cover all possible permutations for function declarations.
338+
339+
## Mappings
340+
341+
- [x] In variable declarations, do not separate the keyword `mapping` from its type by a space. Do not separate any nested `mapping` keyword from its type by whitespace.
342+
343+
```Solidity
344+
mapping(uint => uint) map;
345+
mapping(address => bool) registeredAddresses;
346+
mapping(uint => mapping(bool => Data[])) public data;
347+
mapping(uint => mapping(uint => s)) data;
348+
```
349+
350+
## Variable Declarations
351+
352+
- [x] Declarations of array variables should not have a space between the type and the brackets.
353+
354+
```Solidity
355+
uint[] x;
356+
```
357+
358+
## Other Recommendations
359+
360+
- [ ] Strings should be quoted with double-quotes instead of single-quotes.
361+
362+
```Solidity
363+
str = "foo";
364+
str = "Hamlet says, 'To be or not to be...'";
365+
```
366+
367+
- [x] Surround operators with a single space on either side.
368+
369+
```Solidity
370+
x = 3;
371+
x = 100 / 10;
372+
x += 3 + 4;
373+
x |= y && z;
374+
```
375+
376+
- [ ] Operators with a higher priority than others can exclude surrounding whitespace in order to denote precedence. This is meant to allow for improved readability for complex statement. You should always use the same amount of whitespace on either side of an operator:
377+
378+
```Solidity
379+
x = 2**3 + 5;
380+
x = 2*y + 3*z;
381+
x = (a+b) * (a-b);
382+
```

0 commit comments

Comments
 (0)