solidity – Default Getter in Solidity: Understanding Its Underutilization

contract-designerc-20solidity

So I discovered that solidity is generating automatic getter for public property.
I was wondering why is not used more often, especially in ERC20 Contract.
For example the default ERC 20 contract is using a specified getter for the decimals.

    string private _name;
    string private _symbol;


    function name() public view virtual override returns (string memory) {
        return _name;
    }

    /**
     * @dev Returns the symbol of the token, usually a shorter version of the
     * name.
     */
    function symbol() public view virtual override returns (string memory) {
        return _symbol;
    }

Why are they not using?

string public name
string public symbol

Am I missing something? Is it for security reasons? What are the advantages?

Many thanks in advance

Best Answer

As you can see by the keyword virtual these getters can be overridden in the derived contracts. Someone who might want to change the way those getters behave could use this property to do just that.

Another thing for me is just simplifying naming. By convention underscore before the name of the variable notes that it is a global variable (state variable in Solidity), and the ones with the underscore after the name are local (memory variables). Getter does not really need that distinction and simplification may take place.

Related Topic