Skip to content

Latest commit

 

History

History
99 lines (53 loc) · 2.88 KB

File metadata and controls

99 lines (53 loc) · 2.88 KB

Code Smell 84 - Max < Min (Javascript)

Code Smell 84 - Max < Min (Javascript)

Some functions do not behave as expected. Sadly, most programmers accept them.

TL;DR: Don't trust max() and min() functions. Just ignore them.

Problems

  • Principle of the least astonishment

  • Bijection Violation.

  • Unexpected Results

Solutions

  1. Use mature languages.

  2. Avoid max() and min() functions.

  3. Model Infinites carefully.

Sample Code

Wrong

console.log(Math.max() > Math.min());

// returns false

console.log(Math.max());

// returns -Infinite

Right

console.log(Math.max() > Math.min());
console.log(Math.max());

// returns Exception. Not enough arguments passed.
// Max requires at least one argument

Detection

These functions belong to the standard Math library. Therefore, they are not easy to avoid.

We can block them on our linters.

Tags

  • Javascript

Conclusion

We need to be very careful using functions that violate real-world concepts using language tricks.

Relations

Code Smell 69 - Big Bang (JavaScript Ridiculous Castings)

More Info

Credits

Photo by Cris Baron on Unsplash

Inspired by @@Oliver Jumpertz

Twitter


Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

Rick Cook

Software Engineering Great Quotes


This article is part of the CodeSmell Series.

How to Find the Stinky Parts of your Code