No programmer likes looking at their old code. It’s like looking back at pictures of your horrible haircut from middle school. You cringe and think, “Why is it so bad? What was I thinking?” This repulsion you get looking at old projects is likely because of the code smells and bad habits. It’s great to think of how much you’ve improved, learning better practices, as you were exposed more to good, clean code. You also probably hung on to some bad habits, or code smells, that still stink up your work today.
This is my story. And for this post, I read two great articles (linked and referenced below) and applied them to my current coding practices. With this new awareness, I hope to fix up some bad habits and create new ones to replace them and write cleaner code.
What’s That Smell?
How can you tell if your code stinks? How can you fix it and keep the code you write in the future smelling rosy? Well the Refactoring Guru on thier blog explains the common smells in five different categories: Bloaters, Object-Orientation Abusers, Change Preventers, Dispensable, and Couplers. A few smells in these categories looked a little too familiar to me.
Long Methods
First up, from the Bloaters category, we have long methods. This is a smell that is very familiar to me, and it’s in so many of my projects. Sometimes when you have an idea and are on a roll, you just keep adding and adding to the method and the class. Then you end up with one giant class and a huge do-all method.
This smell makes debugging, maintaining, and adding to your code very difficult [1]. For example, let’s take this JavaScript code I wrote two years ago for an interactive cleaning game:
const drying = async () => {
console.log('drying');
textBox.textContent = "";
switchBackground('1');
startType();
typeWriter("Let's dry this plate. Take a deep breath in.");
await sleep(4000);
endType();
countingPop();
textBox.textContent = "";
await sleep(3800);
Doesn’t look too bad, right? Wrong. It goes on for 80 more lines. All in the “drying” function. Most of the code is very repetitive too (we’ll talk about this later, *wink wink*). Debugging this code was a mess because I remember not knowing where the code was broken, but because there was an error, the whole chunk of code wouldn’t work!
Even though this was from two years ago, it is a bad habit I still have. I always say I will “Get back and break them up later” but I never do. Moving forward, I want to make a conscious effort to break up my methods and functions to smaller, more-specific pieces.
Repetitive Code
Repetitive code is my kryptonite, my downfall. I have the habit of finding something that works and then copying in my code again and again. If it works once, it will work everywhere, right? While this is true, it is also a code smell. It makes your files so much longer and takes much more time to write than just making a helper function or even a few (if we want to keep the methods small) to cover the repetitive tasks. Then a simple, one-lined call will replace a whole block of code.
The Refactoring Guru has some great advice to help that I really think would help me in the future. They say to use the “Extract Method“. This is a way to combine two different functions that have very repetitive code. You put all the re-used code in one function, then in a separate function or two, you put the different code from the original repetitive code. Then you can simply call two functions instead of writing an entire block of code. This is a great idea, and I can’t wait to add it to my code to help me avoid repetitive code.
Cleaning Up
Writing clean code is an important practice for any developer. It is a way to make your code more readable and maintainable. If you want to become a better programmer (and I do) this is a great place to start.
Mert Oz writes on his blog that writing clean code is not easy! The most important thing to remember when wanting to become a better programmer who writes clean code is to not give up. It takes discipline, practice, and the ability to spot bad code to be good at writing clean code.
In this section, I will take a look at some clean coding practices I struggle with and would like to implement in the future.
Comments
In Mert Oz’ article, he quotes the famous Brian Kernighan who once said, “Don’t comment bad code – rewrite it.” [2]. If comments are meant to make unclear code more understandable, then it is just like treating the symptoms and not the cause. The solution would just be writing code that is easier to understand. This practice of diligently looking at your code and evaluating its readability is difficult and time-consuming. However, the more you do it, the easier writing clean code the first time will be. Soon you won’t need to rewrite sections so often. This practice of not relying too heavily on comments is something I would like to start doing more often.
Naming
I am a lazy namer! When I first started learning how to code, I didn’t see the importance of good naming practices. The programs were so small and simple that I couldn’t imagine anyone not being able to understand what my variable “x” did. Now, I see the importance, but it is a hard habit to break! Names should be meaningful and enhance the readability of your code [2]. Improving my naming practices will even help me with the previous clean coding practice of comments! With better names, comes less comments explaining the variables.
Conclusion
Eliminating code smells and writing clean code are not easy thing to do. They require you to be conscious of the code you write, and forces you to become an honest evaluator of your work. With time and practice, I plan to improve my skills by focusing on less reliance on comments and better naming. I will work towards making my methods and functions smaller and reduce repetition. I hope to look back at my code a year from now and be proud of my progress, instead of being embarrassed by my bad coding habits!
References
[1] “Code Smells,” refactoring.guru. https://refactoring.guru/refactoring/smells
[2] M. Oz, “Guide on Writing Clean Code – Trendyol Tech – Medium,” Medium, Jun. 12, 2022. https://medium.com/trendyol-tech/guide-on-writing-clean-code-496bb1100cde (accessed Jan. 15, 2025).