📅  最后修改于: 2020-11-04 04:25:25             🧑  作者: Mango
样式指南有助于保持代码布局的一致性,并使代码更具可读性。以下是与Solidity签订合同时遵循的最佳实践。
pragma solidity ^0.5.0;
contract LedgerBalance {
//...
}
contract Updater {
//...
}
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 {
// ...
}
}
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
);
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 solidity ^0.5.0;
// Owned.sol
contract Owned {
address public owner;
constructor() public {
owner = msg.sender;
}
modifier onlyOwner {
//....
}
function transferOwnership(address newOwner) public onlyOwner {
//...
}
}
国会
pragma solidity ^0.5.0;
// Congress.sol
import "./Owned.sol";
contract Congress is Owned, TokenRecipient {
//...
}
−使用CapWords样式,如SmartCoin。
−使用CapWords样式,例如存款,AfterTransfer。
−使用“ mixedCase样式”,如“ initiateSupply”。
−使用mixedCase样式,例如creatorAddress,供应。
−使用所有带有下划线的大写字母分隔单词,例如MAX_BLOCKS。
−像onlyAfter一样使用mixCase Style。
−使用CapWords样式,例如TokenGroup。