Are we done on code smells? Guess not.
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 IV. Part I can be found here, Part II here , Part III is here and Part V Here.
Code Smell 16 - Ripple Effect
Small changes yield unexpected problems.
Photo by Jack Tindall on Unsplash
Problems
- Coupling
Coupling: The One and Only Software Designing Problem
Solutions
- Decouple.
- Cover with tests.
- Refactor and isolate what is changing.
- Depend on interfaces.
Examples
- Legacy Systems
Sample Code
Wrong
Right
Detection
It is not easy to detect problems before they happen. Mutation Testing and root cause analysis of single points of failures may help.
Tags
- Legacy
Conclusion
There are multiple strategies to deal with Legacy and coupled systems. We should deal with this problem before it explodes under our eyes.
More info
Architecture is the tension between coupling and cohesion.
Neal Ford
40+ Thought-Provoking Software Engineering Quotes
Code Smell 17 - Global Functions
Discouraged by Object-Oriented Programmings, Many mixed languages support it. And developers abuse them.
Problems
- Coupling
- Readability
- Maintainability
- Testability
Solutions
- Wrap the function in a context object.
Examples
- External Resources Access, Database access, Time and Operating System resources.
Sample Code
Wrong
Right
Detection
Many modern languages avoid them. For the permissive ones, scope rules can be applied and automatically checked.
Tags
- Global
Conclusion
Structured programming considers global functions harmful. Yet, we can observe some bad practices cross paradigm boundaries.
Relations
Singleton and Classes are global points of access.
More Info
The road to programming hell is paved with global variables.
Steve McConnell
Code Smell 18 - Static Functions
Yet another global access coupled with laziness.
Photo by Alex Azabache on Unsplash
Problems
- Coupling
- Testability
- Protocol Overloading
- Cohesion
Solutions
- Class Single Responsibility Principle is to create instance. Honor it.
- Delegate method to instance.
- Create stateless objects. Don't call them helpers.
Examples
- Static class methods
- Static attributes
Sample Code
Wrong
Right
Detection
We can enforce a policy to avoid static methods (all class methods but constructors).
Tags
- Global
- Libraries
Conclusion
Class are globals disguised. Polluting their protocol with "library methods" breaks cohesion and generates coupling. We should extract static with refactorings.
Relations
More info
There is no programming problem that can't be solved with one more level of indirection.
John McCarthy
Code Smell 19 - Optional Arguments
Disguised as a friendly shortcut is another coupling smell.
Problems
- Coupling
- Unexpected results
- Side effects
- Ripple Effect
- In languages with optional arguments but limited to basic types, we need to set a flag and add an accidental IF (another smell).
Solutions
- Make arguments explicit.
- All function calls must have same arity.
- Use Named Parameters if your language supports them.
Sample Code
Wrong
Right
Detection
Detection is easy if language supports optional arguments.
Tags
- Optional
- Laziness
Conclusion
Be explicit. Favor readability over shorter (and more coupled) function call.
More info
The trouble with programmers is that you can never tell what a programmer is doing until it’s too late.
Seymour Cray
Code Smell 20: Premature Optimization
Planning ahead of time needs a crystal ball no developer has.
Photo by Markus Spiske on Unsplash
Problems
- Coupling
- Testability
- Readability
- YAGNI
Solutions
- Create great models and bijections first.
- Create a conclusive benchmark once the model is working.
- Design for Performance.
- Use Test Driven Development technique. It always favors the simplest solution.
Examples
- Weird data structures
- Caches
- Singletons
Sample Code
Wrong
Right
Detection
This is a design smell so it can not be detected by mechanical tools (yet).
Tags
- Premature Optimization
- Antipattern
Conclusion
Defer performance decisions until functional models are mature enough.
Donald Knuth created/compiled the best/fastest algorithms and data structures. With great wisdom he warned us of abuse. Why do we think we are smarter than him?
More info
Programmers waste enormous amounts of time worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered.
Donald Knuth
Premature optimization is the root of all evil.
Donald Knuth
And that's it for now.
But rest assured, more smells will come.