5 simple tips for writing clean code

5 simple tips for writing clean code

Code that is easy to understand, easy to change also another developer can read, understand and easily extendable is called clean code.

Table of contents

No heading

No headings in the article.

If you write a program but other's developers do not understand it then there is no point in writing such a code. Because no one wants to continue a project with a messy code base. Also if you want to scale a project later on, it will take so much time to understand everything. It also hampers the business.

“Of course, bad code can be cleaned up. But it’s very expensive.”
― Robert C. Martin,
Clean Code: A Handbook of Agile Software Craftsmanship

That's why writing clear code is important for everyone. It doesn't matter if you are a beginner or an experienced programmer. Everyone should write clean code.

So, here are my 5 tips for you to write clean code.

  1. Use meaningful names

    Name your variable or function name according to its work. Keep it simple and easy to understand. Such as you give a variable name "data". No one can understand what kind of data your talking about. But if you say "userData" then it'll be more clear to for others to get its purpose.

    Also, conventions while writing names. It makes code easier to read and maintain.

  2. Try to Avoid Writing Unnecessary Comments

    Comments help the developer to understand the purpose of a piece of code. It is useful but writing unnecessary comments makes your code messy and It can create confusion and distraction as well. So, write comments when it is needed.

  3. Be Consistent

    When writing code, consistency is key. If you use camel case for naming, use it consistently across your code base. There are so many linter and style guides you can use to apply these standards. Such as the Airbnb style guide.

    Consistency makes your code base clean, readable and easy to understand.

  4. Single Responsibility Principle(SRP)

    There is a good way to organize the code in any programming language by using Classes, Functions or methods. The Single Responsibility principle is a concept of making classes or functions or methods for one specific responsibility. It reduces the complexity of code. Increased readability, extensibility, and maintenance. It also makes the code reusable and reduces error and gives better testability.

  5. Follow Developer Best Practice

    There is a tremendous amount of best practices on the internet. Use the most common best practice of a language or framework you are working with. It helps the entire team to follow a guide. It reduces the learning curve for the code and increases the overall speed and efficiency of the team. It helps to write clean code as well.

Writing clean code is an art. You have to achieve it by practicing more and more. You just need to be conscious of your code.

That's it for now. Follow me to get more tips and wonderful blogs.