Clarification on override Keyword Usage in ERC20 Functions (name, symbol, decimals) (original) (raw)
December 3, 2024, 3:41pm 1
I have encountered a potential concern regarding the use of the override keyword in the name
, symbol
, and decimals
functions of the ERC20
contract. According to best practices, the override
keyword should be used to ensure proper method overriding and prevent subtle bugs. However, I have noticed that these functions work both with and without the override keyword.
I would like clarification on whether the absence of the override
keyword in these functions could lead to any potential bugs or unexpected behavior during smart contract development. Below are the relevant code snippets for comparison:
function name() public view virtual returns (string memory) { ... }
// vs
function name() public view virtual override returns (string memory) { ... }
function symbol() public view virtual returns (string memory) { ... }
// vs
function symbol() public view virtual override returns (string memory) { ... }
function decimals() public view virtual returns (uint8) { ... }
// vs
function decimals() public view virtual override returns (uint8) { ... }
Environment:
I am using OpenZeppelin Contracts in my project.
I appreciate your insights and assistance on this matter.
Hey @armgit5, thanks for sharing your question.
Indeed, the ERC20
contract doesn't use the override
keyword because no security concerns arise from not using it in this case (when you inherit from an interface and implement its functions). Although you can specify it, we considered it an unnecessary overhead.
On the opposite, Solidity will require you to specify an override(..., ...)
if you're using our ERC20 and another extension that implements a function twice. In that case, the override becomes explicit but the order of execution is defined by Solidity's linearization
Hope this helps
armgit5 December 5, 2024, 9:21am 3
Hi @ernestognw, thank you for the clear explanation about override
in multiple inheritance! It makes perfect sense that specifying override
is optional in single inheritance, as it avoids unnecessary overhead. Your clarification on Solidityโs linearization determining the execution order is extremely helpful. I truly appreciate your insights so much