DEV Community

Cover image for Code Smell 152 - Logical Comment
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

3 1

Code Smell 152 - Logical Comment

Code Smell 152 - Logical Comment

Code Smell 152 - Logical Comment

Temporary hacks might be permanent

TL;DR: Don't change code semantics to skip code.

Problems

  • Readability

  • Non-Intention Revealing

Solutions

  1. If you need a temporary hack, make it explicit

  2. Rely on your source control system

Context

Changing code with a temporary hack is a very bad developer practice.

We might forget some temporary solutions and leave them forever.

Sample Code

Wrong

if (cart.items() > 11 && user.isRetail())  { 
  doStuff();
}
doMore();
// Production code

// the false acts to temporary skip the if condition
if (false && cart.items() > 11 && user.isRetail())  { 
  doStuff();
}
doMore();

if (true || cart.items() > 11 && user.isRetail())  {
// Same hack to force the condition
Enter fullscreen mode Exit fullscreen mode

Right

if (cart.items() > 11 && user.isRetail())  { 
  doStuff();
}
doMore();
// Production code

// Either if we need to force or skip the condition
// we can do it with a covering test forcing
// real world scenario and not the code

testLargeCartItems() {}

testUserIsRetail() {}
Enter fullscreen mode Exit fullscreen mode

Detection

[X] Semi-Automatic

Some linters might warn us of strange behavior.

Tags

  • Comments

Conclusion

Separation of concerns is extremely important in our profession.

Business logic and hacks should always be apart.

Relations

Credits

Photo by Belinda Fewings on Unsplash

Thanks @Ramiro Rela for this tip


You might not think that programmers are artists, but programming is an extremely creative profession. It's logic-based creativity.

John Romero


This article is part of the CodeSmell Series.

Build gen AI apps that run anywhere with MongoDB Atlas

Build gen AI apps that run anywhere with MongoDB Atlas

MongoDB Atlas bundles vector search and a flexible document model so developers can build, scale, and run gen AI apps without juggling multiple databases. From LLM to semantic search, Atlas streamlines AI architecture. Start free today.

Start Free

Top comments (0)

MongoDB Atlas runs apps anywhere. Try it now.

MongoDB Atlas runs apps anywhere. Try it now.

MongoDB Atlas lets you build and run modern apps anywhere—across AWS, Azure, and Google Cloud. With availability in 115+ regions, deploy near users, meet compliance, and scale confidently worldwide.

Start Free

Announcing the First DEV Education Track: "Build Apps with Google AI Studio"

The moment is here! We recently announced DEV Education Tracks, our new initiative to bring you structured learning paths directly from industry experts.

Dive in and Learn

DEV is bringing Education Tracks to the community. Dismiss if you're not interested. ❤️