Complexity: Excessive Dependencies
Dependencies increase complexity because it adds more things to consider. If developers are unsure of what hidden dependency will break, they will have to dig into every line of code to make sure it is safe. Efferent coupling is what this module depends on, which is easier to manage than afferent coupling, which is other things that depend on this module.
Let's look at a simple example to explain why dependencies add to complexity:
- Example of a simple app: one which is fully self contained, depends on nothing. eg a html based site with no serverside functionality.
- Example of a complex app: one which depends on 100 different packages which needs updates and has version conflicts, it has to reach 20 different apis, it consumes messages from many different queues, it reads and writes from a db, it logs to files, other applications are dependent on it's API and messages it produces, etc.
- Cut out the dependency: If you can do without the dependency, scrap it. Often developers throw in every possible small tool they can find because it does something small or because the default is "not good enough". Adding an extra package, tool, dependency must be an absolute last resort, not the first thing you do.
- Improve Visibility: Do NOT hide dependencies by instantiating them (or their wrappers) in random places scattered throughout the code base. Rather keep the full list of dependencies in one place by instantiating them in one place or by injecting them via a DI container, so that the dependencies are clear.
- Improve Management: Make sure your code is modular so that dependencies are locked to certain areas, making it easier to change.
- Improve Understanding: Make sure the flow of dependency is uni-directional, it should be easy to reason about the flow of dependencies without explaining the details of the code. When you open an application you have never seen before and it's structure represents the flow of dependencies, the overall picture of the project becomes far easier to understand. Compared to a structure where anything could be dependent on anything and the project structure does not represent a clear flow.