Clean Code

Whether you are a student, professional, or even a recreational programmer, clean code should be a way of life. But what is clean code, and why should we care?

Clean Code Matters

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.

Cleanliness makes correctness and efficiency easier to achieve.

Read These First

For a quick introduction to the topics and techniques addressed by the umbrella term Clean Code, begin by reading this summary of the Clean Code principles from Robert C. Martin’s Clean Code book.

Next, read this 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. Some of it my even be called timeless. Some of it is common sense. But the book has flaws and 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.

Comments

“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

Testing

Not writing unit tests is not an option.

Formatting and Naming

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.

DRY Code

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.

Conceptual Organization

Large systems can be more understandable if they are constructed, layered, and wired together cleanly.

Efficiency

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.

Safety and Security

This section is a little different, being not about style but rather about responsibilities.

Compactness

Often, compact code is more readable than drawn-out code.

Be Reasonable and Pragmatic

You need to get along with others, don’t be mean and dogmatic.