Solidity Remix Warnings – State Mutability and Public Visibility

remixsolidity

I noticed the new Remix gave me two new warnings:

browser/Untitled.sol:6:5: Warning: No visibility specified. Defaulting to "public".
    function setFee(uint _fee){
    ^
Spanning multiple lines.

and

browser/Untitled.sol:15:3: Warning: Function state mutability can be restricted to pure
  function mul(uint256 a, uint256 b) internal constant returns (uint256) {
  ^
Spanning multiple lines.

Should I change anything? Is it best practice to type out visibility?
Here's my simple code:

pragma solidity ^0.4.13;

contract test {

    uint public fee; 
    function setFee(uint _fee){
      fee = Sf.mul(_fee,10);
    }

}


library Sf{

  function mul(uint256 a, uint256 b) internal constant returns (uint256) {
    uint256 c = a * b;
    assert(a == 0 || c / a == b);
    return c;
  }


}

Best Answer

It is a good practice to always declare the type access to your methods. If you forget to declare a function internal previous version will compile happily and will not warn about the potential security issue.

function setFee(uint _fee) public {
    fee = Sf.mul(_fee,10);
}

The constant modifier is being replaced by view and pure. With view you cannot modify the contract storage, but you can access the storage. With pure you cannot access the contract storage. For example contract getters are views, and utility libraries that do not access conctract storage but only its parameters can be declared as pure.

function mul(uint256 a, uint256 b) internal pure returns (uint256) {
    uint256 c = a * b;
    assert(a == 0 || c / a == b);
    return c;
}

See Solidity: What is the difference between `view` and `constant`? for more details.

Related Topic