October 17, 2013 by Roman Ožana

6 Important Qualities of Clean Code

How often do you hear someone refer to a mess at work as “spaghetti code”? Probably a lot. So, how do you know that someone doesn’t look at your code and say the same thing?

You don’t.

image
Image via agx.eu

No matter how many books you read about best practices for design and coding practices – it’s just not enough. Our ability to evaluate both good and bad qualities of code are often dependent (and limited) to the projects we work on and the people we work with. In other words, one of the best ways to recognize and appreciate clean code is to make sure you understand what it is.

Now, this is no easy task and some would argue, nearly impossible to define because of the topic’s subjectivity. However, we’ve put together a short list of 6 principles that can be applied to achieve a basic level of clean code.

1. Take pride in writing crisp, clear code that is both understandable and readable. 

“Clean code is simple and direct. Clean code reads like well-written prose.”

– Grady Booch, author of Object Oriented Analysis and Design with Applications

As a programmer, you should think of yourself as a craftsman. Someone whose work can be seen and appreciated. Maybe your customers will never get to see your work (or fully understand it), but other developers can.

When browsing through code, you should feel that it was made to be read by any developer, regardless of whether that developer was the original person to write it.

2. Clean code should be simple and focused. Bad code tries too hard. 

“Don’t spoil a perfectly good program by overembellishment and over-refinement.”

– Andrew Hunt, The Pragmatic Programmer 

Don’t let your code run wild, and avoid being redundant. If you follow the SRP (Single Responsibility Principle) every object will remain undisturbed. According to popular definition, SRP means that if you can think of a reason to change a class – there should never be more than one.

Your code should also comply with the DRY (Don’t Repeat Yourself) principle. Large projects can become overwhelmingly complex, so a way to deal with complexity is to reduce a system down into manageable components and sub-components. This way, you’ll be able to change single elements of a system without having to change unrelated elements.

3. Less is always more. 

Code should be minimal. Keep your classes and methods short. Make sure that you divide your code well (even within a class). This will also make it easier for another developer to read.

Additionally, remove or omit commented code. Don’t be afraid to remove old versions – delete it ruthlessly and without hesitation. If you’re using version control, you’ll still be able to find it.

4. Your code should be for everyone, not just you. 

“Clean code can be read, and enhanced by a developer other than its original author.” 

“Big” Dave Thomasfounder of Object Technology International 

Don’t be selfish. Look at the big picture, and don’t be self-concerned about what you are personally are achieving. You should write code with others in mind. In other words, produce code that is:

  • maintainable
  • flexible
  • extendable
  • testable

This means that someone else can easily continue or repurpose your code without anything getting lost in translation.

5. No matter what language you write in, your code should look like it was made for the problem.

“It is not the language that makes a program look simple, but the programmer who makes the language appear simple.”

Robert C. Martin, author of Clean Code  

This means the language (whichever one you are using) should seem like it was made for the problem you’re solving. In short, don’t use awkward workarounds. Think of it this way – for every problem, there is a clean solution.

6. Care about your code. 

“Clean code always looks like it was written by someone who cares.”

Michael Feathers author of Working Effectively with Legacy Code 

Clean code is essentially taking pride in your work. Being able to effectively convey our ideas to other developers should be crucial for all developers. Therefore, when we write dirty code, we’re letting everyone know that we lack pride in our work.

Treat your work as a craft, as an art form. Something that deserves to have every detail scrutinized and thought over.

Just a note… 

This post is meant to provide a basic overview about some of the principles of clean code – but the topic is much more complex and goes beyond what we’ve written here.

Please share more characteristics to clean up code!

Leave a comment below or on Facebook. Or, you can always tweet us @testomatocom.

*Quotes via informit.com

Is your website available? Testomato can check your website availability every 15 seconds from 10 different locations around the world and will send you an alert if the site is unavailable.

Keep track of the important parts of your website with simple checks that can run every minute. They can check plain words, HTML code, HTTP headers, redirects … and much more. Never miss anything.

Websites break, it happens all time. You'll be the first to know. Testomato has an extensive database of and will let you know if it finds any error on monitored url.