Binary expressions
ID | CC-15 |
Title | Binary expressions should not make an assumption on the evaluation order of their operands. |
Priority | Strongly recommended |
Severity level | 5 |
Description | Binary expressions should not make an assumption on the evaluation order of their operands. Generated code does not enforce the order of evaluation in expressions. It is therefore advised to define variables to enforce a specific evaluation order. For more info, see The Mathworks. |
Rationale | Robustness |
avoid
if (aFcnCall(z) || anotherFcnCall(z))
instead use
First compute the intermediate results in desired order:
isApprovedForA = aFcnCall(z);
isApprovedForB = anotherFcnCall(z);
if (isApprovedForA || isApprovedForB)
or in case you really need the conditional execution:
isApproved = aFcnCall(z);
if (~isApproved)
% not yet approved
isApproved = anotherFcnCall(z);
else
% allready approved
end
if (isApproved)