Many token contracts have unrestricted getter functions for state variables, which seems redundant (eg, see OpenZepplin's ERC20 contract). For example, a contract could have it this way:
uint private _totalSupply;
function totalSupply() view public returns (uint) {
return _totalSupply
}
or this way:
uint public totalSupply;
Web3.js and solidity would use the exact same syntax--totalSupply--to retrieve the variable "totalSupply." As the former is more common, but also more verbose, I presume I am missing some way in which a getter function dominates querying a contract directly. Can someone enlighten me?
totalSupply
variable, whereas in the second case, every inheriting contract can also change the value of thetotalSupply
variable. That is why OZ have moved frompublic
variables on their v1 toprivate
variables on their v2. Off-chain-wise (e.g., web3.js), both paradigms are identical of course. – goodvibration Sep 11 '20 at 21:37