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...
First programming lesson: Control structures. Senior developer lesson: avoid them.
Photo by Adarsh Kummur on Unsplash
Problems:
Wrong
Right
Since there are valid cases for If/else usages, we should not pull the plug and forbid these instructions. We can put a ratio of if statements/other statements as a warning instead.
If debugging is the process of removing software bugs, then programming must be the process of putting them in.
Edsger Dijkstra
Protected attributes are great for encapsulating and controlling access to our properties. They might be warning us for another smell.
Photo by Jonathan Farber on Unsplash
Wrong
Right
In languages supporting protected attributes we can avoid them by policy or have a warning of this smell.
Protected attributes are yet another tool we should use carefully. Every decision is a smell, and we should be very careful with attributes and inheritance.
Subclasses shouldn’t always share all characteristics of their parent class but will do so with inheritance. This can make a program’s design less flexible. It also introduces the possibility of calling methods on subclasses that don’t make sense or that cause errors because the methods don’t apply to the subclass.
Steve Klabnik
Avoid too abstract names. Names should have real world meaning
Photo by Rodion Kutsaev on Unsplash
Wrong
Right
We can set up policies and rules warning for certain words like base, abstract, helper, manager, object etc.
Finding names is the last thing we should do on our designs. Unless we have a clear business understanding, good names emerge at the end after defined behavior and protocol boundaries.
More info
There are only two hard things in Computer Science: cache invalidation and naming things.
Phil Karlton
70s first tutorial: getCurrentDate(). Piece of Cake. We are in the 20s Time is global no more
Wrong
Right
We should forbid global functions policies. We need to couple to accidental and pluggable time sources.
Date.today(), Time.now(), and other global system calls are coupling smell.
Since tests must be in full environmental control. We should easily set up time, moved it back and forth etc.
Date and Time classes should only create immutable instances. It is not their responsibility to give the actual time. This violates Single Responsibility Principle.
The passage of time is always scorned by programmers. This makes objects mutable and designs poor and coupled.
In programming, the hard part isn't solving problems, but deciding what problems to solve.
Paul Graham
Data Transfer Objects (DTOs) are widely used, and they 'solve' real problems, do they?
Wrong
Right
We can use the same anemic object detectors.
We can check for anemic classes with no business object behavior (removing serializes, constructors, mutators etc).
DTOs are a tool and an established practice in some languages. We should use them with care and responsibility.
If we need to disassemble our objects in order to send them away from our realms, we need to be extremely cautioned. Since dismembered objects have no integrity considerations.
His author warns us about its actual abuse.
The best smells are something that's easy to spot and most of time lead you to really interesting problems. Data classes (classes with all data and no behavior) are good examples of this. You look at them and ask yourself what behavior should be in this class.
Martin Fowler
Are 40 enough?. When will we stop?
I keep getting more suggestions on twitter, so they won't be the last!