每一门语言都有其相应的编码规范, Solidity 也一样, 下面官方推荐的规范及我的总结,供大家参考,希望可以帮助大家写出更好规范的智能合约。
命名规范
避免使用
小写的l,大写的I,大写的O 应该避免在命名中单独出现,因为很容易产生混淆。
合约、库、事件、枚举及结构体命名
合约、库、事件及结构体命名应该使用单词首字母大写的方式,这个方式也称为:帕斯卡命名法或大驼峰式命名法,比如:SimpleToken, SmartBank, CertificateHashRepository,Player。
函数、参数、变量及修饰器
函数、参数、变量及修饰器应该使用首单词小写后面单词大写的方式,这个方式也称为:(小)驼峰式命名法,是一种混合大小写的方式,如:
- 函数名应该如:getBalance,transfer,verifyOwner,addMember。
- 参数和变量应该如:initialSupply,senderAddress,account,isPreSale。
- 修饰器应该如:onlyAfter,onlyOwner。
代码格式相关
缩进
使用空格(spaces)而不是Tab, 缩进应该是4个空格
空行
合约之间应该有空行,例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
| contract A {
...
}
contract B {
...
}
contract C {
...
}
|
而不是使用:
1
2
3
4
5
6
7
8
9
10
| contract A {
...
}
contract B {
...
}
contract C {
...
}
|
1
2
3
4
5
6
7
8
9
| contract A {
function spam() public {
...
}
function ham() public {
...
}
}
|
没有实现的话,空行可以省去,如:
1
2
3
4
| contract A {
function spam() public;
function ham() public;
}
|
而不是:
1
2
3
4
5
6
7
8
| contract A {
function spam() public {
...
}
function ham() public {
...
}
}
|
左括号应该跟定义在一行
定义包括合约定义、函数定义、库定义、结构体定义等等,例如推荐使用:
1
2
3
4
5
6
| contract Coin {
struct Bank {
address owner;
uint balance;
}
}
|
而不是:
1
2
3
4
5
6
7
| contract Coin
{
struct Bank {
address owner;
uint balance;
}
}
|
左括号应该跟条件控制在一行
在使用if, else, while, for 时,推荐的写法是:
1
2
3
4
5
6
7
| if (...) {
...
}
for (...) {
...
}
|
而不是:
1
2
3
4
5
6
7
8
9
10
| if (...)
{
...
}
while(...){
}
for (...) {
...;}
|
如果控制语句内只有一行,括号可省略,如:
但像下面一个语句有多方就不能省略,如:
1
2
3
4
5
| if (x < 10)
someArray.push(Coin({
name: 'spam',
value: 42
}));
|
表达式内的空格
而不是:
1
spam( ham[ 1 ], Coin( { name: "ham" } ) );
有一种例外是,结尾的括号跟在结束的分号后面, 应该加一个空格,如下面的方式也是推荐的:
1
function singleLine() public { spam(); }
-
分号;前不应该有空格,例如推荐使用:
1
| function spam(uint i, Coin coin) public;
|
而不是:
1
function spam(uint i , Coin coin) public ;
-
不要为对齐添加不必要的空格,例如推荐使用:
1
2
3
| x = 1;
y = 2;
long_variable = 3;
|
而不是:
1
2
3
x = 1;
y = 2;
long_variable = 3;
-
回退函数不应该有空格,例如推荐使用:
1
2
3
4
5
6
7
8
9
10
11
12
| function() public {
...
}
```
而不是:
```js
function () public {
...
}
|
控制每一行长度
每行不应该太长,最好在79(或99)个字符以内,函数的参数应该是单独的行,且只有一个缩进,例如推荐的方式是:
1
2
3
4
5
| thisFunctionCallIsReallyLong(
longArgument1,
longArgument2,
longArgument3
);
|
而不是:
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
| thisFunctionCallIsReallyLong(longArgument1,
longArgument2,
longArgument3
);
thisFunctionCallIsReallyLong(longArgument1,
longArgument2,
longArgument3
);
thisFunctionCallIsReallyLong(
longArgument1, longArgument2,
longArgument3
);
thisFunctionCallIsReallyLong(
longArgument1,
longArgument2,
longArgument3
);
thisFunctionCallIsReallyLong(
longArgument1,
longArgument2,
longArgument3);
|
对应的赋值语句应该是这样写:
1
2
3
4
5
6
| thisIsALongNestedMapping[being][set][to_some_value] = someFunction(
argument1,
argument2,
argument3,
argument4
);
|
而不是:
1
2
3
4
| thisIsALongNestedMapping[being][set][to_some_value] = someFunction(argument1,
argument2,
argument3,
argument4);
|
事件定义也应该遵循同样的原则,例如应该使用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| event LongAndLotsOfArgs(
adress sender,
adress recipient,
uint256 publicKey,
uint256 amount,
bytes32[] options
);
LongAndLotsOfArgs(
sender,
recipient,
publicKey,
amount,
options
);
|
而不是:
1
2
3
4
5
6
7
8
9
10
11
| event LongAndLotsOfArgs(adress sender,
adress recipient,
uint256 publicKey,
uint256 amount,
bytes32[] options);
LongAndLotsOfArgs(sender,
recipient,
publicKey,
amount,
options);
|
文件编码格式
推荐使用utf-8 及 ASCII 编码
引入文件应该在最上方
建议使用:
1
2
3
4
5
6
7
8
9
10
11
| import "owned";
contract A {
...
}
contract B is owned {
...
}
|
而不是:
1
2
3
4
5
6
7
8
9
10
11
| contract A {
...
}
import "owned";
contract B is owned {
...
}
|
函数编写规范
函数的顺序
在编写函数的时候,应该让大家容易找到构造函数,回退函数,官方推荐的的函数顺序是:
- 构造函数
- 回退函数 (如果有)
- 外部函数(external)
- 公有函数(public)
- 内部函数(internal)
- 私有函数(private)
同一类函数时,constant函数放在后面, 例如推荐方式为:
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
| contract A {
// 构造函数
function A() public {
...
}
// 回退函数
function() public {
...
}
// 外部函数
// ...
// 带有constant 外部函数
// ...
// 公有函数
// ...
// 内部函数
// ...
// 私有函数
// ...
}
|
而不是下面的函数顺序:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| contract A {
// 外部函数
// ...
// 公有函数
// ...
// 内部函数
// ...
function A() public {
...
}
function() public {
...
}
// 私有函数
// ...
}
|
明确函数的可见性
所有的函数(包括构造函数)应该在定义的时候明确函数的可见性,例如应该使用:
1
2
3
| function explicitlyPublic(uint val) public {
doSomething();
}
|
而不是
1
2
3
| function implicitlyPublic(uint val) {
doSomething();
}
|
可见性应该在修饰符前面
函数的可见性应该写在自定义的函数修饰符前面,例如:
1
2
3
| function kill() public onlyowner {
selfdestruct(owner);
}
|
而不是
1
2
3
| function kill() onlyowner public {
selfdestruct(owner);
}
|
区分函数和事件
为了防止函数和事件(Event)产生混淆,声明一个事件使用大写并加入前缀(可使用LOG)。对于函数, 始终以小写字母开头,构造函数除外。
1
2
3
4
5
6
7
| // 不建议
event Transfer() {}
function transfer() {}
// 建议
event LogTransfer() {}
function transfer() external {}
|
常量
常量应该使用全大写及下划线分割大词的方式,如:MAX_BLOCKS,TOKEN_NAME, CONTRACT_VERSION。
参考文献
Solidity style-guide