7 Tips for Writing Clean Code


Writing clean code is what you must do in order to call yourself a professional. There is no reasonable excuse for doing anything less than your best.

Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.

The total cost of owning a mess exceeds the cost of cleaning it up.

Clean code is a pretentious term that appears all over the place, but that no one really stops to define. Until now.

What is clean code? The answer is that it’s code that’s easy to read and easy to understand. And I think most developers would agree with this definition.

But how do you get from reading this definition to actually writing clean code? That’s the question we’ll answer in this blog post.

There are a lot of different ways we can write code, and not all of them are created equal. But if we know what makes some code better than other code, we can use these considerations when writing our own programs and thus write cleaner code.

To uncover the best way to write clean code, let’s first take a look at what makes some code more difficult to read than other code. Then we’ll review some standards that make it easier for us to write clean code. And finally, we’ll look at some tools we can use to aid us in our quest for cleaner code.

Writing clean code is what you must do in order to call yourself a professional. There is no reasonable excuse for doing anything less than your best. Clean code according to Uncle Bob is simple and direct. Clean code should be readable and convey meaning presenting itself like a nice bottle of single malt scotch. Some of my favorite quotes on the subject are:

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

By Michael Feathers

We don’t make mistakes, we just have happy accidents.

By Bob Ross

The function of good software is to make the complex appear to be simple.

By Grady Booch

A language that doesn’t affect the way you think about programming, is not worth knowing.

By Alan Perlis

So how does one go about writing clean code? Let’s take a look at some tips on achieving this lofty goal:

1. Avoid long functions and classes

Your functions should be small enough to fit on your screen so you can quickly scan for logic errors and unexpected behavior. Long functions are usually a sign that you are doing more than one thing, which is the first thing to avoid when writing clean code. If you find yourself writing a 200 line function, stop, take a step back, and refactor your code into smaller pieces.

2. Avoid doing too much in one place

The idea of single responsibility principle is that every module or class should have responsibility over a single part of the functionality provided by the software and that responsibility should be entirely encapsulated by the class, module or function. All its services should be narrowly aligned with that responsibility.

3. Use descriptive variable names

If there is one piece of advice I would give to developers who want to write cleaner code, it’s to use descriptive variable names. Using good naming conventions will make it easier for others (and even yourself) to understand what your variables are used for without having to read through lines of code just to figure out what a single line is doing.

4. Use comments only if you need to

Comments are meant to be used sparingly – they should not repeat what the code

A clean code is an organized and fast code. It allows a single developer to understand the code easily. A clean code reduces debugging time, enhances productivity and improves developer experience.

The best way to write a clean code is to follow standard conventions and adhere to best practices.

This blog post covers some tips that can be followed while writing a clean code:

1) Follow the naming convention:

It’s important to follow a naming convention while coding. It is recommended to use simple and meaningful names for variables, functions and classes. One should also avoid using abbreviations or acronyms as it will be difficult for developers to understand the meaning of such abbreviations/acronyms.

While writing a clean code, we should also avoid using similar names for different things in our code. For Example, if we have two methods in our code named ‘get_data()’ and ‘get_data_from_file()’, then we need to change the name of one of these methods so that it becomes clear what each method does. We can rename them as ‘get_data()’ and ‘get_data_from_file()’ respectively.

Tip 1: Write code that is easy to delete, not easy to extend

Tip 2: Use descriptive names

Tip 3: Write code in the language of the problem domain

Tip 4: Use functions as comments for why something is being done.

Tip 5: Make code express intent. Don’t make it express implementation.

Tip 6: Understand what your code does by reading it out loud.

Tip 7: Don’t do clever things. Do simple things clearly.

1. Write code that is easy to delete, not easy to extend

2. Do one thing

3. Encapsulate conditionals

4. Prefer polymorphism to if/else or switch/case

5. Separate things that change from things that stay the same

6. Replace magic numbers with named constants

7. Use intention-revealing names


Leave a Reply

Your email address will not be published. Required fields are marked *