Skip to content

Latest commit

 

History

History
382 lines (285 loc) · 8.18 KB

STYLEGUIDE.md

File metadata and controls

382 lines (285 loc) · 8.18 KB

Style Guide

Indentation

  • Use 4 spaces per indentation level.

Tabs or Spaces

  • Spaces are the preferred indentation method.
  • Mixing tabs and spaces should be avoided.

Blank Lines

  • Surround top level declarations in solidity source with two blank lines.
pragma solidity >=0.4.0 <0.7.0;

    contract A {
        // ...
    }


    contract B {
        // ...
    }


    contract C {
        // ...
    }
  • Within a contract surround function declarations with a single blank line.

    Blank lines may be omitted between groups of related one-liners (such as stub functions for an abstract contract)

pragma solidity >=0.4.0 <0.7.0;

contract A {
    function spam() public pure;
    function ham() public pure;
}


contract B is A {
    function spam() public pure {
        // ...
    }

    function ham() public pure {
        // ...
    }
}

Maximum Line Length

Keeping lines under the PEP 8 recommendation to a maximum of 79 (or 99) characters helps readers easily parse the code.

Wrapped lines should conform to the following guidelines.

  1. The first argument should not be attached to the opening parenthesis.
  2. One, and only one, indent should be used.
  3. Each argument should fall on its own line.
  4. The terminating element, :code:);, should be placed on the final line by itself.
  • Function Calls
thisFunctionCallIsReallyLong(
    longArgument1,
    longArgument2,
    longArgument3
);
  • Assignment Statements
thisIsALongNestedMapping[being][set][to_some_value] = someFunction(
    argument1,
    argument2,
    argument3,
    argument4
);
  • Event Definitions and Event Emitters
event LongAndLotsOfArgs(
    address sender,
    address recipient,
    uint256 publicKey,
    uint256 amount,
    bytes32[] options
);

LongAndLotsOfArgs(
    sender,
    recipient,
    publicKey,
    amount,
    options
);

Source File Encoding

  • UTF-8 or ASCII encoding is preferred.

Whitespace in Expressions

Avoid extraneous whitespace in the following situations:

  • Immediately inside parenthesis, brackets or braces, with the exception of single line function declarations.
spam(ham[1], Coin({name: "ham"}));
  • Immediately before a comma, semicolon:
function spam(uint i, Coin coin) public;
  • More than one space around an assignment or other operator to align with another:
x = 1;
y = 2;
long_variable = 3;
  • Don't include a whitespace in the fallback function:
function() external {
    ...
}

Control Structures

  • The braces denoting the body of a contract, library, functions and structs should:

    • open on the same line as the declaration
    • close on their own line at the same indentation level as the beginning of the declaration.
    • The opening brace should be proceeded by a single space.
pragma solidity >=0.4.0 <0.7.0;

contract Coin {
    struct Bank {
        address owner;
        uint balance;
    }
}
  • The same recommendations apply to the control structures if, else, while, and for.

    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.

if (...) {
    ...
}

for (...) {
    ...
}
  • For control structures whose body contains a single statement, omitting the braces is ok if the statement is contained on a single line.
if (x < 10)
    x += 1;
  • 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.
if (x < 3) {
    x += 1;
} else if (x > 7) {
    x -= 1;
} else {
    x = 5;
}


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

Function Declaration

  • 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.

    The closing brace should be at the same indentation level as the function declaration.

    The opening brace should be preceded by a single space.

function increment(uint x) public pure returns (uint) {
    return x + 1;
}

function increment(uint x) public pure onlyowner returns (uint) {
    return x + 1;
}

- [x] The visibility modifier for a function should come before any custom modifiers.

function kill() public onlyowner {
    selfdestruct(owner);
}
  • 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.
function thisFunctionHasLotsOfArguments(
    address a,
    address b,
    address c,
    address d,
    address e,
    address f
)
    public
{
    doSomething();
}
  • If a long function declaration has modifiers, then each modifier should be dropped to its own line.
function thisFunctionNameIsReallyLong(address x, address y, address z)
    public
    onlyowner
    priced
    returns (address)
{
    doSomething();
}

function thisFunctionNameIsReallyLong(
    address x,
    address y,
    address z,
)
    public
    onlyowner
    priced
    returns (address)
{
    doSomething();
}
  • Multiline output parameters and return statements should follow the same style recommended for wrapping long lines found in the Maximum Line Length section.
function thisFunctionNameIsReallyLong(
    address a,
    address b,
    address c
)
    public
    returns (
        address someAddressName,
        uint256 LongArgument,
        uint256 Argument
    )
{
    doSomething()

    return (
        veryLongReturnArg1,
        veryLongReturnArg2,
        veryLongReturnArg3
    );
}
  • 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.
pragma solidity >=0.4.0 <0.7.0;

// Base contracts just to make this compile
contract B {
    constructor(uint) public {
    }
}


contract C {
    constructor(uint, uint) public {
    }
}


contract D {
    constructor(uint) public {
    }
}


contract A is B, C, D {
    uint x;

    constructor(uint param1, uint param2, uint param3, uint param4, uint param5)
        B(param1)
        C(param2, param3)
        D(param4)
        public
    {
        // do something with param5
        x = param5;
    }
}

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.

Mappings

  • 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.
mapping(uint => uint) map;
mapping(address => bool) registeredAddresses;
mapping(uint => mapping(bool => Data[])) public data;
mapping(uint => mapping(uint => s)) data;

Variable Declarations

  • Declarations of array variables should not have a space between the type and the brackets.
uint[] x;

Other Recommendations

  • Strings should be quoted with double-quotes instead of single-quotes.
str = "foo";
str = "Hamlet says, 'To be or not to be...'";
  • Surround operators with a single space on either side.
x = 3;
x = 100 / 10;
x += 3 + 4;
x |= y && z;
  • 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:
x = 2**3 + 5;
x = 2*y + 3*z;
x = (a+b) * (a-b);