Solidity Style Guide

Layout

4 spaces per indentation level

Use spaces for indentation

Use blank lines

1
2
3
4
5
6
7
contract A {
// ...
}

contract B {
// ...
}

Blank lines may be omitted between groups of related one-liners

1
2
3
4
contract A {
function spam();
function ham();
}

Use UTF-8 or ASCII encoding

Place import statements at the top of the file

Functions should be be grouped according to their visibility and ordered:

  • constructor

  • fallback function (if exists)

  • external

  • public

  • internal

  • private

Within a grouping, place the constant functions last.

Use white spaces in expressions

Avoid extraneous whitespaces inthe following situations: immediately inside parenthesis, brackets or braces, with the exception of single line function declaration.

1
2
3
4
5
spam(ham[1], Coin({name: "han"})):

// exception

function singleLine() { spam() }

More than one space around an assignment or other operator to align with

1
2
3
x = 1;
y = 2;
long_variable = 3;

Don’t include a whitespace in the fallback function

1
2
3
4
5
6
7
function() {
// ...
}
// not
function () {
// ...
}

For control structure whose body contains a single statement, omitting the braces is ok if the statement is contained on a single line

1
2
3
4
if (x < 10)
x += 1;
else
x -= 1;

Function declaration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function increment(uint x) returns (uint) {
return x + 1;
}
// visibility precedes custom modifier
function increment(uint x) public onlyOwner returns (uint) {
return x + 1;
}
// long parameters list
function increment(
uint x1,
uint x2,
uint x3,
uint x4,
uint x5,
uint x6,
) {
// ...
}
// long visibility modifier list
function increment (uint x)
public
onlyOwner
priced
{
// ...
}

Stirng should be quoted with double-quotes.

Contract and Library Names should be named using the CapWords style.

1
2
SimpleToken,
SmartBank,

Event Names should use CapWords

Function Names should use CamelCase

Function Arguments should use CamelCase

Local and State Variables should use CamelCase

Constants should be named with all captial letters with underscores seperating words: MAX_BLOCKS

Modifier should use CamelCase

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×