Why You Should Write Clean Code
Developers discuss and debate clean code all the time. They even debate what constitutes clean code. The discussion about clean code is as old as languages themselves; entire languages (I’m looking at you, Python) have been written around the philosophy of easily readable code.
But why should you care about writing clean code? What does it matter? Especially if you work on a small one or two person team, why should you care about clean code?
The computer doesn’t care about clean code
At it’s most basic, code is transformed from the letters you write and routines you invent into 1′s and 0′s that the computer processes in a particular order to get a response.
The computer doesn’t honestly care whether or not the code is beautiful. It doesn’t care whether or not you wrote every expression on its own line, what your indentation strategy was, or whether or not you put all the code on a single line (assuming, of course, you’re not using a whitespace-sensitive language).
The computer only cares that the code compiles, and can be executed. Pass this check, and the code will run. Fail this check, and the code won’t run. Period.
Code is for communicating ideas
So if the computer doesn’t care, why should you?
The answer is that code is written to communicate ideas in a human readable form.
If you didn’t care about the expression of ideas, you could simply write code in 1′s and 0′s for the processor to handle. After all, this is what code will end up being in the end.
But languages are designed to express ideas as much as they’re designed to instruct computers. Language features are added because they make it easier to express certain ideas. Entire languages have been developed with the purpose of handing a particular set of problems. Many if not most languages are implemented using another language (PHP is written in C, for example). Developers could write in C, but PHP allows them to express themselves in a new and different way from C.
Communicating ideas requires clarity
Since our goal in writing code is to communicate ideas to other humans (not to computers), it is imperative that we write that code clearly and cleanly.
Writing clean code can take many lessons from writing documents or books.
Developers often edit code for clarity, in much the same way that an editor modifies a document for clarity. And developers look for better ways to express their ideas, much like a writer seeks to express their ideas in multiple revisions.
Messy code, though it may work, fails at communicating the ideas of the author as well as clean, clear, organized code. Sure, variable names like $a and $i might hold values, but descriptive names like $databaseObject and $iterator are much clearer and articulate a far better point.
Clean code is about pride of authorship
The purpose of writing clear and clean code is really about your pride in authorship. Since your code communicates your thoughts to another developer, and to the rest of the world, it’s crucial that you do everything you can to make those ideas clear.
When we write poor code, we say that we don’t care. When we write messy code we’re saying that we don’t have pride in what we’re doing.
Writing clean code takes just as much effort as writing a good blog post or a great book. The logical arguments and functionality are the plot and cohesiveness of your program.
Take pride in your work. Clean code is important!
In two weeks time I’ll be opening preorders for my latest book, The Ten Commandments of Code. Sign up below to receive updates and special offers on the book!
Write better object
oriented PHP today.
A brand new book on object oriented PHP that will leave you an object oriented master.Learn more about the book »