Having clean code is something that has been discussed many times since starting down the path of Computer Science. However, I have noticed many classes are looking for a working program versus a clean program. This has led me to develop some coding practices that I am not proud of just for the sake of completing a project on time. I have also noticed some professors don’t have the best examples of clean code either. On this mission to identify and fix smelly code, I read two articles, and have marked two important habits I want to work on.
The first article I read was Clean Code Explained. This article went into naming conventions, writing functions, not repeating code, and good commenting practices. Though this was all information that I have learned in the past, it was good to reread and key in on some things that I do. The first key takeaway is naming conventions. I have noticed some professors have this habit as well, but it becomes a problem when looking over code and not understanding what a certain variable is because it is just a single letter. For example,
var a = b/c;
could be written better:
var average = sum/number;
Naming variables and functions in a way that describes what they do, is helpful in code reviews, learning new code, and looking back at old code. It doesn’t feel great when even you can’t understand what you were writing!
Another fix I want to make is repeating code. I often write code, thinking, I will come back and refine it or make it cleaner. However, with due dates often looming, I don’t get to this part. I would like to make more of an effort of cleaning code up as I go. For example, if I notice that I am repeating something, instead of just moving on, I should take the time and make a function. I have found that I am developing a bad habit of writing long rambling programs.
This point was discussed in the second article I read, 5 Most Common Code Smells You Should Avoid. The article went into smelly duplicate code and super long programs. The point here is that we want to be writing short, efficient programs. While this usually helps people find more efficient ways to write things, it also helps others be able to quickly review the code. I want to start actively looking for ways to remove un-needed lines, add recursive functions, and cut down runtimes. Another note was how functions should really just have one purpose that can be identified through the name and return. I want to make sure I don’t have hidden actions within any function in the future.
While these points are important for me to work on, I also thought that each article provided many more useful tips to writing clean code. I highly recommend checking out the articles to learn more!
Leave a Reply