Featured Stories

What Is Clean Code?

Lessons learned from the bible of software engineering

Severin Perez
Sep 10, 2018 · 6 min read
Photo by pan xiaozhen on Unsplash

In a field as vast and varied as software engineering, constant reading is essential for building foundational knowledge and exploring branch knowledge. As in any profession, software development has its must-reads, and Clean Code is one of them. The book comes up consistently in Google’s search results for “top software engineering books,” and it’s well-known in engineering circles.

Here are a few lessons learned from engaging with this touchstone for the very first time.

In many ways, Clean Code is structured like an owner’s manual for code. It’s an extremely opinionated book, as most great books are.

Martin describes best practices and deconstructs poor practices, admonishing those who fail to recognize why and how those habits are bad. Martin is open about his failures. Years of reading (and sometimes writing) bad code have made him intent on helping other developers avoid danger. Taken together, the book’s hundreds of recommendations no doubt produce pristine code. That said, most engineers learn the hard way — by suffering through enough bad code that they want to make it better.

But Clean Code is more than a tutorial. It describes not just how but why to write good code. More than rote memorization of rules (though many of the book’s rules stick with me), I read to understand a great programmer’s inviolable values. For me, it all boiled down to three key concepts.

1. Craftsmanship Matters

Bottom line: Code that “works” is not always “done.” If a coder’s job is to produce a product that solves a problem, should it matter how it solves the problem? Yes. Software done well benefits everyone in the long run — clients, users, companies, and coders. Code that is messy, rushed, or “good enough” is code with an eye on short-term returns.

Poorly crafted code frays at the edge much faster than you might expect.

Martin suggests that the key to writing stellar code is high-quality input (thoughtful, maintainable, flexible code) that results in high-quality output (long-term business payoffs).

2. Extra Effort Today Saves Pain Tomorrow

Compare this to something you treasure for its quality. I once wore a pair of shoes almost daily for over a decade before they finally wore out. They were a bit more expensive than other shoes but their quality was undeniable, and I saved money in the long run. What did I do when the time finally came to throw those shoes out? I bought an identical pair because I knew they’d last me another decade.

The same principle applies to code. High-quality input not only produces longer-lasting output, it also saves money and builds customer loyalty. Meticulously refactoring and testing your code may take longer in the beginning, but the savings in technical debt and maintainability are well worth the extra effort.

3. Your Code Is Not Your Own

Overly clever tricks, hacks, and sleights of programmatic hand are only fun for the author.

This may seem like a bit of a paradox — after all, writing clean code requires you to have a stake in its quality. But good code is not really yours because it’s intended for consumption by others: your teammates, your customers, and even your future self. What happens when some future project maintainer (possibly even yourself) can’t understand why you’ve written something the way you have? They will no doubt have to spend precious time and mental energy deciphering the puzzle you’ve left for them.

What Is Clean Code?

  1. Clean code is simple. Perhaps not simple in algorithmic or system-level complexity, but certainly simple in implementation. Overly clever tricks, hacks, and sleights of programmatic hand are only fun for the author and diminish the code’s long-term value. The same goes for long-winded code that takes forever to get to the point.
  2. Clean code is readable. If the naming conventions, spacing, structure, and flow used in a program are not designed with the reader in mind, then that reader will almost certainly fail to understand the original author’s intent. Conventions about how to write readable code may seem dogmatic or lacking in expressiveness, but they help to make code communal rather than arcane.
  3. Clean code is considerate. Code that does not do its best to inform future readers is code that does not respect their time. Clean code should be written with the assumption that future consumers are intelligent, thoughtful professionals (like you), and it should go out of its way to help them.
  4. Clean code is tested. No one writes perfect, bug-free code on the first try. Even if it were possible to do so, there is no guarantee that perfect code won’t break later. Writing clean code means writing tested code. That way, future users can be confident they’re interacting with something that works. Moreover, when making changes, they will have a ready-made test suite to confirm that nothing broke.
  5. Clean code is practiced. Writing clean code requires good muscle memory, just like playing an instrument, kicking a ball, or frying an egg. The best way to learn to write clean code — and, more importantly, retain the skill — is to do it all the time. When you’re at home working on a personal project, do it with clean code — even when no one else will ever see it.
  6. Clean code is relentlessly refactored. Clean code should be in a constant state of refactoring. With a good test suite to back up your code, you can refactor it as much as you like and never worry about breakage.
  7. Clean code is SOLID. Good code is as much about good design as it is about cleanliness. Following the SOLID principles is one way to ensure that your code is flexible, maintainable, and long-lasting.

If we take Martin’s book as a bible, the attributes for what makes “clean code” could multiply exponentially. But this list, culled from careful reading of Clean Code, is a good place to start. Adhering to these principles requires constant attention to detail and a willingness to acknowledge that what you wrote yesterday can still be improved today — even if it worked just fine before.

Although Clean Code contains a plethora of useful rules and best practices, it’s perhaps more useful for the mentality it imbues in its readers — that craftsmanship matters, effort today saves pain tomorrow, and code is never really your own. The more coders read it, the cleaner the world’s code will be, and all of our jobs will be easier.

Writer | Developer

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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