前言
這是Solidity智能合約語(yǔ)言的最后一講了,這一節(jié)主要說(shuō)說(shuō)Solidity編碼規(guī)范的問(wèn)題面哥。
代碼格式相關(guān)
縮進(jìn)
使用使用空格(spaces)而不是Tab亚斋,用4個(gè)空格來(lái)表示縮進(jìn)。避免混合使用Tab和空格
空白行(Blank Lines)
合約之間應(yīng)該有2行空格
規(guī)范的寫(xiě)法
contract A {
...
}
contract B {
...
}
contract C {
...
}
不規(guī)范的寫(xiě)法:
contract A {
...
}
contract B {
...
}
contract C {
...
}
在一個(gè)合約的2個(gè)函數(shù)之間應(yīng)該有1個(gè)空行饱溢。例如:
contract A {
function spam() public {
...
}
function ham() public {
...
}
}
沒(méi)有實(shí)現(xiàn)的話,空行可以省去.
規(guī)范的寫(xiě)法:
contract A {
function spam() public;
function ham() public;
}
contract B is A {
function spam() public {
...
}
function ham() public {
...
}
}
不規(guī)范的寫(xiě)法:
contract A {
function spam() public {
...
}
function ham() public {
...
}
}
控制每一行的最大長(zhǎng)度(Maximum Line Length)
控制每一行在79(或者99)個(gè)字符以內(nèi)走芋,方便讀者解讀代碼
1绩郎、第一個(gè)參數(shù)不應(yīng)該附加在開(kāi)頭的括號(hào)上
2、應(yīng)該只使用一個(gè)縮進(jìn)
3翁逞、每個(gè)參數(shù)應(yīng)該在單獨(dú)的一行
4肋杖、這個(gè)標(biāo)識(shí)符 );應(yīng)該放在最后一行
函數(shù)調(diào)用
推薦的方式
thisFunctionCallIsReallyLong(
longArgument1,
longArgument2,
longArgument3
);
不推薦的方式:
thisFunctionCallIsReallyLong(longArgument1,
longArgument2,
longArgument3
);
thisFunctionCallIsReallyLong(longArgument1,
longArgument2,
longArgument3
);
thisFunctionCallIsReallyLong(
longArgument1, longArgument2,
longArgument3
);
thisFunctionCallIsReallyLong(
longArgument1,
longArgument2,
longArgument3
);
thisFunctionCallIsReallyLong(
longArgument1,
longArgument2,
longArgument3);
對(duì)應(yīng)的賦值語(yǔ)句
thisIsALongNestedMapping[being][set][to_some_value] = someFunction(
argument1,
argument2,
argument3,
argument4
);
不規(guī)范的寫(xiě)法:
thisIsALongNestedMapping[being][set][to_some_value] = someFunction(argument1,
argument2,
argument3,
argument4);
事件定義(Event Definitions and Event Emitters)
規(guī)范寫(xiě)法:
event LongAndLotsOfArgs(
adress sender,
adress recipient,
uint256 publicKey,
uint256 amount,
bytes32[] options
);
LongAndLotsOfArgs(
sender,
recipient,
publicKey,
amount,
options
);
不規(guī)范寫(xiě)法:
event LongAndLotsOfArgs(adress sender,
adress recipient,
uint256 publicKey,
uint256 amount,
bytes32[] options);
LongAndLotsOfArgs(sender,
recipient,
publicKey,
amount,
options);
源文件編碼
推薦使用UTF-8 或者 ASCII編碼
Imports
Import語(yǔ)句應(yīng)該在文件最上方
推薦使用:
import "owned";
contract A {
...
}
contract B is owned {
...
}
不推薦使用:
contract A {
...
}
import "owned";
contract B is owned {
...
}
函數(shù)規(guī)范(Order of Functions)
函數(shù)順序
函數(shù)排序能夠幫助讀者識(shí)別他們可以調(diào)用哪些函數(shù),并且更容易找到構(gòu)造函數(shù)挖函,回退函數(shù)
函數(shù)應(yīng)該根據(jù)其可見(jiàn)行和順序來(lái)分組(官方推薦的的函數(shù)順序是)
1状植、構(gòu)造函數(shù)
2、回退函數(shù) (如果有)
3怨喘、外部函數(shù)(external)
4津畸、公有函數(shù)(public)
5、內(nèi)部函數(shù)(internal)
6必怜、私有函數(shù)(private)
同一類函數(shù)肉拓,constant函數(shù)放在后面。
推薦的方式:
contract A {
// 構(gòu)造函數(shù)
function A() public {
...
}
// 回退函數(shù)
function() public {
...
}
// 外部函數(shù)
// ...
// 帶有constant 外部函數(shù)
// ...
// 公有函數(shù)
// ...
// 內(nèi)部函數(shù)
// ...
// 私有函數(shù)
// ...
}
不推薦的格式:
contract A {
// 外部函數(shù)
// ...
// 公有函數(shù)
// ...
// 內(nèi)部函數(shù)
// ...
function A() public {
...
}
function() public {
...
}
// 私有函數(shù)
// ...
}
表達(dá)式中的空格
在以下情形梳庆,避免使用沒(méi)有必要的空格
一個(gè)單行的表達(dá)里暖途,在小括號(hào)、中括號(hào)靠益、大括號(hào)里應(yīng)該避免不必要的空格
推薦:
spam(ham[1], Coin({name: "ham"}));
不推薦:
spam( ham[ 1 ], Coin( { name: "ham" } ) );
下面這種是一個(gè)例外丧肴,結(jié)尾的括號(hào)跟在結(jié)束的分號(hào)后面, 應(yīng)該加一個(gè)空格胧后,推薦使用這種方式:
function singleLine() public { spam(); }
在逗號(hào)芋浮、分號(hào)之前不應(yīng)有空格
推薦:
function spam(uint i, Coin coin) public;
不推薦:
function spam(uint i , Coin coin) public ;
在賦值的時(shí)候,沒(méi)有必要為了對(duì)齊而添加不必要的空格
推薦:
x = 1;
y = 2;
long_variable = 3;
不推薦:
x = 1;
y = 2;
long_variable = 3;
在回退函數(shù)不要包含空格
推薦:
function() public {
...
}
不推薦:
function () public {
...
}
控制結(jié)構(gòu)(Control Structures)
表示合約定義、函數(shù)定義纸巷、庫(kù)定義镇草、結(jié)構(gòu)體定義的大括號(hào),推薦使用方式:
左括號(hào)應(yīng)該跟定義在一行
contract Coin {
struct Bank {
address owner;
uint balance;
}
}
不推薦:
contract Coin
{
struct Bank {
address owner;
uint balance;
}
}
控制語(yǔ)句if, else, while, for的左括號(hào)也應(yīng)該跟條件控制在一行
推薦:
if (...) {
...
}
for (...) {
...
}
不推薦:
if (...)
{
...
}
while(...){
}
for (...) {
...;}
如果控制結(jié)構(gòu)的方法體內(nèi)的語(yǔ)句只有1行瘤旨,那么大括號(hào)是可以省略的梯啤。
推薦:
if (x < 10)
x += 1;
不推薦:
if (x < 10)
someArray.push(Coin({
name: 'spam',
value: 42
}));
明確函數(shù)的可見(jiàn)性
所有的函數(shù)(包括構(gòu)造函數(shù))應(yīng)該在定義的時(shí)候明確函數(shù)的可見(jiàn)性,例如應(yīng)該使用:
function explicitlyPublic(uint val) public {
doSomething();
}
不推薦:
function implicitlyPublic(uint val) {
doSomething();
}
對(duì)于包含有else 或者 else if子句的if語(yǔ)句存哲, else 或者 else if應(yīng)該放置在if結(jié)束的那一行
推薦:
if (x < 3) {
x += 1;
} else if (x > 7) {
x -= 1;
} else {
x = 5;
}
if (x < 3)
x += 1;
else
x -= 1;
不推薦:
if (x < 3) {
x += 1;
}
else {
x -= 1;
}
可見(jiàn)性應(yīng)該在修飾符前面
函數(shù)的可見(jiàn)性應(yīng)該寫(xiě)在自定義的函數(shù)修飾符前面
推薦方式:
function kill() public onlyowner {
selfdestruct(owner);
}
不推薦方式:
function kill() onlyowner public {
selfdestruct(owner);
}
命名規(guī)范
在命名中要避免單獨(dú)的使用小寫(xiě)的l因宇,大寫(xiě)的I,大寫(xiě)的O祟偷。因?yàn)檫@樣容易產(chǎn)生混淆察滑,容易和數(shù)字0、1不可區(qū)分修肠。
合約贺辰、庫(kù)(Library)、結(jié)構(gòu)體嵌施、事件饲化、枚舉的命名
合約、庫(kù)吗伤、結(jié)構(gòu)體吃靠、事件的命名應(yīng)該使用大駝峰(首字母大寫(xiě)的方式)命名法。例如:SimpleToken, SmartBank, CertificateHashRepository, Player
函數(shù)牲芋、函數(shù)參數(shù)撩笆、局部變量捺球、狀態(tài)變量缸浦、修飾器(Modifier)的命名
除了構(gòu)造函數(shù)以外的函數(shù)、函數(shù)參數(shù)氮兵、局部變量裂逐、狀態(tài)變量、修飾器(Modifier)都應(yīng)該使用小駝峰(mixedCase)命名法泣栈。比如:getBalance, transfer, verifyOwner, addMember, changeOwner
常量
常量應(yīng)該使用全大寫(xiě)及下劃線分割大詞的方式卜高,如:MAX_BLOCKS,TOKEN_NAME南片, CONTRACT_VERSION掺涛。
區(qū)分函數(shù)和事件
為了防止函數(shù)和事件(Event)產(chǎn)生混淆,聲明一個(gè)事件使用大寫(xiě)并加入前綴(可使用LOG)疼进。對(duì)于函數(shù)薪缆, 始終以小寫(xiě)字母開(kāi)頭,構(gòu)造函數(shù)除外伞广。
// 不建議
event Transfer() {}
function transfer() {}
// 建議
event LogTransfer() {}
function transfer() external {}
參考:
https://solidity.readthedocs.io/en/v0.4.23/style-guide.html