DEV Community

Cover image for Code Smell 186 - Hardcoded Business Conditions
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

2 1

Code Smell 186 - Hardcoded Business Conditions

You are FTX and your code allows special cases

TL;DR: Don't add hard business rules to your code.

Problems

  • Open / Closed Principle Violation

  • Hardcoding

  • Testability

Solutions

  1. Reify the condition.

  2. Create configuration options and set the exception on the configuration behavior.

  3. Don't use Settings/Configs.

Context

According to Reuters, in a recent FTX scandal, there was a hardcoded condition to skip risk controls to its own portfolio.

The code was explicit and developers were aware of that rule.

Sample Code

Wrong

if (currentExposure > 0.15 && customer != "Alameda") {
  // Be extra careful not to liquidate
  liquidatePosition();
}


Enter fullscreen mode Exit fullscreen mode

Right

  customer.liquidatePositionIfNecessary(0.15);

  // This follows the Tell, Don't ask principle
Enter fullscreen mode Exit fullscreen mode

Detection

[X] Semi-Automatic

We can search for primary hardcoded conditions (related to primitive types).

We might have more false positives than actual problems.

Tags

  • Hardcoding

Conclusion

If you make code reviews, pay special attention to this kind of hard coding.

Relations

More Info

Disclaimer

Code Smells are just my opinion.

Credits

Photo by Alexander Mils on Unsplash


Computer science inverts the normal. In normal science, you're given a world, and your job is to find out the rules. In computer science, you give the computer the rules, and it creates the world.

Alan Kay


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)

Dev Diairies image

User Feedback & The Pivot That Saved The Project

🔥 Check out Episode 3 of Dev Diairies, following a successful Hackathon project turned startup.

Watch full video 🎥

👋 Kindness is contagious

Discover fresh viewpoints in this insightful post, supported by our vibrant DEV Community. Every developer’s experience matters—add your thoughts and help us grow together.

A simple “thank you” can uplift the author and spark new discussions—leave yours below!

On DEV, knowledge-sharing connects us and drives innovation. Found this useful? A quick note of appreciation makes a real impact.

Okay