I’m senior software engineer specialized in declarative designs and S.O.L.I.D. and Agile lover.
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.
Code Smell 36 – Switch/case/elseif/else/if statements
First programming lesson: Control structures. Senior developer lesson: avoid them.
- Too many decisions together
- Duplicated code
- Violation of Open/Closed Principle.
- A new condition should not change the main algorithm.
- Create hierarchies/compose objects following Open closed principle.
- Use State pattern to model transitions.
- Use Strategy Pattern/Method Object to choose for branches.
- Discrete Values
- State transition
- Algorithm choice.
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.
- Favor composition
- Don’t subclassify attributes.
- Extract behavior to separate objects.
- Use traits (if available).
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.
Code Smell 38 – Abstract Names
Avoid too abstract names. Names should have real world meaning
- Choose meaningful names.
- Find metaphors.
- Avoid words like abstract, base, generic, helper etc.
- Use rules for naming.
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.
There are only two hard things in Computer Science: cache invalidation and naming things.
Code Smell 39 – new Date()
70s first tutorial: getCurrentDate(). Piece of Cake. We are in the 20s Time is global no more
- Fragile Tests
- Timezone Problems
- Use Dependency injection to decouple time source.
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.
Code Smell 40 – DTOs
Data Transfer Objects (DTOs) are widely used, and they ‘solve’ real problems, do they?
- Anemic Object
- Inconsistent Data
- Duplicated logic
- Duplicated structure
- Class Polluting
- Information Hiding Violation
- Code repeated among mutators, accessors, serializers, parsers
- Ripple Effect
- Data integrity
- Transfer anemic data on arrays.
- Use real business objects.
- If we want to transfer partial objects: use proxies or null objects to break the reference graph.
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.
Are 40 enough?. When will we stop?
I keep getting more suggestions on twitter, so they won’t be the last!
Create your free account to unlock your custom reading experience.