First programming lesson: Control structures. Senior developer lesson: avoid them.
Problems:
Too many decisions together
Coupling
Duplicated code
Violation of Open/Closed Principle.
A new condition should not change the main algorithm.
Nulls
Solutions
Polymorphism
Create hierarchies/compose objects following Open closed principle.
Use State pattern to model transitions.
Use Strategy Pattern/Method Object to choose for branches.
Examples
Discrete Values
State transition
Algorithm choice.
Sample Code
Wrong
Right
Detection
Since there are valid cases for If/else usages, we should not pull the plug and forbid these instructions. We can put a ratio of if statements/other statements as a warning instead.
Relations
More info
![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)
How to Get Rid of Annoying IFs Forever
Maxi Contieri ・ Nov 9 '20
Credits
Photo by Adarsh Kummur on Unsplash
If debugging is the process of removing software bugs, then programming must be the process of putting them in.
Edsger Dijkstra
Top comments (3)
The code isn't at all different, not sure of the intent here?
If you see the code exactly the same is due to a bug in dev.to cache. Try refreshing the cache
It's been a couple of years and it's definitely the same for me :)