Math.min and Math.max functions should be combined in correct order

  • Error
  • Medium
  • No tags

This rule applies when Math.min and Math.max functions are incorrectly combined.

x = Math.min(100, x); specifies upper limit of x as 100 and x = Math.max(0, x); specifies the lower limit as 0. When these limit values are combined in incorrect order, the same result is always produced in unexpected way.

Noncompliant Code Example

View with compliant examples side by side
x = Math.min(0, Math.max(100, x)); // BAD_MIN_MAX_FUNC alarm. The result is always 0.

Compliant Code Example

View with noncompliant examples side by side
x = Math.max(0, Math.min(100, x));


This rule was introduced in DeepScan 1.0.0-alpha.


Was this documentation helpful?