# Code Smell 133 - Hardcoded IF Conditions

> TL;DR: Don't leave a hardcoded mess on IFs.

# Problems

- Testability

- Hardcoded values

- Open/Closed Principle Violation

# Solutions

1. Replace all [IFs](https://maximilianocontieri.com/how-to-get-rid-of-annoying-ifs-forever) with a dynamic condition or [polymorphism](https://maximilianocontieri.com/how-to-get-rid-of-annoying-ifs-forever).

# Context

Hard-coding *iF* conditions is great when doing [Test-Driven Development](https://maximilianocontieri.com/tdd-conference-2021-all-talks).

We need to clean up stuff.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/894c442e034658ee3a6d602c1dcfcca4)
```csharp
private string FindCountryName (string internetCode)
{
  if (internetCode == "de")
    return "Germany";
  else if(internetCode == "fr") 
    return "France";
  else if(internetCode == "ar")
    return "Argentina";
    //lots of elses
  else
    return "Suffix not Valid";
}
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/5d45c77d532eb370ca6b78606c0db05e)
```csharp
private string[] country_names = {"Germany", "France", "Argentina"} //lots more
private string[] Internet_code_suffixes= {"de", "fr", "ar" } //more
 
private Dictionary<string, string> Internet_codes = new Dictionary<string, string>();

//There are more efficient ways for collection iteration
//This pseudocode is for illustration
int currentIndex = 0; 
foreach (var suffix in Internet_code_suffixes) {
  Internet_codes.Add(suffix, Internet_codes[currentIndex]);
  currentIndex++;
}

private string FindCountryName (string internetCode) {
  return Internet_codes[internetCode];
}
```

# Detection

[X] Automatic 

By checking If/else conditions we can detect hard-coded conditions.

# Tags

- IFs

# Conclusion

In the past, hard-coding was not an option.

With modern methodologies, we learn by hard-coding, and then, we generalize and refactor our solutions.

# Relations

%[https://maximilianocontieri.com/code-smell-36-switchcaseelseifelseif-statements]

%[https://maximilianocontieri.com/code-smell-102-arrow-code]

# More Info

- [How to Get Rid of IFs forever](https://maximilianocontieri.com/how-to-get-rid-of-annoying-ifs-forever)

- [Test Driven Development](https://maximilianocontieri.com/tdd-conference-2021-all-talks)

# Credits

Photo by [Jessica Johnston](https://unsplash.com/@jdjohnston) on Unsplash

* * *

> Don't be (too) clever. My point was to discourage overly clever code because "clever code" is hard to write, easy to get wrong, harder to maintain, and often no faster than simpler alternatives because it can be hard to optimize.

_Bjarne Stroustrup_
 
%[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]
