Coding is a form of writing. When you code, you are expressing yourself. Code cleanly, to show pride in your work, and, importantly, that you care about (1) yourself, (2) the teammates that will read your code, and (3) the users that are trusting in the correctness of your software.
If your code is not clean, it is error-prone, hard to reason about, and hard to test. Others cannot read it well. Clean code is like well-written prose: it communicates clearly and effectively.
Cleanliness makes correctness and efficiency easier to achieve.
You must be able to write clean code but also recognize clean code. If you are working with others' code, whether human-generated or AI-generated, you need to be able to understand the code, and clean it yourself where necessary. Humans can get lazy or sloppy, or perhaps have never learned the importance of clean code. AI code assistants have been trained on a lot of unclean code, so they might generate slop.
You are one ultimately responsible for creating well maintainable, reliable, and efficient systems, even if an AI writes the code. You need to know this stuff.
What distinguishes well-written code from poorly written code? Perhaps some aspects of quality code are subjective, but to be honest, there are far more that are widely agreed upon objective measures!
There’s a short video by Kantan Coding that presents three laws for readable reliable code:
A followup video has three more laws:
Robert C. Martin (Uncle Bob) wrote a now-pretty-famous book a while back called Clean Code. You can read a summary of the Clean Code principles from this book, as well as a more detailed article, explaining some of the principles. (It’s okay if you don’t understand every single principle for now; many are Java-specific since this book dates from way back in 2008 when Java was king, but most are very general.)
Finally, browse the “Smells and Heuristics” section of Martin’s book. It’s good to know what kinds of coding constructs to avoid as well as what kinds to write.
Wait what’s all this about a single book by a single individual?Well, Martin’s book is called “Clean Code” after all. And a lot of what he says in the book is reasonable. Maybe some of it my even be called timeless? Some, but not all of it, common sense.
The book does have its flaws. It is NOT gospel.
Next, and this is important too, read this negative review of the Clean Code book to add some balance!
Next, free yourself from thinking Martin’s book is the last word. If you can, read The Art of Readable Code instead (it’s good).
Now that you have a sense of what the term “Clean Code” refers to, and you know that people can disagree on some of the details of what is and what is not clean code, and you know not to adopt certain practices just because some individual carved them in stone, I’ve added my own short list of things in the world of clean code that I find important. I’m sure it is incomplete.
“Nothing can be quite so helpful as a well-placed comment. ... Nothing can be quite so damaging as an old crufty comment that propagates lies and misinformation.”—Robert C. Martin
Not writing unit tests is not an option.
Sloppy formatting can be construed as being lazy or not taking pride in yourself or your work. It reflects badly on your competence and potentially on your worth as a prospective employee. Your code should, in addition to being correct, look beautiful. It should read, in the words of Grady Booch, “like well-written prose.”
Note that the use of a linter or code prettier can check for these items and more, and generally automatically fix any problems. Once you have learned the basics of programming and have gotten familiar with editing and running programs, use these tools.
So many bugs have been traced to “I remembered to change it here but not there.”
You have some flexibility with DRYness
In rare situations it is possible to be “too” dry. Ask an expert when unsure.
Large systems can be more understandable if they are constructed, layered, and wired together cleanly.
Sometimes a readability versus performance tradeoff pops up, but quite often the most readable code actually is the most efficient. And sometimes, your code may be prone to efficiency attacks, so watch out.
strlen, say) over and over again if they will always be returning the same result. If you need the result of a long-running function call more than once, you probably want to cache it.This section is a little different, being not about style but rather about responsibilities.
null before dereferencing. look up a property from through a reference (generally through . or ->), ask yourself whether there is any way the pointer can be null. If your language has a ?. operator, use it!mallocs and frees.
destroy operation.
strcpy of a passed in string into a fixed-size local string variable.
strcpy.
Often, compact code is more readable than drawn-out code.
if found==true, instead say if found
if index>0 return true else return false, instead say return index>0
You need to get along with others, don’t be mean and dogmatic.
Everyone, especially students, should practice coding skills by writing software without assistance. The practice of writing helps you internalize the principles of quality code.
But you will make mistakes, and should frequently ask agents for a code review.
Also, don’t write everything on your own!
You should also review the code that agents write for you. When you understand the principles of quality code, you can guide your agent to write code in a certain way and recognize when the agent’s code can be improved. Use the language of clean code in your prompting: the agents will certainly understand it.
We’ve covered: