1. 程式人生 > >What Is Clean Code?

What Is Clean Code?

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

Rarely does a coder have the freedom to sit with a piece of code until it’s perfect (also, “perfect code” is an illusion; everything has flaws). Still, writing the best possible code should be a software crafter’s top priority. At times, they may mean pushing back against deadlines, client demands, management demands, and fatigue. Business conditions often set boundaries on what’s “possible,” but a coder always has at least some control over the quality of what he or she writes.

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

All of us have experienced the disappointment that comes with using a low-quality product. Think of the new shirt that frays at the edges after just a few runs through the washing machine, or the plastic toy that breaks mere moments after your child takes it out of the box. Poorly crafted code is no different — it frays at the edges much faster than you might expect.

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

It’s important to take pride in your own work. It’s equally important to recognize that the code you write 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.