Solidity Require – Multiple vs Single Require in Solidity

contract-developmentrequiresoliditytesting

One of the functions of my smart contract can only be executed if three conditions are met. At the moment I have a single require which combines all the conditions like this:

  require(evalCond1() && evalCond2() && evalCond3());

But when I try to check my code coverage with solidity-coverage it only sees two branches: the one in which the require is met and the other, in which at least one of those conditions evaluates to false (no matter which one). I was wondering if there was any issue with breaking down the single require in three separate ones, one for each condition:

 require(evalCond1());
 require(evalCond2());
 require(evalCond3());

I think this improves readability (since the real function names are a bit more complex than evalCond) and helps showing the correct coverage for branches, but I'm not sure this doesn't have any side effects regarding gas usage and performance.

Best Answer

Comparing the bytecode generated by each approach, the second is 6 bytes longer - basically because the REVERT opcode and associate memory stuff appears 3 times instead of once. But since these instructions are not executed (unless something goes wrong!) this is irrelevant. Both approaches result in the same number of JUMPIs

In short, the gas usage will be near enough the same with either approach within half a dozen gas, so take your pick.

Related Topic