DEV Community

Cover image for Coupling: The one and only software design problem
Maxi Contieri
Maxi Contieri

Posted on • Edited on • Originally published at maximilianocontieri.com

Coupling: The one and only software design problem

A root cause analysis of all failures of our software will find a single culprit with multiple costumes.

The enemy is always there. Many times disguised as laziness, sometimes simplification, and usually with the optimization outfit.

If we analyze many common mistakes, we will always find a single culprit. The bloody coupling.

Expanding the axioms

In previous articles, we talked about the definition of software design in an axiomatic way.
We enunciated the rule to know what objects to represent in our model:

and we showed the only principle that we should use:

We are going to add the only mistake we should avoid by all means to this axiomatic list.

Coupling Examples

Classes

Global variables link a global reference from the code. This link cannot be easily broken unless we connect to interfaces instead of references and useĀ dependency inversionĀ (theĀ SOLIDĀ D).
Having global variables in a structured language involves being attached to a reference that cannot be replaced, mocked or deferred over time. In object-oriented programming using classification languages, ā€‹ā€‹the problem is the same.

This is a step back to more pure functional languages where there's and explicit prohibition enforced by preventing functions from having side effects.

If we take an extreme and minimalist position:

Every function/method should only invoke objects in their attributes and/or their parameters.

Settings

Those plugs that allow us to 'configure' the software using arbitrary global references from anywhere on the code.

They are a particular case of global references and prevent the correct unit test of a system. If something must be configurable, this possible configuration must be passed as an object as we proposed in this article.

In this way we can replace the configuration on tests and have full control and no side effects.

Hidden Assumptions

As we have described in the note on bijections, partially ignoring this principle implies running the risk of losing information on the contract and making mistakes under different interpretations.

In our previous example where we representedĀ 10 metersĀ with theĀ number 10.

In this case, we are coupled to the hidden assumption thatĀ 10Ā representsĀ 10 meters.

Hidden

Hidden assumptions appear at the worst moments of the development cycle.

Null References

This is a particular case of the item above.Ā NullĀ should never be used because it violates our only non-negotiable principle since it is not bijective with any real-world entity (NullĀ only exists in the world of developers).

If we decide to use aĀ nullĀ as a flag of some particular behavior we are coupling the decision of the function implementer to the one who invokes it. This ambiguous semantics brings countless problems.

Same issue happens when someone sets a property toĀ null coupling to the reader of the attribute.

Singletons

TheĀ Singleton patternĀ is a controversial design pattern. If we look at it under the guidance of our single design rule we will discard its use immediately. An object is represented by aĀ *SingletonĀ *if there is only one instance of its class. This also violates the principle of being declarative since the uniqueness of a concept, in general, is coupled to implementation problems, so we are violating the only design rule that we imposed ourselves.

Besides,Ā *singletonsĀ *are generally referenced through their class name, so we add all the problems mentioned in the first paragraph.

If/Case/Switch and all their friends

IfĀ clauses have a hidden coupling between theĀ conditionĀ and the place where they areĀ evaluatedĀ and violate theĀ open/closed principle. (The O on the solid acronym).

Ifs (and henceĀ cases)Ā should be avoided unless these conditions are business rules thus related to theĀ bijection.

A business ruleĀ 'A bonus should be paid to employees with 3 or more years at a company'Ā can be safely stated by anĀ IfĀ clause but rules such asĀ *'If the employee's position is junior then pay them 10.000'Ā *shouldn't, since this is not essential on the business rule but accidental therefore should be treated with polymorphism.

In the real world, employees are aware of their position, but they are not usually aware of their age on the company.

Documentation

If you need to add comments to your code there's a smell you might not be declarative enough.

Code documentation is many times not synced with the code itself. Many times developers change the code and don't have enough courage to change the code documentation bound to the code. This is another subtle coupling case.

Some months later we read the code and the documentation and need a lot of time to figure out their meaning.

Ripple effect

If we are faithful to our unique design rule and have a declarative model, we will expect, consequently, that a small change in the requirements will generate a small change in the model and so on. When this does not occur, the dreadedĀ ripple effectĀ is produced, turning the software unpredictable and full of potential errors that hinder its maintenance.

Ripple

Solutions

There are many ways to remove coupling once identified.

In this article we will see coupling reduction techniques:

Decouple a Legacy System

Conclusions

Coupling is necessary because the objects must know each other to collaborate and be able to solve the problems raised in the simulation.

Finding out which binding is good versus which one is bad to avoid the wave effect requires a little experience and a lot of staying true to the rules defined in this article.


Part of the objective of this series of articles is to generate spaces for debate and discussion on software design.

We look forward to comments and suggestions on this article.

This article is published at the same time in SpanishĀ here

Top comments (2)

Collapse
 
gsarciotto profile image
Giovanni Sarciotto

Really great article

Collapse
 
mcsee profile image
Maxi Contieri

Thank you, Giovanni!