There are 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 II. Part I can be found here, Part III here, Part IV and Part V.
The code is difficult to read, there are tricky with names without semantics. Sometimes using language's accidental complexity.
Image Source: NeONBRAND on Unsplash
Problems
Solutions
Examples
Exceptions
Sample Code
Wrong
Right
Detection
Automatic detection is possible in some languages. Watch some warnings related to complexity, bad names, post increment variables, etc.
Also Known as
Conclusion
Too clever developers write cryptic code to brag. Smart developers write clean code. Clean beats clever, every time.
Tags
Using boolean variables as flags expose accidental implementation and pollutes the code with Ifs.
Image Source: Phil Hearing on Unsplash
Problems
Solutions
If Boolean maps to a real-world entity it's safe. Otherwise model as a State to favor Extensibility. This also follows Open/Closed Principle.
Examples
Exceptions
Sample Code
Wrong
Right
Detection
Automatic detection can warn for boolean usage but this can yield false positives.
Relations
Some languages have issues with boolean comparators.
If these are coupled with accidental complex languages, booleans are a common error source.
Also Known As
Tags
Conclusion
Take extra care when declaring something boolean. Flags are difficult to maintain and extend. Learn more about the domain or try migrating to state design pattern. Use polymorphism instead of ifs/switch/cases.
Making long chains generate coupling and ripple effect. Any chain change breaks the code.
Image Source: Chewy on Unsplash
Problems
Solutions
Sample Code
Wrong
Right
Detection
Automatic detection is possible using parsing trees.
Also Known As
Tags
Conclusion
Avoid successive message calls. Try to hide the intermediate collaborations and create new protocols.
Code that is no longer used or needed.
Image Source: Ray Shrewsberry on Pixabay
Problems
Solutions
Examples
Exceptions
Avoid metaprogramming. When used it is very difficult to find references to the code.
Laziness Chapter I: Meta-Programming
Sample Code
Wrong
Right
Detection
Coverage tools can find dead code (uncovered) if you have a great suite of tests.
Tags
Conclusion
Remove dead code for simplicity. If you are uncertain of some code you can temporarily disable it using Feature Toggle. Removing code is always more rewarding than adding.
Objects or Functions need too many arguments to work.
Image Source: Tobias Tullius on Unsplash
Problems
Solutions
Exceptions
Sample Code
Wrong
Right
Detection
Most linters warn when the arguments list is too large.
Tags
Conclusion
Relate similar arguments and group them. Always favor real-world mappings. Find in the real-world how to group the arguments in cohesive objects.
If a function has too many arguments, some of them might be related to the class construction. This is a design smell too.
That's all for now! More code smells coming soon!