Clean Code and Code Smells

As a programmer, it is vital to write clean code as not only you are doing yourself a favor, you are doing other programmers a favor as well. Clean code improves readability, maintainability, collaboration, bug reduction, and efficiency [Mfonido, 2023]. I cannot stress to both new programmers and seasoned programmers that you must write clean code regardless if it is a short five-lined program or a huge, two-thousand-lined corporate program. I have learned in my 10 years of programming that clean code is the holy grail of self-development and I will explain why.

First off, have you ever looked at your programs that you created years ago and wonder what the heck the program is doing? Yet, you spend too much time figuring out what it does and eventually you give up and vow to never touch it again. Admittedly, I am guilty of that practice and I have learned from my mistakes.

About four years into my ten year coding experience, I started to implement clean code and success ensued. When I started organizing my code neatly and commenting on almost every line, my knowledge of programming languages and data structures had dramatically improved. I struggled a lot on basic data structures like sorting an array, accessing a specific index in an array, and iterating through an array. Well, guess what? I am now a master of arrays thanks to clean code. One of the few practices that I implemented was naming variables that are readable in the English language as well as commenting the steps that the code is processing.

When I learned programming languages, all textbooks used variables for arrays like ‘i’ to store iteration count, ‘x’ to hold values, and storing array values like ‘x = array[i]’. At that time, I was a novice programmer and I understood what the sample code does, but this didn’t really help me understand the ‘big picture’. Suppose I was given a task to find the median of the array etc. but I had difficulty connecting the dots on why we were using iteration count for the array variable. Well, I was able to figure it out when I started using variable names like ‘index’ instead of ‘i’ and ‘value = array[index]’ or ‘arrayValue = array[index]’ to hold data from the index of the array. Look at the C code below and it is clearer to understand what is being processed when saving the content of one array to another:

for (index = 0; index < array.length – 1; index++) {
newArray[index] = array[index] // Get the value of array at that index and save it to // newArray
}

The above code sparked my understanding of arrays and it allowed me to see the big picture. I went on for the next six years utilizing clean code and incorporating new skills to make code better for myself and everyone else. Another thing that I would like to mention is that adding a comment block is extremely useful for complex programs.

Comment blocks are very important especially when working on large programs. I cannot emphasize how beneficial it is to me when scrolling through programs that have thousands of lines of code. Comment blocks allow me to find certain sections of the code that I need to fix, implement, or work on because multi-line comment blocks really stand out more than single line comments that are infused in long sections of code. Below is one example that I do in large C programs:





/* ############################################### */
/* # # */
/* # # */
/* # CALCULATING THE MEDIAN # */
/* # # */
/* # # */
/* ###############################################
*/

… code to calculate median goes here …

The above comment block sample is very easy to spot when you need to fix a part of the program and it can be done quickly. I highly recommend all programmers to incorporate comment blocks when working on large programs.

Lastly, I am still learning on how to improve my code even though I have a decade of programming experience under my belt. I really need to work on code smells. Code smells is something that I never really practiced since all of the programs that I have created are very one-dimensional. Most of the programs I created didn’t really need refactoring since the code said exactly what it did. However, when I enter the career force, I know for a fact that I will be need to be careful with not only clean code, but code smells as well. Code smells can cause a lot of technology debt and can come back and bite me or the employer very hard. This is something that I do not want to do especially when I am out in the field. In summary, I would plan on incorporating methods to reduce code smells and keep on improving clean code. I would also recommend others to do the same. You will thank yourself later.


Bibliography
Mfonido, 2023: Mfonido, Mark, Writing Clean Code: Best Practices and Principles, 2023, https://dev.to/favourmark05/writing-clean-code-best-practices-and-principles-3amh

Print Friendly, PDF & Email

Posted

in

by

Tags:

Comments

Leave a Reply

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