Clean Code, what does it mean?

The term “Clean Code” could be confusing for beginners. What does it mean? You clean the car, you clean your bedroom, how do you clean your code?

Lets start by thinking about almost the opposite of Clean, Messy. You can imagine a messy room, and I'm sure you can also imagine messy code. When I think about messy code, I think of lots of lines of code, poorly named variables, multiple classes in the same file and so on.

What are the benefits of clean code?

There are multiple benefits for writing clean code, here are some of the main ones:

  • It is easier to understand.
  • You can tell what the code will do.
  • You will be able to understand it, even when coming back to it after 6 months.
  • Other people will be able to understand it.
  • It will be easier to maintain.
  • It will be easier to test.

What things can you do to write clean code?

Here are some of the main things you can do towards writing clean code:

  • Give your variables, methods and classes meaningful names.

    If you have a method which gets a customer by id, then name it 'GetCustomerById' and name the parameter 'id', or 'customerId'.
  • Don't make your methods and classes do too much.

    Apart from the main calling method, you should aim for your classes and methods to do just one thing each. That way it is easier to maintain, test and to understand.
  • Formatting

    Use white space effectively. If you want a particular part of your code to stand out, use leave an empty row above and below it.

    When adding new methods to a class, add them to the bottom. This helps with code reviews as the only change in the file will be the addition of code at the bottom, rather than lines moving down to accommodate it.
  • Don't write comments inside the method.

    If you feel the need to write comments inside your method to explain what the code is going to do, you probably need to create a method for that code and give it a meaningful name e.g. SendEmailToCustomer()

    See this video about why you should not write comments in your code.

Watch on YouTube

How can I learn more about writing clean code?

There are some excellent resources on the internet to help you learn how to write clean code. Here are some of my favourites:

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Proudly sponsored by

Moriyama

  • Moriyama build, support and deploy Umbraco, Azure and ASP.NET websites and applications.
AppVeyor

  • CI/CD service for Windows, Linux and macOS
  • Build, test, deploy your apps faster, on any platform.
elmah.io

  • elmah.io is the easy error logging and uptime monitoring service for .NET.
  • Take back control of your errors with support for all .NET web and logging frameworks.
uSync Complete

  • uSync.Complete gives you all the uSync packages, allowing you to completely control how your Umbraco settings, content and media is stored, transferred and managed across all your Umbraco Installations.
uSkinned

  • More than a theme for Umbraco CMS, take full control of your content and design with a feature-rich, award-nominated & content editor focused website platform.
UmbHost

  • Affordable, Geo-Redundant, Umbraco hosting which gives back to the community by sponsoring an Umbraco Open Source Developer with each hosting package sold.