The use of increment and decrement operators in method calls or in combination with other arithmetic operators is not recommended, because:
- It can significantly impair the readability of the code.
- It introduces additional side effects into a statement, with the potential for undefined behavior.
- It is safer to use these operators in isolation from any other arithmetic operators.
Noncompliant Code Example
u8a = ++u8b + u8c--;
foo = bar++ / 4;
The following sequence is clearer and therefore safer:
u8a = u8b + u8c;
foo = bar / 4;
- MISRA C:2004, 12.1 - Limited dependence should be placed on the C operator precedence rules in expressions.
- MISRA C:2004, 12.13 - The increment (++) and decrement (--) operators should not be mixed with other operators in an expression.
- MISRA C++:2008, 5-2-10 - The increment (++) and decrement (--) operator should not be mixed with other operators in an expression.
- MISRA C:2012, 12.1 - The precedence of operators within expressions should be made explicit
- MISRA C:2012, 13.3 - A full expression containing an increment (++) or decrement (--) operator should have no other potential side effects
other than that cause by the increment or decrement operator
- CERT, EXP30-C. - Do not depend on the order of evaluation for side effects
- CERT, EXP50-CPP. - Do not depend on the order of evaluation for side effects
- CERT, EXP05-J. - Do not follow a write by a subsequent write or read of the same
object within an expression