0
mapping(address => uint) private _balances;

function balance() public view returns (uint) {
    return _balances[msg.sender];
}

My question is. Should you/Why/When should you use private _balances, versus just using public and not having to write a getter for the function

mapping(address => uint) public balances;

What's the main difference?

1 Answer 1

1

The only reason to write your own getter and use private for the mapping is if you want to combine the mapping somehow with some other state of the contract (add some additional value to the getter).

In writing your own getter function, you are doing the same action that solidity is going to do when it compiles state variables and functions into byte code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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