What is clean code, and how do I make sure my code is clean? These are the questions I had when first hearing the term “Clean Code.” In my quest to answer these questions, I found a blog (https://codingsans.com/blog/clean-code) transcribing Karolina Tóth’s interview with Robert C. Martin (Uncle Bob) who literally wrote the book Clean Code. The main topics covered in the interview are clean code, developer and manager communication, and code ethics. Focusing on what Uncle Bob has to say on the importance of clean code while including my personal experiences.
What is Clean Code?
Uncle Bob quotes Michael Feathers saying, “Clean code always looks like it was written by someone who cares.”. Which makes sense, as I have taken pride in having simple aspects of Clean Code such as clearly named variables and proper indentation in my code blocks. In contrast, as a tutor, I was asked to review code written with neither of these aspects by a student who openly expected not caring about the class.
How Can You Measure Clean Code?
Looking at the functions reveals a lot about the code and how clean it is. Uncle Bob states that you can measure the size of a function, and that going above six lines of code they become too large. Naming the functions properly will also create cleaner codes as Uncle Bob points out that having smaller and more specific functions will require more specific names. This combined with the if and while statements will create easy to read code that is close to english.
Importance of Clean Code
Understanding the importance of clean code starts with examining the consequences of having dirty code. Uncle Bob emphasizes that everything goes slower when you are working with “dirty” code. Developers commonly believe that quick and messy code is faster upfront, however the slowdown happens a lot faster than most realize. The example given by Uncle Bob is someone writing bad code in the morning and after a simple lunch break they will be lost in what they wrote.
“Slow Is Smooth, Smooth Is Fast”
“Slow is smooth, smooth is fast,” is a quote drilled into my head from my highschool soccer coach. Take your time with your movements, make them deliberate, and the result will be smooth. Going slower and executing a move is better than going too fast and failing. It feels slower to stay disciplined in writing clean code, however the time saved by having easy to read code will immediately and only grow over time.
Using Clean Code Moving Forward
Listening to what Uncle Bob had to say about the flow of functions and how they should create sentences really sticks with me. As someone who never saw the need for “the flood of functions”, having easy to read sentences in my code will make returning to projects significantly easier.
From the blog CS@Worcester – CS Learning by kbourassa18 and used with permission of the author. All other rights reserved by the author.