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

  • BAD_MIN_MAX_FUNC
  • 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

x = Math.min(0, Math.max(100, x)); // BAD_MIN_MAX_FUNC alarm. The result is always 0.

Compliant Code Example

x = Math.max(0, Math.min(100, x));

Version

This rule was introduced in DeepScan 1.0.0-alpha.

See