Increment (++) and decrement (–) operators should not be used in a method call or mixed with other operators in an expression

Why you should care

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.

How we detect

CAST Highlight counts one occurrence each time an operator ++ or – – is not the only one in the statement or an operator ++ or – – is used as a parameter in function call.

Bad Code

u8a = ++u8b + u8c--
foo = bar++ / 4
myMethodCall(foo++)
myMethodCall(bar--)
myMethodCall(++foo)
myMethodCall(--bar)

Good Code

++u8b
u8a = u8b + u8c
u8c--
foo = bar / 4
bar++

References

5362

About CAST and Highlight’s Code Insights

Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.