Avoid variable assignments in conditional expressions

Why you should care

This pattern verifies the presence of variable assignments in conditional (IF) expressions. It is generally not recommended to use the assignment operator in this case as it is close to the comparison operator (==) and could lead to misreading the code logic.
5362

How we detect

CAST Highlight counts one occurrence each time the assignment operator (=) is detected within a conditional expression, whether it is IF, EACH or WHILE.

 

if(foo = "1234") { ... // +1

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.