There are yet more code smells. Let’s keep changing the aromas. We see several symptoms and situations that make us doubt the quality of our development.
Let's look at some possible solutions.
Most of these smells are just hints of something that might be wrong. They are not rigid rules.
This is part III. Part I can be found here and Part II here.
The part IV, and finally Part V.
Objects or Functions need too many arguments to work
Photo by Tobias Tullius on Unsplash
Wrong
Right
Most linters warn when the arguments list is too large.
Relate arguments and group them. Always favor real world mappings. Find in real world how to group the arguments in cohesive objects.
If a function gets too many arguments, some of them might be related to the class construction. This is a design smell too.
Code Smell 11 — Subclassification for Code Reuse
Code reuse is good. But subclassing generates a static coupling.
Photo by Brandon Green on Unsplash
Wrong
Right
Overriding can issue warnings when subclassing concrete methods.Deep Hierarchies (more than 3 levels) are also a clue of bad subclassing.
In legacy systems is very common to have Deep Hierarchies and method overriding, we need to refactor them and subclass by essential reasons and not implementative ones.
Programmers use Null as different flags. It can hint an absence, an undefined value, en error etc. Multiple semantics lead to coupling and errors.
Photo by Kurt Cotoaga on Unsplash
Coupling among the callers and the senders.
Mismatch among the callers and the senders.
If/Switch/Case Polluting.
Null is not polymorphic with real objects. Hence Null Pointer Exception
Null does not exist on real world. Thus it violates Bijection Principle
NULL: The Billion Dollar Mistake
Wrong
Right
Most linters can show null usages and warn us.
Null is the billion dollar mistake. Yet, most program languages support them and libraries suggest its usage.
NULL: The Billion Dollar Mistake
I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.
Tony Hoare
40+ Thought-Provoking Software Engineering Quotes
Code Smell 13 — Empty Constructors
Incomplete objects cause lots of issues.
Photo by Brett Jordan in Pexels
Some persistence frameworks in static typed languages require an empty constructor.
Wrong
Right
Any linter can warn this (possible) situation.
Always create complete objects.
Make their essence immutable to endure through time.
Every object needs its essence to be a valid one since inception.
Writing a class without its contract would be similar to producing an engineering component (electrical circuit, VLSI (Very Large Scale Integration) chip, bridge, engine…) without a spec. No professional engineer would even consider the idea.
Bertrand Meyer
An object that knows too much or does too much.
Photo by Francisco Ghisletti on Unsplash
Wrong
Right
Linters can count methods and warn against a threshold.
Libraries were fine in the 60. In Object Oriented Programming we will distribute responsibilities among many objects.
Assertions, Preconditions, Postconditions and invariants are our allies to avoid invalid objects. Avoiding them leads to errors.
Photo by Jonathan Chng on Unsplash
Constructors are an excellent first line of defense.
Anemic Objects lack these rules.
Wrong
Right
It’s difficult to find missing preconditions, as long with assertions and invariants.
Always be explicit on object integrity.
Turn on production assertions.
Even if it brings performance penalties.
Data and object corruption is harder to find.
Fail fast is a blessing.
Fail Fast Philosophy, Explained
Code Smell 01 — Anemic Models
Writing a class without its contract would be similar to producing an engineering component (electrical circuit, VLSI (Very Large Scale Integration) chip, bridge, engine…) without a spec. No professional engineer would even consider the idea.
Bertrand Meyer
These would certainly not be the last smells.
We will keep on smelling the trash trail very soon!