Why you should care
The logical OR operator (||) will not work in a switch case as one might think, only the first argument will be considered at execution time.
How we detect
CAST Highlight counts one occurrence each time a case argument is implementing unexpected logical ||.
Bad CodeÂ
switch (x) { case 1 || 2: // Noncompliant; only '1' is handled doSomething(x); break; case 3: doAnotherThing(x); break; default: console.log("Boom!"); // this happens when x is 2 }
Good CodeÂ
switch (x) { case 1: case 2: doSomething(x); break; case 3: doAnotherThing(x); break; default: console.log("Boom!"); }
References
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.