2015-12-07 20:16:25 +00:00
.. index :: style, coding style
#############
Style Guide
#############
***** ***** **
Introduction
***** ***** **
2022-02-16 00:17:05 +00:00
This guide is intended to provide coding conventions for writing Solidity code.
2015-12-07 20:16:25 +00:00
This guide should be thought of as an evolving document that will change over
time as useful conventions are found and old conventions are rendered obsolete.
Many projects will implement their own style guides. In the event of
conflicts, project specific style guides take precedence.
The structure and many of the recommendations within this style guide were
taken from python's
2023-02-01 11:26:44 +00:00
`pep8 style guide <https://peps.python.org/pep-0008/> `_ .
2015-12-07 20:16:25 +00:00
The goal of this guide is *not* to be the right way or the best way to write
2022-02-16 00:17:05 +00:00
Solidity code. The goal of this guide is *consistency* . A quote from python's
2023-02-01 11:26:44 +00:00
`pep8 <https://peps.python.org/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds> `_
2015-12-07 20:16:25 +00:00
captures this concept well.
2020-04-20 16:24:31 +00:00
.. note ::
2015-12-07 20:16:25 +00:00
A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is most important.
2020-04-20 16:24:31 +00:00
2022-02-16 00:17:05 +00:00
But most importantly: **know when to be inconsistent** -- sometimes the style guide just doesn't apply. When in doubt, use your best judgment. Look at other examples and decide what looks best. And don't hesitate to ask!
2015-12-07 20:16:25 +00:00
***** ***** *
Code Layout
***** ***** *
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
===========
2022-02-16 00:17:05 +00:00
Surround top level declarations in Solidity source with two blank lines.
2015-12-07 20:16:25 +00:00
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
2020-05-13 15:45:58 +00:00
// SPDX-License-Identifier: GPL-3.0
2020-09-08 08:48:04 +00:00
pragma solidity >=0.4.0 <0.9.0;
2018-08-09 19:10:53 +00:00
2015-12-07 20:16:25 +00:00
contract A {
2018-08-09 19:10:53 +00:00
// ...
2015-12-07 20:16:25 +00:00
}
contract B {
2018-08-09 19:10:53 +00:00
// ...
2015-12-07 20:16:25 +00:00
}
contract C {
2018-08-09 19:10:53 +00:00
// ...
2015-12-07 20:16:25 +00:00
}
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
2020-05-13 15:45:58 +00:00
// SPDX-License-Identifier: GPL-3.0
2020-09-08 08:48:04 +00:00
pragma solidity >=0.4.0 <0.9.0;
2018-08-09 19:10:53 +00:00
2015-12-07 20:16:25 +00:00
contract A {
2018-08-09 19:10:53 +00:00
// ...
2015-12-07 20:16:25 +00:00
}
contract B {
2018-08-09 19:10:53 +00:00
// ...
2015-12-07 20:16:25 +00:00
}
contract C {
2018-08-09 19:10:53 +00:00
// ...
2015-12-07 20:16:25 +00:00
}
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)
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
2020-05-13 15:45:58 +00:00
// SPDX-License-Identifier: GPL-3.0
2020-09-08 08:48:04 +00:00
pragma solidity >=0.6.0 <0.9.0;
2018-08-09 19:10:53 +00:00
2019-10-23 20:10:12 +00:00
abstract contract A {
2019-11-05 17:25:34 +00:00
function spam() public virtual pure;
function ham() public virtual pure;
2015-12-07 20:16:25 +00:00
}
contract B is A {
2019-12-12 19:12:42 +00:00
function spam() public pure override {
2018-08-09 19:10:53 +00:00
// ...
2015-12-07 20:16:25 +00:00
}
2019-12-12 19:12:42 +00:00
function ham() public pure override {
2018-08-09 19:10:53 +00:00
// ...
2015-12-07 20:16:25 +00:00
}
}
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
2020-05-13 15:45:58 +00:00
// SPDX-License-Identifier: GPL-3.0
2020-09-08 08:48:04 +00:00
pragma solidity >=0.6.0 <0.9.0;
2018-08-09 19:10:53 +00:00
2019-12-12 19:12:42 +00:00
abstract contract A {
function spam() virtual pure public;
function ham() public virtual pure;
}
contract B is A {
function spam() public pure override {
2018-08-09 19:10:53 +00:00
// ...
2015-12-07 20:16:25 +00:00
}
2019-12-12 19:12:42 +00:00
function ham() public pure override {
2018-08-09 19:10:53 +00:00
// ...
2015-12-07 20:16:25 +00:00
}
}
2018-02-23 18:10:01 +00:00
.. _maximum_line_length:
2018-06-29 17:54:44 +00:00
Maximum Line Length
2018-02-14 21:09:52 +00:00
===================
2022-02-16 04:15:12 +00:00
Maximum suggested line length is 120 characters.
2018-02-14 21:09:52 +00:00
Wrapped lines should conform to the following guidelines.
2018-06-29 17:54:44 +00:00
1. The first argument should not be attached to the opening parenthesis.
2018-02-14 21:09:52 +00:00
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
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2018-02-14 21:09:52 +00:00
thisFunctionCallIsReallyLong(
2018-06-29 17:54:44 +00:00
longArgument1,
longArgument2,
2018-02-14 21:09:52 +00:00
longArgument3
);
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2018-02-14 21:09:52 +00:00
2018-06-29 17:54:44 +00:00
thisFunctionCallIsReallyLong(longArgument1,
longArgument2,
2018-02-14 21:09:52 +00:00
longArgument3
);
2018-06-29 17:54:44 +00:00
thisFunctionCallIsReallyLong(longArgument1,
longArgument2,
2018-02-14 21:09:52 +00:00
longArgument3
2018-06-29 17:54:44 +00:00
);
2018-02-14 21:09:52 +00:00
thisFunctionCallIsReallyLong(
longArgument1, longArgument2,
longArgument3
2018-06-29 17:54:44 +00:00
);
2018-02-14 21:09:52 +00:00
thisFunctionCallIsReallyLong(
2018-06-29 17:54:44 +00:00
longArgument1,
longArgument2,
2018-02-14 21:09:52 +00:00
longArgument3
);
thisFunctionCallIsReallyLong(
2018-06-29 17:54:44 +00:00
longArgument1,
longArgument2,
longArgument3);
2018-02-14 21:09:52 +00:00
Assignment Statements
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2018-02-14 21:09:52 +00:00
2022-02-16 03:24:44 +00:00
thisIsALongNestedMapping[being][set][toSomeValue] = someFunction(
2018-02-14 21:09:52 +00:00
argument1,
argument2,
argument3,
argument4
);
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2018-02-14 21:09:52 +00:00
2022-02-16 03:24:44 +00:00
thisIsALongNestedMapping[being][set][toSomeValue] = someFunction(argument1,
2018-02-14 21:09:52 +00:00
argument2,
argument3,
argument4);
Event Definitions and Event Emitters
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2018-02-14 21:09:52 +00:00
event LongAndLotsOfArgs(
2018-07-10 07:17:33 +00:00
address sender,
address recipient,
2018-02-14 21:09:52 +00:00
uint256 publicKey,
uint256 amount,
bytes32[] options
);
LongAndLotsOfArgs(
sender,
recipient,
publicKey,
amount,
options
);
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2018-02-14 21:09:52 +00:00
2018-07-10 07:17:33 +00:00
event LongAndLotsOfArgs(address sender,
address recipient,
2018-02-14 21:09:52 +00:00
uint256 publicKey,
uint256 amount,
bytes32[] options);
LongAndLotsOfArgs(sender,
recipient,
publicKey,
amount,
2018-06-29 17:54:44 +00:00
options);
2018-02-14 21:09:52 +00:00
2015-12-07 20:16:25 +00:00
Source File Encoding
====================
UTF-8 or ASCII encoding is preferred.
Imports
2016-08-10 15:48:23 +00:00
=======
2015-12-07 20:16:25 +00:00
Import statements should always be placed at the top of the file.
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
2020-05-13 15:45:58 +00:00
// SPDX-License-Identifier: GPL-3.0
2020-09-08 08:48:04 +00:00
pragma solidity >=0.4.0 <0.9.0;
2015-12-07 20:16:25 +00:00
2018-08-09 19:10:53 +00:00
import "./Owned.sol";
2015-12-07 20:16:25 +00:00
contract A {
2018-08-09 19:10:53 +00:00
// ...
2015-12-07 20:16:25 +00:00
}
2022-01-27 16:11:35 +00:00
2018-02-13 06:52:24 +00:00
contract B is Owned {
2018-08-09 19:10:53 +00:00
// ...
2015-12-07 20:16:25 +00:00
}
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
2020-05-13 15:45:58 +00:00
// SPDX-License-Identifier: GPL-3.0
2020-09-08 08:48:04 +00:00
pragma solidity >=0.4.0 <0.9.0;
2018-08-09 19:10:53 +00:00
2015-12-07 20:16:25 +00:00
contract A {
2018-08-09 19:10:53 +00:00
// ...
2015-12-07 20:16:25 +00:00
}
2018-02-13 06:52:24 +00:00
import "./Owned.sol";
2015-12-07 20:16:25 +00:00
2018-02-13 06:52:24 +00:00
contract B is Owned {
2018-08-09 19:10:53 +00:00
// ...
2015-12-07 20:16:25 +00:00
}
2016-11-18 02:06:28 +00:00
Order of Functions
==================
2016-11-22 08:47:58 +00:00
Ordering helps readers identify which functions they can call and to find the constructor and fallback definitions easier.
2016-11-18 02:06:28 +00:00
Functions should be grouped according to their visibility and ordered:
- constructor
2019-09-09 16:22:02 +00:00
- receive function (if exists)
2016-11-18 02:06:28 +00:00
- fallback function (if exists)
- external
- public
- internal
- private
2018-05-08 11:08:22 +00:00
Within a grouping, place the `` view `` and `` pure `` functions last.
2016-11-18 02:06:28 +00:00
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2016-11-18 02:06:28 +00:00
2020-05-13 15:45:58 +00:00
// SPDX-License-Identifier: GPL-3.0
2020-09-29 07:53:50 +00:00
pragma solidity >=0.7.0 <0.9.0;
2016-11-18 02:06:28 +00:00
contract A {
2020-06-23 16:11:34 +00:00
constructor() {
2018-08-09 19:10:53 +00:00
// ...
2016-11-18 02:06:28 +00:00
}
2017-12-12 18:47:30 +00:00
2019-09-09 16:22:02 +00:00
receive() external payable {
// ...
}
fallback() external {
2018-08-09 19:10:53 +00:00
// ...
2016-11-18 02:06:28 +00:00
}
2017-12-12 18:47:30 +00:00
2016-11-18 02:06:28 +00:00
// External functions
// ...
2017-12-12 18:47:30 +00:00
2018-05-08 11:08:22 +00:00
// External functions that are view
// ...
// External functions that are pure
2016-11-18 02:06:28 +00:00
// ...
2017-12-12 18:47:30 +00:00
2016-11-18 02:06:28 +00:00
// Public functions
// ...
2017-12-12 18:47:30 +00:00
2016-11-18 02:06:28 +00:00
// Internal functions
// ...
2017-12-12 18:47:30 +00:00
2016-11-18 02:06:28 +00:00
// Private functions
// ...
}
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2016-11-18 02:06:28 +00:00
2020-05-13 15:45:58 +00:00
// SPDX-License-Identifier: GPL-3.0
2020-09-29 07:53:50 +00:00
pragma solidity >=0.7.0 <0.9.0;
2016-11-18 02:06:28 +00:00
contract A {
2017-12-12 18:47:30 +00:00
2016-11-18 02:06:28 +00:00
// External functions
// ...
2019-09-09 16:22:02 +00:00
fallback() external {
// ...
}
receive() external payable {
2018-08-09 19:10:53 +00:00
// ...
2018-06-28 16:08:45 +00:00
}
2016-11-18 02:06:28 +00:00
// Private functions
// ...
// Public functions
// ...
2020-06-23 16:11:34 +00:00
constructor() {
2018-08-09 19:10:53 +00:00
// ...
2016-11-18 02:06:28 +00:00
}
2017-12-12 18:47:30 +00:00
2016-11-18 02:06:28 +00:00
// Internal functions
2017-12-12 18:47:30 +00:00
// ...
2016-11-18 02:06:28 +00:00
}
2015-12-07 20:16:25 +00:00
Whitespace in Expressions
=========================
Avoid extraneous whitespace in the following situations:
2017-10-28 11:48:57 +00:00
Immediately inside parenthesis, brackets or braces, with the exception of single line function declarations.
2015-12-07 20:16:25 +00:00
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2016-05-05 18:40:35 +00:00
spam(ham[1], Coin({name: "ham"}));
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
2016-05-24 17:57:36 +00:00
spam( ham[ 1 ], Coin( { name: "ham" } ) );
2015-12-07 20:16:25 +00:00
2021-07-14 16:52:39 +00:00
Exception:
.. code-block :: solidity
2016-05-06 14:36:32 +00:00
2017-12-12 18:47:30 +00:00
function singleLine() public { spam(); }
2016-05-06 14:36:32 +00:00
2016-05-06 13:42:05 +00:00
Immediately before a comma, semicolon:
2015-12-07 20:16:25 +00:00
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2016-05-05 18:40:35 +00:00
2017-12-12 18:47:30 +00:00
function spam(uint i, Coin coin) public;
2016-05-05 18:40:35 +00:00
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
2017-12-12 18:47:30 +00:00
function spam(uint i , Coin coin) public ;
2015-12-07 20:16:25 +00:00
2020-04-20 16:24:31 +00:00
More than one space around an assignment or other operator to align with another:
2015-12-07 20:16:25 +00:00
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
x = 1;
y = 2;
2022-02-16 03:24:44 +00:00
longVariable = 3;
2015-12-07 20:16:25 +00:00
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
2022-02-16 03:24:44 +00:00
x = 1;
y = 2;
longVariable = 3;
2015-12-07 20:16:25 +00:00
2019-09-09 16:22:02 +00:00
Don't include a whitespace in the receive and fallback functions:
2016-11-18 02:13:33 +00:00
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2016-11-18 02:13:33 +00:00
2019-09-09 16:22:02 +00:00
receive() external payable {
...
}
fallback() external {
2016-11-18 02:13:33 +00:00
...
}
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2017-12-12 18:47:30 +00:00
2019-09-09 16:22:02 +00:00
receive () external payable {
...
}
fallback () external {
2016-11-18 02:13:33 +00:00
...
}
2015-12-07 20:16:25 +00:00
2019-09-09 16:22:02 +00:00
2015-12-07 20:16:25 +00:00
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.
2019-09-10 09:47:58 +00:00
* The opening brace should be preceded by a single space.
2015-12-07 20:16:25 +00:00
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
2020-05-13 15:45:58 +00:00
// SPDX-License-Identifier: GPL-3.0
2020-09-08 08:48:04 +00:00
pragma solidity >=0.4.0 <0.9.0;
2018-08-09 19:10:53 +00:00
2015-12-07 20:16:25 +00:00
contract Coin {
struct Bank {
address owner;
uint balance;
}
}
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
2020-05-13 15:45:58 +00:00
// SPDX-License-Identifier: GPL-3.0
2020-09-08 08:48:04 +00:00
pragma solidity >=0.4.0 <0.9.0;
2018-08-09 19:10:53 +00:00
2015-12-07 20:16:25 +00:00
contract Coin
{
struct Bank {
address owner;
uint balance;
}
}
2016-05-24 17:57:36 +00:00
The same recommendations apply to the control structures `` if `` , `` else `` , `` while `` ,
and `` for `` .
2015-12-07 20:16:25 +00:00
Additionally there should be a single space between the control structures
2016-05-24 17:57:36 +00:00
`` if `` , `` while `` , and `` for `` and the parenthetic block representing the
2015-12-07 20:16:25 +00:00
conditional, as well as a single space between the conditional parenthetic
block and the opening brace.
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
if (...) {
...
}
for (...) {
...
}
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
if (...)
{
...
}
while(...){
}
for (...) {
...;}
2016-05-06 15:11:00 +00:00
For control structures whose body contains a single statement, omitting the
2015-12-07 20:16:25 +00:00
braces is ok *if* the statement is contained on a single line.
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
if (x < 10)
x += 1;
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
if (x < 10)
someArray.push(Coin({
name: 'spam',
value: 42
}));
2016-05-24 17:57:36 +00:00
For `` if `` blocks which have an `` else `` or `` else if `` clause, the `` else `` should be
2016-08-24 19:27:46 +00:00
placed on the same line as the `` if `` 's closing brace. This is an exception compared
to the rules of other block-like structures.
2015-12-07 20:16:25 +00:00
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
if (x < 3) {
x += 1;
2016-08-24 19:27:46 +00:00
} else if (x > 7) {
2015-12-07 20:16:25 +00:00
x -= 1;
2016-08-24 19:27:46 +00:00
} else {
x = 5;
2015-12-07 20:16:25 +00:00
}
if (x < 3)
x += 1;
else
x -= 1;
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
if (x < 3) {
x += 1;
2016-08-24 19:27:46 +00:00
}
else {
2015-12-07 20:16:25 +00:00
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.
2018-02-22 18:18:09 +00:00
The opening brace should be preceded by a single space.
2015-12-07 20:16:25 +00:00
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
2017-12-12 18:47:30 +00:00
function increment(uint x) public pure returns (uint) {
2015-12-07 20:16:25 +00:00
return x + 1;
}
2020-11-01 18:15:09 +00:00
function increment(uint x) public pure onlyOwner returns (uint) {
2015-12-07 20:16:25 +00:00
return x + 1;
}
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
2017-12-12 18:47:30 +00:00
function increment(uint x) public pure returns (uint)
2015-12-07 20:16:25 +00:00
{
return x + 1;
}
2017-12-12 18:47:30 +00:00
function increment(uint x) public pure returns (uint){
2015-12-07 20:16:25 +00:00
return x + 1;
}
2017-12-12 18:47:30 +00:00
function increment(uint x) public pure returns (uint) {
2015-12-07 20:16:25 +00:00
return x + 1;
}
2017-12-12 18:47:30 +00:00
function increment(uint x) public pure returns (uint) {
2015-12-07 20:16:25 +00:00
return x + 1;}
2019-12-12 19:12:42 +00:00
The modifier order for a function should be:
2018-02-22 18:18:09 +00:00
2019-12-12 19:12:42 +00:00
1. Visibility
2. Mutability
3. Virtual
4. Override
5. Custom modifiers
2018-02-22 18:18:09 +00:00
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2018-02-22 18:18:09 +00:00
2019-12-12 19:12:42 +00:00
function balance(uint from) public view override returns (uint) {
return balanceOf[from];
2018-02-22 18:18:09 +00:00
}
2020-11-01 18:15:09 +00:00
function shutdown() public onlyOwner {
2015-12-07 20:16:25 +00:00
selfdestruct(owner);
}
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
2019-12-12 19:12:42 +00:00
function balance(uint from) public override view returns (uint) {
return balanceOf[from];
}
2020-11-01 18:15:09 +00:00
function shutdown() onlyOwner public {
2015-12-07 20:16:25 +00:00
selfdestruct(owner);
}
2016-09-06 09:24:06 +00:00
For long function declarations, it is recommended to drop each argument onto
2022-02-16 00:17:05 +00:00
its own line at the same indentation level as the function body. The closing
2015-12-07 20:16:25 +00:00
parenthesis and opening bracket should be placed on their own line as well at
the same indentation level as the function declaration.
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
function thisFunctionHasLotsOfArguments(
address a,
address b,
address c,
address d,
address e,
2016-05-09 20:42:57 +00:00
address f
2017-12-12 18:47:30 +00:00
)
public
{
2016-05-06 14:25:18 +00:00
doSomething();
2015-12-07 20:16:25 +00:00
}
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
function thisFunctionHasLotsOfArguments(address a, address b, address c,
2017-12-12 18:47:30 +00:00
address d, address e, address f) public {
2016-05-06 14:25:18 +00:00
doSomething();
2015-12-07 20:16:25 +00:00
}
function thisFunctionHasLotsOfArguments(address a,
address b,
address c,
address d,
address e,
2017-12-12 18:47:30 +00:00
address f) public {
2016-05-06 14:25:18 +00:00
doSomething();
2015-12-07 20:16:25 +00:00
}
function thisFunctionHasLotsOfArguments(
address a,
address b,
address c,
address d,
address e,
2017-12-12 18:47:30 +00:00
address f) public {
2016-05-06 14:25:18 +00:00
doSomething();
2015-12-07 20:16:25 +00:00
}
If a long function declaration has modifiers, then each modifier should be
2017-12-12 18:47:30 +00:00
dropped to its own line.
2015-12-07 20:16:25 +00:00
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
function thisFunctionNameIsReallyLong(address x, address y, address z)
public
2020-11-01 18:15:09 +00:00
onlyOwner
2015-12-07 20:16:25 +00:00
priced
returns (address)
{
2016-05-06 14:25:18 +00:00
doSomething();
2015-12-07 20:16:25 +00:00
}
function thisFunctionNameIsReallyLong(
address x,
address y,
2021-12-02 23:11:04 +00:00
address z
2015-12-07 20:16:25 +00:00
)
public
2020-11-01 18:15:09 +00:00
onlyOwner
2015-12-07 20:16:25 +00:00
priced
returns (address)
{
2016-05-06 14:25:18 +00:00
doSomething();
2015-12-07 20:16:25 +00:00
}
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
function thisFunctionNameIsReallyLong(address x, address y, address z)
public
2020-11-01 18:15:09 +00:00
onlyOwner
2015-12-07 20:16:25 +00:00
priced
returns (address) {
2016-05-06 14:25:18 +00:00
doSomething();
2015-12-07 20:16:25 +00:00
}
function thisFunctionNameIsReallyLong(address x, address y, address z)
2020-11-01 18:15:09 +00:00
public onlyOwner priced returns (address)
2015-12-07 20:16:25 +00:00
{
2016-05-06 14:25:18 +00:00
doSomething();
2015-12-07 20:16:25 +00:00
}
function thisFunctionNameIsReallyLong(address x, address y, address z)
public
2020-11-01 18:15:09 +00:00
onlyOwner
2015-12-07 20:16:25 +00:00
priced
returns (address) {
2016-05-06 14:25:18 +00:00
doSomething();
2015-12-07 20:16:25 +00:00
}
2018-02-23 18:10:01 +00:00
Multiline output parameters and return statements should follow the same style recommended for wrapping long lines found in the :ref: `Maximum Line Length <maximum_line_length>` section.
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2018-02-23 18:10:01 +00:00
function thisFunctionNameIsReallyLong(
address a,
address b,
address c
2018-06-29 17:54:44 +00:00
)
public
2018-02-23 18:10:01 +00:00
returns (
2018-06-29 17:54:44 +00:00
address someAddressName,
uint256 LongArgument,
2018-02-23 18:10:01 +00:00
uint256 Argument
)
2018-06-29 17:54:44 +00:00
{
2018-02-23 18:10:01 +00:00
doSomething()
2018-06-29 17:54:44 +00:00
2018-02-23 18:10:01 +00:00
return (
2018-06-29 17:54:44 +00:00
veryLongReturnArg1,
veryLongReturnArg2,
2018-02-23 18:10:01 +00:00
veryLongReturnArg3
);
}
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2018-02-23 18:10:01 +00:00
function thisFunctionNameIsReallyLong(
address a,
address b,
address c
2018-06-29 17:54:44 +00:00
)
public
returns (address someAddressName,
uint256 LongArgument,
2018-02-23 18:10:01 +00:00
uint256 Argument)
2018-06-29 17:54:44 +00:00
{
2018-02-23 18:10:01 +00:00
doSomething()
2018-06-29 17:54:44 +00:00
return (veryLongReturnArg1,
veryLongReturnArg1,
2018-02-23 18:10:01 +00:00
veryLongReturnArg1);
}
2016-05-06 15:11:00 +00:00
For constructor functions on inherited contracts whose bases require arguments,
2015-12-07 20:16:25 +00:00
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.
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
2020-05-13 15:45:58 +00:00
// SPDX-License-Identifier: GPL-3.0
2020-09-29 07:53:50 +00:00
pragma solidity >=0.7.0 <0.9.0;
2018-08-09 19:10:53 +00:00
// Base contracts just to make this compile
contract B {
2020-06-23 16:11:34 +00:00
constructor(uint) {
2018-08-09 19:10:53 +00:00
}
}
2022-01-27 16:11:35 +00:00
2018-08-09 19:10:53 +00:00
contract C {
2020-06-23 16:11:34 +00:00
constructor(uint, uint) {
2018-08-09 19:10:53 +00:00
}
}
2022-01-27 16:11:35 +00:00
2018-08-09 19:10:53 +00:00
contract D {
2020-06-23 16:11:34 +00:00
constructor(uint) {
2018-08-09 19:10:53 +00:00
}
}
2022-01-27 16:11:35 +00:00
2015-12-07 20:16:25 +00:00
contract A is B, C, D {
2018-08-09 19:10:53 +00:00
uint x;
2018-06-29 17:54:44 +00:00
constructor(uint param1, uint param2, uint param3, uint param4, uint param5)
2015-12-07 20:16:25 +00:00
B(param1)
C(param2, param3)
D(param4)
{
// do something with param5
2018-08-09 19:10:53 +00:00
x = param5;
2015-12-07 20:16:25 +00:00
}
}
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
2020-05-13 15:45:58 +00:00
// SPDX-License-Identifier: GPL-3.0
2020-09-29 07:53:50 +00:00
pragma solidity >=0.7.0 <0.9.0;
2019-05-30 12:34:52 +00:00
2018-08-09 19:10:53 +00:00
// Base contracts just to make this compile
contract B {
2020-06-23 16:11:34 +00:00
constructor(uint) {
2018-08-09 19:10:53 +00:00
}
}
2019-05-30 12:34:52 +00:00
2018-08-09 19:10:53 +00:00
contract C {
2020-06-23 16:11:34 +00:00
constructor(uint, uint) {
2018-08-09 19:10:53 +00:00
}
}
2019-05-30 12:34:52 +00:00
2018-08-09 19:10:53 +00:00
contract D {
2020-06-23 16:11:34 +00:00
constructor(uint) {
2018-08-09 19:10:53 +00:00
}
}
2019-05-30 12:34:52 +00:00
2015-12-07 20:16:25 +00:00
contract A is B, C, D {
2018-08-09 19:10:53 +00:00
uint x;
2018-06-29 17:54:44 +00:00
constructor(uint param1, uint param2, uint param3, uint param4, uint param5)
2015-12-07 20:16:25 +00:00
B(param1)
C(param2, param3)
2020-06-23 16:11:34 +00:00
D(param4) {
2018-08-09 19:10:53 +00:00
x = param5;
2015-12-07 20:16:25 +00:00
}
}
2019-05-30 12:34:52 +00:00
2018-08-09 19:10:53 +00:00
contract X is B, C, D {
uint x;
2018-06-29 17:54:44 +00:00
constructor(uint param1, uint param2, uint param3, uint param4, uint param5)
2015-12-07 20:16:25 +00:00
B(param1)
C(param2, param3)
2020-06-23 16:11:34 +00:00
D(param4) {
2019-05-30 12:34:52 +00:00
x = param5;
}
2015-12-07 20:16:25 +00:00
}
2019-05-30 12:34:52 +00:00
2016-05-06 14:19:28 +00:00
When declaring short functions with a single statement, it is permissible to do it on a single line.
2021-06-25 10:25:29 +00:00
Permissible:
.. code-block :: solidity
2016-05-06 14:19:28 +00:00
2017-12-12 18:47:30 +00:00
function shortFunction() public { doSomething(); }
2015-12-07 20:16:25 +00:00
These guidelines for function declarations are intended to improve readability.
2022-02-16 00:17:05 +00:00
Authors should use their best judgment as this guide does not try to cover all
2015-12-07 20:16:25 +00:00
possible permutations for function declarations.
Mappings
========
2018-11-12 11:06:59 +00:00
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.
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2018-11-12 11:06:59 +00:00
mapping(uint => uint) map;
mapping(address => bool) registeredAddresses;
mapping(uint => mapping(bool => Data[])) public data;
mapping(uint => mapping(uint => s)) data;
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2018-11-12 11:06:59 +00:00
mapping (uint => uint) map;
mapping( address => bool ) registeredAddresses;
mapping (uint => mapping (bool => Data[])) public data;
mapping(uint => mapping (uint => s)) data;
2015-12-07 20:16:25 +00:00
Variable Declarations
=====================
Declarations of array variables should not have a space between the type and
the brackets.
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2016-03-23 04:34:25 +00:00
uint[] x;
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2016-03-23 04:34:25 +00:00
uint [] x;
2015-12-07 20:16:25 +00:00
Other Recommendations
=====================
2016-08-10 15:48:23 +00:00
* Strings should be quoted with double-quotes instead of single-quotes.
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2016-08-10 15:48:23 +00:00
str = "foo";
str = "Hamlet says, 'To be or not to be...'";
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2016-08-10 15:48:23 +00:00
str = 'bar';
str = '"Be yourself; everyone else is already taken." -Oscar Wilde';
2015-12-07 20:16:25 +00:00
* Surround operators with a single space on either side.
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2021-07-14 17:32:42 +00:00
:force:
2015-12-07 20:16:25 +00:00
x = 3;
x = 100 / 10;
x += 3 + 4;
x |= y && z;
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2021-07-14 17:32:42 +00:00
:force:
2015-12-07 20:16:25 +00:00
x=3;
x = 100/10;
x += 3+4;
x |= y&&z;
* Operators with a higher priority than others can exclude surrounding
2015-12-07 22:31:58 +00:00
whitespace in order to denote precedence. This is meant to allow for
2022-02-16 00:17:05 +00:00
improved readability for complex statements. You should always use the same
2015-12-07 20:16:25 +00:00
amount of whitespace on either side of an operator:
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
x = 2**3 + 5;
x = 2*y + 3* z;
x = (a+b) * (a-b);
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2015-12-07 20:16:25 +00:00
x = 2** 3 + 5;
x = y+z;
x +=1;
2018-09-17 15:50:45 +00:00
***** ***** *****
Order of Layout
***** ***** *****
Layout contract elements in the following order:
2018-09-18 14:06:49 +00:00
1. Pragma statements
2. Import statements
3. Interfaces
4. Libraries
5. Contracts
2018-09-17 15:50:45 +00:00
Inside each contract, library or interface, use the following order:
1. Type declarations
2. State variables
3. Events
2022-11-04 07:58:19 +00:00
4. Errors
5. Modifiers
6. Functions
2018-09-17 15:50:45 +00:00
.. note ::
It might be clearer to declare types close to their use in events or state
variables.
2015-12-16 23:18:36 +00:00
2022-11-04 07:58:19 +00:00
Yes:
.. code-block :: solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.4 <0.9.0;
abstract contract Math {
error DivideByZero();
function divide(int256 numerator, int256 denominator) public virtual returns (uint256);
}
No:
.. code-block :: solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.4 <0.9.0;
abstract contract Math {
function divide(int256 numerator, int256 denominator) public virtual returns (uint256);
error DivideByZero();
}
2015-12-16 23:18:36 +00:00
***** ***** ***** ***
2015-12-07 20:16:25 +00:00
Naming Conventions
2015-12-16 23:18:36 +00:00
***** ***** ***** ***
Naming conventions are powerful when adopted and used broadly. The use of
different conventions can convey significant *meta* information that would
otherwise not be immediately available.
2016-01-06 23:26:08 +00:00
The naming recommendations given here are intended to improve the readability,
2015-12-16 23:18:36 +00:00
and thus they are not rules, but rather guidelines to try and help convey the
most information through the names of things.
2018-07-10 07:17:33 +00:00
Lastly, consistency within a codebase should always supersede any conventions
2015-12-16 23:18:36 +00:00
outlined in this document.
Naming Styles
=============
To avoid confusion, the following names will be used to refer to different
naming styles.
* `` b `` (single lowercase letter)
* `` B `` (single uppercase letter)
* `` lowercase ``
* `` UPPERCASE ``
* `` UPPER_CASE_WITH_UNDERSCORES ``
* `` CapitalizedWords `` (or CapWords)
* `` mixedCase `` (differs from CapitalizedWords by initial lowercase character!)
2019-09-10 10:05:52 +00:00
.. note :: When using initialisms in CapWords, capitalize all the letters of the initialisms. Thus HTTPServerError is better than HttpServerError. When using initialisms in mixedCase, capitalize all the letters of the initialisms, except keep the first one lower case if it is the beginning of the name. Thus xmlHTTPRequest is better than XMLHTTPRequest.
2015-12-17 15:57:02 +00:00
2015-12-16 23:18:36 +00:00
Names to Avoid
==============
* `` l `` - Lowercase letter el
* `` O `` - Uppercase letter oh
* `` I `` - Uppercase letter eye
Never use any of these for single letter variable names. They are often
indistinguishable from the numerals one and zero.
Contract and Library Names
==========================
2018-02-13 06:52:24 +00:00
* Contracts and libraries should be named using the CapWords style. Examples: `` SimpleToken `` , `` SmartBank `` , `` CertificateHashRepository `` , `` Player `` , `` Congress `` , `` Owned `` .
* Contract and library names should also match their filenames.
* If a contract file includes multiple contracts and/or libraries, then the filename should match the *core contract* . This is not recommended however if it can be avoided.
2020-04-20 16:24:31 +00:00
As shown in the example below, if the contract name is `` Congress `` and the library name is `` Owned `` , then their associated filenames should be `` Congress.sol `` and `` Owned.sol `` .
2018-02-13 06:52:24 +00:00
2021-06-25 10:25:29 +00:00
Yes:
.. code-block :: solidity
2018-02-13 06:52:24 +00:00
2020-05-13 15:45:58 +00:00
// SPDX-License-Identifier: GPL-3.0
2020-09-29 07:53:50 +00:00
pragma solidity >=0.7.0 <0.9.0;
2019-05-30 12:34:52 +00:00
2018-02-13 06:52:24 +00:00
// Owned.sol
contract Owned {
2019-05-30 12:34:52 +00:00
address public owner;
2018-02-13 06:52:24 +00:00
2019-05-30 12:34:52 +00:00
modifier onlyOwner {
require(msg.sender == owner);
_;
}
2018-02-13 06:52:24 +00:00
2022-11-04 07:58:19 +00:00
constructor() {
owner = msg.sender;
}
2019-05-30 12:34:52 +00:00
function transferOwnership(address newOwner) public onlyOwner {
owner = newOwner;
}
2018-02-13 06:52:24 +00:00
}
2021-06-25 10:25:29 +00:00
and in `` Congress.sol `` :
.. code-block :: solidity
2019-05-30 12:34:52 +00:00
2020-05-13 15:45:58 +00:00
// SPDX-License-Identifier: GPL-3.0
2020-09-08 08:48:04 +00:00
pragma solidity >=0.4.0 <0.9.0;
2019-05-30 12:34:52 +00:00
2018-02-13 06:52:24 +00:00
import "./Owned.sol";
2019-05-30 12:34:52 +00:00
2018-02-13 06:52:24 +00:00
contract Congress is Owned, TokenRecipient {
2018-08-09 19:10:53 +00:00
//...
2018-02-13 06:52:24 +00:00
}
2021-06-25 10:25:29 +00:00
No:
.. code-block :: solidity
2018-02-13 06:52:24 +00:00
2020-05-13 15:45:58 +00:00
// SPDX-License-Identifier: GPL-3.0
2020-09-29 07:53:50 +00:00
pragma solidity >=0.7.0 <0.9.0;
2019-05-30 12:34:52 +00:00
2018-02-13 06:52:24 +00:00
// owned.sol
contract owned {
2019-05-30 12:34:52 +00:00
address public owner;
2018-02-13 06:52:24 +00:00
2019-05-30 12:34:52 +00:00
modifier onlyOwner {
require(msg.sender == owner);
_;
}
2018-02-13 06:52:24 +00:00
2022-11-04 07:58:19 +00:00
constructor() {
owner = msg.sender;
}
2019-05-30 12:34:52 +00:00
function transferOwnership(address newOwner) public onlyOwner {
owner = newOwner;
}
2018-02-13 06:52:24 +00:00
}
2021-06-25 10:25:29 +00:00
and in `` Congress.sol `` :
.. code-block :: solidity
2019-05-30 12:34:52 +00:00
2020-12-12 03:35:17 +00:00
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.7.0;
2018-02-13 06:52:24 +00:00
import "./owned.sol";
2019-05-30 12:34:52 +00:00
2018-02-13 06:52:24 +00:00
contract Congress is owned, tokenRecipient {
2018-08-09 19:10:53 +00:00
//...
2018-02-13 06:52:24 +00:00
}
2015-12-16 23:18:36 +00:00
2018-01-14 18:53:45 +00:00
Struct Names
==========================
Structs should be named using the CapWords style. Examples: `` MyCoin `` , `` Position `` , `` PositionXY `` .
2017-10-25 07:33:26 +00:00
Event Names
===========
2015-12-16 23:18:36 +00:00
2017-10-25 07:33:26 +00:00
Events should be named using the CapWords style. Examples: `` Deposit `` , `` Transfer `` , `` Approval `` , `` BeforeTransfer `` , `` AfterTransfer `` .
2015-12-16 23:18:36 +00:00
Function Names
==============
2020-06-23 16:11:34 +00:00
Functions should use mixedCase. Examples: `` getBalance `` , `` transfer `` , `` verifyOwner `` , `` addMember `` , `` changeOwner `` .
2015-12-16 23:18:36 +00:00
2017-10-25 19:58:18 +00:00
Function Argument Names
=======================
2017-10-25 07:33:26 +00:00
Function arguments should use mixedCase. Examples: `` initialSupply `` , `` account `` , `` recipientAddress `` , `` senderAddress `` , `` newOwner `` .
2015-12-07 20:16:25 +00:00
2015-12-16 23:18:36 +00:00
When writing library functions that operate on a custom struct, the struct
should be the first argument and should always be named `` self `` .
2017-10-25 19:58:18 +00:00
Local and State Variable Names
==============================
2015-12-16 23:18:36 +00:00
2017-10-25 07:33:26 +00:00
Use mixedCase. Examples: `` totalSupply `` , `` remainingSupply `` , `` balancesOf `` , `` creatorAddress `` , `` isPreSale `` , `` tokenExchangeRate `` .
2015-12-16 23:18:36 +00:00
Constants
=========
Constants should be named with all capital letters with underscores separating
2018-03-30 19:28:15 +00:00
words. Examples: `` MAX_BLOCKS `` , `` TOKEN_NAME `` , `` TOKEN_TICKER `` , `` CONTRACT_VERSION `` .
2015-12-16 23:18:36 +00:00
2017-10-25 07:33:26 +00:00
Modifier Names
==============
2015-12-16 23:18:36 +00:00
2017-10-25 07:33:26 +00:00
Use mixedCase. Examples: `` onlyBy `` , `` onlyAfter `` , `` onlyDuringThePreSale `` .
2015-12-16 23:18:36 +00:00
2017-12-09 14:53:40 +00:00
Enums
=====
Enums, in the style of simple type declarations, should be named using the CapWords style. Examples: `` TokenGroup `` , `` Frame `` , `` HashStyle `` , `` CharacterLocation `` .
2017-10-25 07:33:26 +00:00
Avoiding Naming Collisions
==========================
2015-12-16 23:18:36 +00:00
2022-02-16 03:24:44 +00:00
* `` singleTrailingUnderscore_ ``
2015-12-16 23:18:36 +00:00
2022-02-16 20:06:33 +00:00
This convention is suggested when the desired name collides with that of
an existing state variable, function, built-in or otherwise reserved name.
2015-12-16 23:18:36 +00:00
2021-03-25 11:03:43 +00:00
.. _style_guide_natspec:
2017-05-15 10:45:26 +00:00
***** **
NatSpec
***** **
2021-03-25 11:03:43 +00:00
Solidity contracts can also contain NatSpec comments. They are written with a
triple slash (`` /// `` ) or a double asterisk block (`` /** ... */ `` ) and
they should be used directly above function declarations or statements.
2017-05-15 10:45:26 +00:00
2020-08-06 10:56:17 +00:00
For example, the contract from :ref: `a simple smart contract <simple-smart-contract>` with the comments
2021-06-25 10:25:29 +00:00
added looks like the one below:
.. code-block :: solidity
2017-05-15 10:45:26 +00:00
2020-05-13 15:45:58 +00:00
// SPDX-License-Identifier: GPL-3.0
2020-09-08 08:48:04 +00:00
pragma solidity >=0.4.16 <0.9.0;
2017-05-15 10:45:26 +00:00
/// @author The Solidity Team
/// @title A simple storage example
contract SimpleStorage {
uint storedData;
/// Store `x` .
/// @param x the new value to store
/// @dev stores the number in the state variable `storedData`
function set(uint x) public {
storedData = x;
}
/// Return the stored value.
/// @dev retrieves the value of the state variable `storedData`
/// @return the stored value
function get() public view returns (uint) {
return storedData;
}
}
2020-08-06 10:56:17 +00:00
It is recommended that Solidity contracts are fully annotated using :ref: `NatSpec <natspec>` for all public interfaces (everything in the ABI).
2019-02-27 03:34:33 +00:00
2020-08-06 10:56:17 +00:00
Please see the section about :ref: `NatSpec <natspec>` for a detailed explanation.