Search code examples
unit-testingcode-coveragecode-analysis

Should branches of code caused by short-circuiting be considered for code coverage?


The regular branch coverage will require two unit tests to cover a simple if statement. But if there is combound condition like if (A && B), from the control flow graph perspective, there is an additional branch if short-circuiting is used. This is in concert with cyclomatic complexity count, which gives 3 (also applying the rules that each logical operators increases the complexity by 1, because a decision node is created in case of short-circuiting). But as far as I know, the code analyzer do not consider those branches. Is it worth covering them anyway to make sure no side-effects result from partial evaluation of the expression?


Solution

  • It depends on your purpose in analysing the code, naturally.

    FAA generally recommends (for example DOT/FAA/AR-06/54 "Software Verification Tools Assessment Study", Final Report, June 2007. Section 4.2.5) that all operands to a short-circuited operator (including C's ternary operator, as well as the boolean operators) be interpreted as decisions - like you describe. For higher design assurance levels (particularly catastrophic and hazardous), the objectives to be satisfied under relevant standards (DO178, DO254, etc) have an effect of requiring coverage of all possible decisions - with increasing independence at higher DALs.

    So, assuming your application requires higher DAL, the answer would normally be yes. The alternative would be to construct a specific argument to support a claim that such coverage is not needed to meet objectives of your analysis or testing - and convince reviewers to accept that argument. Such an argument might need to be constructed for every instance of short circuiting.