Solhint

Solhint

Community Analyzer

Check order of elements in file and inside each contract, according to the style guide SOLHINT-W1034

Style
Minor

Check order of elements in file and inside each contract, according to the style guide.

Bad Practice

  1. State variable declaration after function

    contract MyContract {
      function foo() public {}
    
      uint a;
    }
    
  2. Library after contract

    contract MyContract {}
    
    library MyLibrary {}
    
  3. Interface after library

    library MyLibrary {}
    
    interface MyInterface {}
    
  4. Use for after state variable

contract MyContract {
  uint public x;

  using MyMathLib for uint;
}
  1. External pure before external view
contract MyContract {
  function myExternalFunction() external {}
  function myExternalPureFunction() external pure {}
  function myExternalViewFunction() external view {}
}
  1. Public pure before public view
contract MyContract {
  function myPublicFunction() public {}
  function myPublicPureFunction() public pure {}
  function myPublicViewFunction() public view {}
}
  1. Internal pure before internal view
contract MyContract {
  function myInternalFunction() internal {}
  function myInternalPureFunction() internal pure {}
  function myInternalViewFunction() internal view {}
}
  1. Private pure before private view
contract MyContract {
  function myPrivateFunction() private {}
  function myPrivatePureFunction() private pure {}
  function myPrivateViewFunction() private view {}
}

Recommended

  1. All units are in order - ^0.4.0
pragma solidity ^0.4.0;

import "./some/library.sol";
import "./some/other-library.sol";

enum MyEnum {
  Foo,
  Bar
}

struct MyStruct {
  uint x;
  uint y;
}

interface IBox {
  function getValue() public;
  function setValue(uint) public;
}

library MyLibrary {
  function add(uint a, uint b, uint c) public returns (uint) {
    return a + b + c;
  }
}

contract MyContract {
  struct InnerStruct {
    bool flag;
  }

  enum InnerEnum {
    A, B, C
  }

  uint public x;
  uint public y;

  event MyEvent(address a);

  constructor () public {}

  fallback () external {}

  function myExternalFunction() external {}
  function myExternalConstantFunction() external constant {}

  function myPublicFunction() public {}
  function myPublicConstantFunction() public constant {}

  function myInternalFunction() internal {}
  function myPrivateFunction() private {}
}
  1. All units are in order - ^0.5.0
pragma solidity ^0.5.0;

import "./some/library.sol";
import "./some/other-library.sol";

enum MyEnum {
  Foo,
  Bar
}

struct MyStruct {
  uint x;
  uint y;
}

interface IBox {
  function getValue() public;
  function setValue(uint) public;
}

library MyLibrary {
  function add(uint a, uint b, uint c) public returns (uint) {
    return a + b + c;
  }
}

contract MyContract {
  using MyLibrary for uint;

  struct InnerStruct {
    bool flag;
  }

  enum InnerEnum {
    A, B, C
  }

  address payable owner;
  uint public x;
  uint public y;

  event MyEvent(address a);

  modifier onlyOwner {
    require(
      msg.sender == owner,
      "Only owner can call this function."
    );
    _;
  }

  constructor () public {}

  fallback () external {}

  function myExternalFunction() external {}
  function myExternalViewFunction() external view {}
  function myExternalPureFunction() external pure {}

  function myPublicFunction() public {}
  function myPublicViewFunction() public view {}
  function myPublicPureFunction() public pure {}

  function myInternalFunction() internal {}
  function myInternalViewFunction() internal view {}
  function myInternalPureFunction() internal pure {}

  function myPrivateFunction() private {}
  function myPrivateViewFunction() private view {}
  function myPrivatePureFunction() private pure {}
}
  1. All units are in order - ^0.6.0
pragma solidity ^0.6.0;

import "./some/library.sol";
import "./some/other-library.sol";

enum MyEnum {
  Foo,
  Bar
}

struct MyStruct {
  uint x;
  uint y;
}

interface IBox {
  function getValue() public;
  function setValue(uint) public;
}

library MyLibrary {
  function add(uint a, uint b, uint c) public returns (uint) {
    return a + b + c;
  }
}

contract MyContract {
  using MyLibrary for uint;

  struct InnerStruct {
    bool flag;
  }

  enum InnerEnum {
    A, B, C
  }

  address payable owner;
  uint public x;
  uint public y;

  event MyEvent(address a);

  modifier onlyOwner {
    require(
      msg.sender == owner,
      "Only owner can call this function."
    );
    _;
  }

  constructor () public {}

  receive() external payable {}

  fallback () external {}

  function myExternalFunction() external {}
  function myExternalViewFunction() external view {}
  function myExternalPureFunction() external pure {}

  function myPublicFunction() public {}
  function myPublicViewFunction() public view {}
  function myPublicPureFunction() public pure {}

  function myInternalFunction() internal {}
  function myInternalViewFunction() internal view {}
  function myInternalPureFunction() internal pure {}

  function myPrivateFunction() private {}
  function myPrivateViewFunction() private view {}
  function myPrivatePureFunction() private pure {}
}

Learn more

ordering on Solhint's documentation.