Fixing one bug often causes multiple new bugs. This article explains why by introducing some basics about dependencies. It also briefly introduces parts of UML to visualize these dependencies.
Every programmer knows this problem: you fix one bug and three new bugs occur elsewhere which need to be fixed, too. This is a Code Smell called Shotgun Surgery. But why does this happen at all?
The answer lies in the dependencies structure of your classes. If you change one class, this change may affect all classes which are depending on your changed class. These affected classes may thus require further changes and affect further classes depending on them.
To solve this problem you need to keep the dependency structure of your software clean. But what exactly is the “dependency structure”?
From the example above, you can see that there is something like a “dependency flow” through your code. I want to introduce a way to visualize this flow: UML Diagrams.
What is UML?
UML is short for Unified Modelling Language. It is a collection of different diagram types, to display all kinds of problems and solutions. These diagrams are standardized, widely accepted and used in the industry, especially in the sector of information technology.
UML Class Diagram Basics
The UML Class Diagram is especially useful to understand dependencies between your classes. So we need to introduce some basics.
In the following pictures we show how to display classes and their relationships in Class Diagrams. Take a look at the following pictures and their descriptions.
There are many more things you could display in a class diagram, but we don’t need more to understand how dependencies work.
All of the above relationships display an arrowhead pointing away from the depending class. This means changes to one class may affect exactly the classes pointing at it:
The dependency flow
When you display your software in a class diagram now, you can follow the arrows backward to tell which class affects which other classes directly or indirectly:
It’s not always trivial to prevent these indirect dependencies. The easiest approach is to keep the dependency-chains short, by inverting dependencies. But I will cover that in a future article.
You now have some basic understanding of why dependencies between classes cause problems and how to identify and visualize them using UML. We also explained why UML arrowheads point into specific directions.