I wondered why OpenZeppelin relies so much on the use of functions when, at first glance, they seem unnecessary for such elementary operations. For example, take the Ownable.sol contract. Why isn't the modifier strictly
modifier onlyOwner() {
require(_owner == msg.sender, "Ownable: caller is not the owner");
_;
}
instead of
address private _owner;
modifier onlyOwner() {
_checkOwner();
_;
}
function owner() public view virtual returns (address) {
return _owner;
}
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
// This _msgSender() function is taken from Context.sol
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
The way OpenZeppelin has their code written seems to spend more gas, so is security the reason? I get the use of owner()
and _checkOwner()
because owner
is a private variable, but why create functions for such atomic pieces of code like the _msgSender()
?
P.S.: The code I've transcribed are snippets of Ownable.sol and Context.sol.