32

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;
  }


}
41

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.

  • Ah, thanks for the clarification. This is like when they got rid of throw all over again – thefett Sep 26 '17 at 20:02
0

Agreeing on exactly what @Ismael has said. For other users who may stumble upon this question as I did - As above pure is used as type access for function, we use view as well where we are returning value from function - where we want to access the storage

e.g:

// No change

    function withdraw(uint amount) public{
        value -= amount;
    }

// View keyword is added

    function balance() public view returns (uint){
        return value;
    }    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.