Yet more code smells? Plenty of!
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 V. Part I can be found here, Part II here, Part III is here, Part IV here, part V and the last one (for now).
Let's continue...
Adding persistence, serialization, displaying, importing, exporting code to an object bloats its protocol and brings coupling.
Photo by Robert Bye on Unsplash
Problems
Solutions
Examples
Sample Code
Wrong
Right
Detection
It is difficult (but not impossible) to create linting rules based on naming and hinting for suspect names.
Exceptions
We should try to use better languages/frameworks.
Tags
Conclusion
We are very used to see business objects polluted. This is normal. We need to reflect on the consequences and coupling from these designs.
Simple things should be simple, complex things should be possible.
Alan Kay
The most used and (in)famous design pattern in the world is causing us great harm.
Photo by Maria Teneva on Unsplash
Problems
Solutions
Examples
Sample Code
Wrong
God is the archetypical singleton example.
Right
Detection
This is a design pattern. We should avoid it by policy.
We can add linter rules for patterns like ‘getInstance()’ so new developers cannot infect code with this anti-pattern.
Tags
Conclusion
This is an historical mistake already acknowledged by the community. Nevertheless, lazy developers bring it again and again. We need to reach a consensus on its drawbacks.
The Diagram is Not the Model. The model is not the diagram. It is an abstraction, a set of concepts and relationships between them.
Eric Evans
Abbreviating is very important so that we look smart and save memory and mind space.
Photo by Jessica Knowlden on Unsplash
Problems
Solutions
Use meaningful and declarative names.
Examples
Sample Code
Wrong
Right
Detection
We can’t automate choosing what is a short name and a declarative.
Some “modern” and shinny languages enforce this bad practice. So we should wisely choose a good language instead.
Tags
Conclusion
Computer science was born from the mother of science (mathematics). In math, the assignment of single letter variables (i, j, x, y) is a good practice.
The concept of reference arose from the variable.
Many people wondered why mathematicians can work with such short variables, and computer scientists cannot.
For mathematicians, once entered into a formula, variables lose all semantics and become indistinguishable.
Our brain wastes a lot of energy figuring out what is the meaning of an abbreviation.
It is 2020, We need to write software for humans, not for compilers.
A long descriptive name is better than a short enigmatic name. A long descriptive name is better than a long descriptive comment.
Robert Martin
A class defines objects with lots of attributes.
Problems
Solutions
Examples
Sample Code
Wrong
Right
Detection
Most linters warn when you declare too many attributes. Setting a good warning threshold should be easy.
Tags
Conclusion
Bloated objects know too much and are very difficult to change due to cohesion.
Developers change these objects a lot, so they bring merge conflicts and are a common problems source.
So much complexity in software comes from trying to make one thing do two things.
Ryan Singer
When an object changes its state the best solution is to change the attribute, isn’t it?
Problems
Solutions
Examples
Sample Code
Wrong
Right
Detection
If we want to be extreme, we should consider every setter to be a potential state change. Linters can warn us. But we might end up getting too many false positives.
Exceptions
Tags
Conclusion
This technique is very elegant but can lead to over design. For example changing a visual component's color should be a counterexample to this smell.
We should be aware and very caution like with any other smell.
They are hints and not rigid rules.
First make the change easy (warning: this might be hard), then make the easy change.
Kent Beck
Those were the first 35. Nevertheless, I keep getting more suggestions on twitter, so they won't be the last!