Solidity - 風格指南



風格指南有助於保持程式碼佈局的一致性並使程式碼更易於閱讀。以下是使用 Solidity 編寫合約時應遵循的最佳實踐。

程式碼佈局

  • 縮排 − 使用 4 個空格代替製表符來維護縮排級別。避免混合使用空格和製表符。

  • 兩行空行規則 − 在兩個合約定義之間使用 2 行空行。

pragma solidity ^0.5.0;

contract LedgerBalance {
   //...
}
contract Updater {
   //...
}
  • 一行空行規則 − 在兩個函式之間使用 1 行空行。如果只有宣告,則無需空行。

pragma solidity ^0.5.0;

contract A {
   function balance() public pure;
   function account() public pure;
}
contract B is A {
   function balance() public pure {
      // ...
   }
   function account() public pure {
      // ...
   }
}
  • 最大行長 − 單行不應超過 79 個字元,以便讀者可以輕鬆解析程式碼。

  • 換行規則 − 第一個引數應在新行中,不帶開括號。每個引數使用單個縮排。結束元素 ); 應為最後一個。

function_with_a_long_name(
   longArgument1,
   longArgument2,
   longArgument3
);
variable = function_with_a_long_name(
   longArgument1,
   longArgument2,
   longArgument3
);
event multipleArguments(
   address sender,
   address recipient,
   uint256 publicKey,
   uint256 amount,
   bytes32[] options
);
MultipleArguments(
   sender,
   recipient,
   publicKey,
   amount,
   options
);
  • 原始碼編碼 − 最好使用 UTF-8 或 ASCII 編碼。

  • 匯入 − 匯入語句應放置在檔案頂部,緊跟在 pragma 宣告之後。

  • 函式順序 − 函式應根據其可見性進行分組。

pragma solidity ^0.5.0;

contract A {
   constructor() public {
      // ...
   }
   function() external {
      // ...
   }

   // External functions
   // ...

   // External view functions
   // ...

   // External pure functions 
   // ...

   // Public functions
   // ...

   // Internal functions
   // ...

   // Private functions
   // ...
}
  • 避免額外的空格 − 避免在括號、方括號或花括號內立即使用空格。

  • 控制結構 − 花括號應與宣告位於同一行。在自己的行上關閉,並保持相同的縮排。在開花括號前使用空格。

pragma solidity ^0.5.0;

contract Coin {
   struct Bank {
      address owner;
      uint balance;
   }
}
if (x < 3) {
   x += 1;
} else if (x > 7) {
   x -= 1;
} else {
   x = 5;
}
if (x < 3)
   x += 1;
else
   x -= 1;
  • 函式宣告 − 對花括號使用上述規則。始終新增可見性標籤。可見性標籤應在任何自定義修飾符之前出現。

function kill() public onlyowner {
   selfdestruct(owner);
}
  • 對映 − 宣告對映變數時避免使用空格。

mapping(uint => uint) map;
mapping(address => bool) registeredAddresses;
mapping(uint => mapping(bool => Data[])) public data;
mapping(uint => mapping(uint => s)) data;
  • 變數宣告 − 宣告陣列變數時避免使用空格。

uint[] x;  // not unit [] x;
  • 字串宣告 − 使用雙引號而不是單引號來宣告字串。

str = "foo";
str = "Hamlet says, 'To be or not to be...'";

佈局順序

元素應按以下順序佈局。

  • Pragma 語句

  • 匯入語句

  • 介面

  • 合約

在介面、庫或合約內,順序應為 -

  • 型別宣告

  • 狀態變數

  • 事件

  • 函式

命名約定

  • 合約和庫應使用 CapWords 樣式命名。例如,SmartContract、Owner 等。

  • 合約和庫名稱應與其檔名匹配。

  • 如果在一個檔案中有多個合約/庫,則使用核心合約/庫的名稱。

Owned.sol

pragma solidity ^0.5.0;

// Owned.sol
contract Owned {
   address public owner;
   constructor() public {
      owner = msg.sender;
   }
   modifier onlyOwner {
      //....
   }
   function transferOwnership(address newOwner) public onlyOwner {
      //...
   }
}

Congress.sol

pragma solidity ^0.5.0;

// Congress.sol
import "./Owned.sol";

contract Congress is Owned, TokenRecipient {
   //...
}
  • 結構體名稱

    − 使用 CapWords 樣式,例如 SmartCoin。

  • 事件名稱

    − 使用 CapWords 樣式,例如 Deposit、AfterTransfer。

  • 函式名稱

    − 使用 mixedCase 樣式,例如 initiateSupply。

  • 區域性變數和狀態變數

    − 使用 mixedCase 樣式,例如 creatorAddress、supply。

  • 常量

    − 使用全部大寫字母,並使用下劃線分隔單詞,例如 MAX_BLOCKS。

  • 修飾符名稱

    − 使用 mixCase 樣式,例如 onlyAfter。

  • 列舉名稱

    − 使用 CapWords 樣式,例如 TokenGroup。

廣告

© . All rights reserved.