DEV Community

Cover image for Code Smell 84 - Max < Min (Javascript)
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

1 1

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 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
Enter fullscreen mode Exit fullscreen mode

Right

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

//returns Exception. Not enough arguments passed.
//Max requires at least one argument
Enter fullscreen mode Exit fullscreen mode

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

More Info

Credits

Photo by Cris Baron on Unsplash

Inspired by @oliverjumpertz


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


This article is part of the CodeSmell Series.

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong · web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video 🌶️🔥

Top comments (0)

Gen AI apps are built with MongoDB Atlas

Gen AI apps are built with MongoDB Atlas

MongoDB Atlas is the developer-friendly database for building, scaling, and running gen AI & LLM apps—no separate vector DB needed. Enjoy native vector search, 115+ regions, and flexible document modeling. Build AI faster, all in one place.

Start Free

👋 Kindness is contagious

Show gratitude for this enlightening post and join the vibrant DEV Community. Developers at every level are invited to share and grow our collective expertise.

A simple “thank you” can make someone’s day. Leave your appreciation below!

On DEV, collaborative knowledge clears our path and deepens our connections. Enjoyed the article? A quick message of thanks to the author goes a long way.

Count me in