It seems ideally both are same. It's like you could require(msg.sender == owner)
instead of using a modifier if only one or two times you use it. Else you could declare a modifier and append it before functions. It just the matter of practice and convenience.
I tried estimating gas of a simple smart contract using both modifier
and require
, results were arrantly same.
Testcase1:
pragma solidity ^0.4.11;
contract A{
uint a =10;
address owner;
function A(){
owner = msg.sender;
}
modifier onlyOwner(){
require(msg.sender == owner);
_;
}
function updateValue(uint a) onlyOwner {
a = a+10;
}
}
Gas Estimates:
{
"Creation": "40366 + 48800\n",
"External": {
"updateValue(uint256)": 454
},
"Internal": {}
}
TestCase2:
Code:
pragma solidity ^0.4.11;
contract A{
uint a =10;
address owner;
function A(){
owner = msg.sender;
}
function updateValue(uint a) {
require(msg.sender == owner);
a = a+10;
}
}
Gas Estimates:
{
"Creation": "40366 + 48800\n",
"External": {
"updateValue(uint256)": 454
},
"Internal": {}
}
Since gas estimates are same in both cases, it seems both are perfectly fine (at least in terms of fee).