Clean Code And Code Smells

Clean Code

Clean code is crucial to developing good programs. Other programmers need to be able to follow the program with ease without needing too many comments. After reading an article about clean code written by Shoaib Mehedi, I noticed that I do follow most of the guidelines. This is due to learning about them in courses because they are often highlighted by professors.

One of the guidelines Mehedi wrote about that I would like to follow is “magic numbers. Magic numbers are assigned a number without a clear meaning. Mehedi gave the example below:

I often do the “bad practice” simply because I’m cutting down on the amount of lines of code. However, I can see how the “good practice” is better. This is something I will start doing in my code. All of the other twelve guidelines mentioned by Mehedi are things I already do in my code including avoiding using too many comments, large functions, repetitive code, and choosing good names for your variables.

Code Smells

Code smells are issues in the code that can lead to larger issues further down the road. It is especially important to watch out for code smells when working on a large code base. Common code smells are temporary fields, dead code, unnecessary primitive variables, and data clumps. I read an article that further explains these and many more examples. This article is written by an unknown author.

A code smell that I would like to stop doing in my code is having long parameter lists. This doesn’t happen often, but sometimes I have a function that takes in a lot of parameters. Some thing I can do to avoid this is if I’m passing in parts of an object, I can instead pass in the whole object for simplicity. In the body of the function, I could separate out the parts of the object that I need. Another thing I can do to solve this is separate out the function into multiple functions so that the multiple functions will have a shorter parameter list. It would be especially good to do this if the body of the original function was too long and doing too many things.

Citations

“Code Smells.” Refactoring.Guru, https://refactoring.guru/refactoring/smells.

Mehedi, Shoaib. “12 Conventions for Writing Clean Code.” Medium, Better Programming, 15 May 2021, https://betterprogramming.pub/12-conventions-for-writing-clean-code-e16c51e3939a.

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *