【收藏】 The Simplest Anti-IF Code

原文地址:The Simplest Anti-IF Code

The basic problem is that IFs create dependencies, coupling between modules (methods, objects, components, etc.) and increases possible paths inside our code (which reduces legibility).

An IF seems like a quick and easy way to make changes, but for the reasons listed above, IF after IF, we create software full of duplications that can’t be modified.

Here’s a simple example:



 

Every change in bond type, for example, a new bond to evaluate, leads to a modification in our piece of code. Imagine what it would take to modify methods with built-in IFs and SWITCHes!



 

      
What do we gain by taking out IFs?

The advantage is that tomorrow, we can fulfill the request for a new type of bond by simply creating a new class, with the single required logic to calculate the value of that new bond.

The solution isn’t always to create a new abstract class or an interface. But the solution will always make the software flexible, communicative, testable, ready for change.

你可能感兴趣的:(simple)