Clean code, a way to spread joy!

Since I started my journey in flatiron school, the phrase “clean code” has been popping up everywhere. From the reading materials to the lectures from the instructors, everyone emphasizes about the importance of writing clean code. At first, I wasn’t able to fully understand the importance of clean code. However, as the complexity of the projects that I am working on gets higher, I started to truly experience the importance of clean code. I wanted to share a few tips from the book “Clean Code” by Robert Cecil Martin that I found helpful to keep in mind when writing code to keep the smell away!

First, keep your code “DRY”. This principle has been reference by many authors of software engineer books. It stands for “don’t repeat yourself”. There are different levels of repeating code. The most obvious one is the duplication of identical code. Having the same code repeats not only increase the length of the code but also create more chance for you to make errors. Another scenario of repeating code is having the same if/else or switch/case statement in multiple different modules. The least noticeable way of duplications is similar algorithms in different modules but the code was not written in the similar fashion. If you are noticing duplication, there are usually ways to make your code dry. For example, if you need to use the same algorithms over and over again. Maybe declare a variable for the algorithm so you can use it without writing out the whole function each and every time.

Second, “too much information”. Generally speaking, the longer the code is the longer it takes to run. In addition, a longer code usually means it has more information. However, more often than not you don’t need all the information. When you have too many functions and each depends on each other to pass along data, it creates more opportunity for the code to break. It is important to plan ahead regarding what is the responsibility of each module and keep each module as small as possible. If you realize one of your module is doing everything, it may be an indicator to separate them into smaller modules.

Third, “dead code”. Writing code is a never ending trial and error process. You will always encounter the code that is no longer applicable. It may be a small line here and there. You may be able to identify the none applicable code when you just started the project. However, the longer you leave the code there, the more stinky it gets. Soon it will take you a long time to figure out what your code is doing let along if you are working as a group. Therefore, it is best practice to delete the dead code as soon as you found it!

“Clean code” sounds like a very self explanatory concept. However, there are endless ways to make your code cleaner. The book “Clean code” is very easy to read and provide a lot of very useful guidelines when you write your code. As an entry level software engineer, it is a good read to cultivate good habits. Remember, it is always less work to keep the code clean as you are writing them than come back after a whole project to clean up the dirty code!

Reference:

Martin, R. C. (2009). Clean code: A handbook of agile software craftsmanship. Upper Saddle River, NJ: Prentice Hall.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store