مشخصات مقاله
-
0.0
-
801
-
0
-
0
Style Guide در سالیدیتی
Style Guide به حفظ خوانایی کد کمک می کند. در ادامه بهترین روش های نوشتن Style Guide آورده شده است:
Code Layout
Indentation :
برای حفظ سطح Indentation از 4 اسپیس به جای Tab استفاده نمایید. از میکس شدن اسپیس ها با تب ها خودداری کنید.
Two Blank Lines Rule :
از 2 خط خالی بین دو تعریف قرارداد استفاده نمایید.
pragma solidity ^0.5.0;
contract LedgerBalance {
//...
}
contract Updater {
//...
}
One Blank Line Rule :
از 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 {
// ...
}
}
Maximum Line Length
یک خط نباید از 79 کاراکتر بیشتر باشد تا افرادی که کد را می خوانند به راحتی آن را تجزیه نمایند.
Wrapping rules
اولین استدلال بدون باز کردن پرانتز در خط جدید می باشد. در هر استدلال از Indent منفرد استفاده کنید. (عنصر پایان دهنده باید آخرین باشد.)
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 );
Source Code Encoding :
Imports :
Import باید درست در بالای فایل پس از اعلام Pragma قرار گیرند.
Order of Functions :
فانکشن ها باید از نظر میزان Visibility گروه بندی شوند.
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
// ...
}
Avoid extra whitespaces
از فضای خالی بلافاصله داخل پرانتز، براکت و … خودداری کنید.
Control structures :
Braces باید در همان اعلام کننده باز شوند. روی خط خودشان ببندید و همان تو رفتگی را حفظ کنید. از یک فضای دارای Brace استفاده کنید.
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 Declaration :
از قانون بالا برای Braces استفاده کنید. همیشه Visibility Label را اضافه کنید. Visibility Label باید قبل از هر Modifier باشد.
function kill() public onlyowner {
selfdestruct(owner);
}
Mappings
هنگام اعلام متغیرهای Mapping از ایجاد فضای خالی خودداری نمایید.
mapping(uint => uint) map; mapping(address => bool) registeredAddresses; mapping(uint => mapping(bool => Data[])) public data; mapping(uint => mapping(uint => s)) data;
Variable declaration:
هنگام اعلام متغیرهای آرایه از فضای خالی خودداری کنید.
uint[] x; // not unit [] x;
String declaration :
برای اعلام یک رشته به جای ‘ از ̎ استفاده کنید.
str = "foo"; str = "Hamlet says, 'To be or not to be...'";
ترتیب Layout
عناصر باید به ترتیب زیر قرار گیرند:
- Pragma statements
- Import statements
- Interfaces
- Libraries
- Contracts
در Interfaces، Libraries یا Contracts باید به صورت زیر باشد:
- Type declarations
- State variables
- Events
- Functions
قراردادهای نامگذاری (Naming conventions)
- Contracts و Library باید با استفاده از CapWord Style یا حروف بزرگ نامگذاری شود. به عنوان مثال، Owner، SmartContract و غیره.
- نام Contract و Library باید با نام فایل آنها مطابقت داشته باشد.
- در صورت وجود چندین Contract یا Library در یک فایل، از نام Contract یا Library اصلی استفاده کنید.
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 {
//...
}
-
نام های Struct
از حروف بزرگ CapWords مانند SmartCoin استفاده کنید.
-
نام های Event
از سبک CapWords مانند Deposit‚ AfterTransfer استفاده کنید.
-
نام توابع
از سبک MixedCase مانند InitiateSupply استفاده کنید.
-
متغیرهای Local و State
از سبک MixedCase مانند CreatorAddress‚ Supply استفاده کنید.
-
Constants
برای جدا کردن کلمات بزرگ از _ استفاده کنید مانند MAX_BLOCKS.
-
نام گذاری Modifier
از سبک MixCase مانند OnlyAfter استفاده کنید.
-
نام Enum
از سبک CapWords مانند TokenGroup استفاده کنید.