Expensive logical operation
A logical operation requires the evaluation of both operands because of their order, resulting in inefficient code
Since R2021a
Description
This defect occurs when all of these conditions are true:
Left and right operands have no side effects.
The right operand does not contain any calls to
const
member functions.The left operand contains one or more calls to
const
member functions.
When assessing possible side effects of an operand:
Polyspace® assumes that
const
member functions of a class do not have side effects. Nonmember functions are assumed to have side effects.Polyspace treats floating-point operations in accordance to the C++ standard. In C++03 or earlier, floating-point operations have no side effects. In C++11 or later, floating-point operations might have side effects, such as modifying the floating-point status flags to indicate abnormal results or auxiliary information. See Floating-point environment.
Polyspace treats the
bool
conversion operator and logicalNOT
operators of astruct
or a class as built-in operators. These operations are not treated as member function calls. The standard template library contains many classes that define such abool
conversion operator or a logicalNOT
operator.
Risk
When evaluating logical operation, the compiler evaluates the left argument first, and then evaluates the right argument only when necessary. In a logical operation, it is inefficient to put function calls as the left argument while putting constant and variables as the right argument. Consider this code:
if(Object.attribute()|| var1){ //... }
if
statement, the compiler always evaluates the
function call Object.attribute()
. Evaluating the function is not always
necessary. For instance, if var1
evaluates to true
,
then the logical expression always evaluates to true. Because var1
is the
right operand, not the left operand, the compiler unnecessarily evaluates a function call,
which is inefficient. Because the inefficient code compiles and behaves correctly, this
defect might go unnoticed.Fix
To fix this defect, flip the order of the operands in a logical expression if the left operand does not perform an operation that must be performed before the right operand in order to evaluate the right operand safely and correctly.
If this condition is not true, then the code relies on the exact order in which the compiler evaluates the flagged logical expression. The best practice is not to rely on the evaluation order of an expression. Consider refactoring your code so that the order of evaluation has no impact. If refactoring the code is not possible, justify the defect by using annotation or review information. See:
Address Results in Polyspace User Interface Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
Performance improvements might vary based on the compiler, library implementation, and environment that you are using.
Examples
Result Information
Group: Performance |
Language: C++ |
Default: Off |
Command-Line Syntax:
EXPENSIVE_LOGICAL_OPERATION |
Impact: Low |
Version History
Introduced in R2021a
See Also
Find defects
(-checkers)
| CERT C: Rule EXP30-C
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)
- List of Classes in STL with
bool
Conversion Operator