Code Smell: Divergent Change

February 22, 2020·2 min read

code smellprogrammingdivergent changetypescript

Continuing the series of blog posts about code smells, let's talk about Divergent Change. Divergent change occurs when you have to do too many changes in a class/module to introduce a new feature/change. Imagine the following Product class:

If we want to introduce a new product type, let's say Vitamins we would have to make changes in 3 different methods of the class.

We can improve the code by introducing a mapping:

Now if we want to introduce a new product type (e.g. Vitamins), we will make changes in a single place without having to touch the methods of the class (with the possibility of introducing a bug affecting the existing products for example).