Clean Code & Code Smells

Clean Code

“Clean code always looks like it was written by someone who cares.” 

Robert C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship

Have you ever looked at your code after a while? Or have you ever looked at someone’s code and thought you could have done it better and more efficiently? Or did you ever received a feedback on your code? If answer to any of this questions is ‘yes’, then you know that writing good and clean code is not an easy task, and requires a lot of practice.

As a part of this assignment, I read an article on freeCodeCamp “Clean Code Explained – A practical Introduction to Clean Coding for Beginners.” My first thought was that I probably won’t learn much from the article since it has ‘beginners’ in its title. Reading the beginning of the article was confirming my beliefs – I would never use a letter for a variable name! How dare you are even accusing me of such thing!? This is literally the first rule of the clean code – use a descriptive name for variables:

int w = 12; // bad
int width = 12; // good

I believe meaningful and defining names of variables are a necessity for a clean code. Misleading names, noise words (the, info, data, variable, object, etc.), and unpronounceable names will make your code harder to read and understand. At the end of the day, code is meant to be read by humans. Just like Martin Flower said, “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” 

Examples of “horrible” variable names:
Disinformation:
peopleList = []; // bad
people = [] // good

Unpronounceable Names:
// Bad
const yyyy = new Date("2021-03-25");
yyyy.getFullYear();
// Good
const year = new Date();
year.getFullYear();

I will be honest and admit that the end of the article perfectly described why my code is not as clean as I would like it to be. One thing that I struggle with a lot is keeping my code consistent. My most used programming languages are C#, Python, and JavaScript. This term, on top of it I am learning Dart for CS 492 – Mobile Software Development. Switching from one convention to another is not an easy task, and very often I tend to ignore it just to achieve the results. However, I am confident that knowing why your code is bad, and clearly seeing the problem is already a step to writing better and cleaner code.

Code Smells

For this part of the assignment, I found the article “Everything You Need To Know About Code Smells” on codegrip.tech. I knew right away that this will be a great article because I saw this:

Isn’t it funny?

Between clean code and code smells, the second topic is an entirely different beast to discuss. Code smells are not bugs or errors in your code. Code smells are “violations of the fundamentals of developing software.” The article states that code smells are different for each project and developer, and I couldn’t agree more – projects have requirements that must be met, and each developer has their own way of writing code. 

I will admit that my code often smells more than I would want it. Very often I have duplicate code (even though I put a lot of effort in keeping it Don’tRepeatYourself). My most common smells are long methods. For some reason, I always struggle to break tasks into subtasks, which would help me in splitting a large method into smaller methods.

Additionally, the article gives different ways how to get rid of code smell. I think one of the main takeaways for me was refactoring. Refactoring is the process of rewriting code and changing software in a way that will not change the program’s behaviors. And apparently, refactoring is something that I lack. After writing my code, I rarely go back and review it or think about ways to improve it, especially when it comes to school projects. The fast pace environment does not give you a lot of time for that. However, I should definitely consider investing time in refactoring my personal and portfolio projects to make sure that code smell won’t embarrass me in front of my GitHub visitors.

Conclusion

To finish my post, I want to say that it is not easy to write clean and “good” smelling code. I believe that it is something that comes with a lot of experience and requires a lot of determination to improve your code. However, I strongly believe that everyone who writes code must learn about good code practices and learn about code smells. Being able to sport bad practices and code smells in your code is already progress! Remember – practice makes perfect.

Print Friendly, PDF & Email

Leave a Reply