Have you ever heard about Code Smells?
Learn more about how to avoid them to develop better solutions! 🧵THREAD 🧵
#100DaysOfCode #Dev #CodeNewbie
"If it stinks, change it"
A code smell is a piece of code that goes against good practices or fundamental design principles and therefore negatively impacts the quality of our solution.
Here is a list of code smells and how to solve them:
1) Long Method Implementation
Methods that make you scroll just to get an idea of what is happening?
You are most probably missing some abstractions and decomposition. Delegate responsibilities to corresponding objects/classes and create smaller methods that make sense together.
2) The Bazooka (Contrive Complexity if you like fancy)
When using over complicated and complex designs for tiny problems. Always look for the best-suited design and implementation to solve the problem. The solution isn´t better because you have 10 classes and 2 patterns together.
3) Long Parameter List
Have you seen those signatures with 8+ params?
You are missing an object in most cases
The interface is a contract and having it all over the place & distributed isnt maintainable/scalable
Create the missing abstraction and more responsibilities will appear
4) Duplicated Code
This is the most typical and easy to spot. Do not repeat yourself. As soon as you find yourself copy/pasting the code stop and encapsulate it in a commonplace.
It can be done using delegation inside a class, composition with another class or inheritance.
5) Misplaced Methods
Avoid secretary methods where you add layers of calls when it is not necessary. Also applies to logic pertinent to another object being encapsulated in the user of said object.
Remove secretary methods. Relocate code to the pertinent class.
6) God Class
Having a class with too many responsibilities. It knows too many objects, has too many interfaces and every change impacts it.
The solution is not trivial, you probably are missing a redesign, a pattern, or extra classes.
The way to solve these and more code smells is to refactor your solutions. Which is a great topic for another thread or an article. Let me know if you liked this and I will work on that soon enough ;)!
Thanks for reading and keep your code clean without smells
Share this Scrolly Tale with your friends.
A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.
