# Code Smell 186 - Hardcoded Business Conditions


> 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](https://maximilianocontieri.com/code-smell-29-settingsconfigs).

# Context

According to [Reuters](https://www.reuters.com/technology/how-secret-software-change-allowed-ftx-use-client-money-2022-12-13/), 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

[Gist Url]: # (https://gist.github.com/mcsee/27cdd48bf20694b735f7d5914c086022)
```solidity
if (currentExposure > 0.15 && customer != "Alameda") {
  // Be extra careful not to liquidate
  liquidatePosition();
}


```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/d43ab068cbec6d361fb429fd2860f518)
```solidity
  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

%[https://maximilianocontieri.com/code-smell-133-hardcoded-if-conditions]

%[https://maximilianocontieri.com/code-smell-29-settingsconfigs]

# More Info

- [Reuters on FTX Crash]( https://www.reuters.com/technology/how-secret-software-change-allowed-ftx-use-client-money-2022-12-13/)

# Disclaimer

Code Smells are just my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [Alexander Mils](https://unsplash.com/@alexandermils) on [Unsplash](https://unsplash.com/s/photos/steal-money)  
  
* * *
> 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_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
