Code Smell: Too much indirection

February 23, 2020·3 min read

code smellprogrammingtoo much indirectiontypescript

In the previous blog posts I wrote about Feature Envy and Divergent Change. We will continue with what I call "Too much indirection". I am not sure if that is a code smell or more of a code burden.

Indirection is the ability to reference something using a name, reference, or container instead of the value itself. When it is overused though, it can make the really difficult to follow the flow of the code. Imagine the following class:

The problem with this code (apart from the nested if statements which is another problem) and all these small (private) functions is that if you want to understand how getBasePrice() works you have to scroll up and down again and again, which needs a lot of cognitive power. In my dummy example I just had 4 functions, imagine having 10. In cases like that I prefer to either inline the statements or declare local variables for them.

I prefer to extract a statement to a seperate function only if the same statements is used in another function of the class, otherwise having local function variables makes reading the flow of the code easier.