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
Reify the condition.
Create configuration options and set the exception on the configuration behavior.
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();
}
Right
customer.liquidatePositionIfNecessary(0.15);
// This follows the Tell, Don't ask principle
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
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
Code Smell 133 - Hardcoded IF Conditions
Maxi Contieri ・ May 20 '22
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
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
This article is part of the CodeSmell Series.
Top comments (0)