How to write clean code: A beginners guide

How to Write Clean Code: A Beginner's Guide

Clean code is code that is easy to read, understand, and maintain. It is code that follows a consistent style, has meaningful names, and avoids unnecessary complexity. Clean code is not only beneficial for the developers who write it, but also for the developers who will work with it in the future.


In this blog post, I will share some tips and best practices on how to write clean code, based on my own experience and the principles of Robert C. Martin, also known as Uncle Bob, who is a renowned software engineer and author of the book Clean Code: A Handbook of Agile Software Craftsmanship.


Tip #1: Use Meaningful Names

One of the most important aspects of writing clean code is choosing meaningful names for your variables, functions, classes, and modules. Meaningful names make your code more expressive and easier to understand. They also reduce the need for comments, which can become outdated or misleading over time.


Some guidelines for choosing meaningful names are:

- Use descriptive and specific names that reveal the intention of the code. For example, instead of using x or y as variable names, use something like width or height.

- Use nouns for variables, classes, and modules, and verbs for functions and methods. For example, use Customer as a class name, and getCustomer as a function name.

- Use consistent naming conventions throughout your code. For example, if you use camelCase for variables and functions, use it everywhere. If you use underscores for constants, use them everywhere.

- Avoid using abbreviations or acronyms that are not widely known or understood. For example, instead of using db or conn as variable names, use database or connection.

- Avoid using names that are too similar or too generic. For example, avoid using names like data or result, which can be confusing or ambiguous. Also avoid using names like a1 or b2, which can be easily mistaken for each other.


Tip #2: Keep Your Functions Short and Focused

Another key aspect of writing clean code is keeping your functions short and focused. Short and focused functions are easier to read, understand, test, and reuse. They also reduce the chances of introducing bugs or errors in your code.


Some guidelines for keeping your functions short and focused are:

- Follow the single responsibility principle (SRP), which states that a function should do one thing and do it well. For example, instead of having a function that validates user input, sends an email confirmation, and updates the database, split it into three separate functions that do each task individually.

- Avoid nesting too many levels of indentation in your functions. This can make your code hard to follow and prone to errors. Instead, use early returns or break your code into smaller functions.

- Avoid using too many parameters in your functions. This can make your code hard to read and test. Instead, use objects or data structures to group related parameters together, or refactor your code to eliminate unnecessary parameters.

- Use descriptive and consistent names for your parameters and return values. This can make your code more self-documenting and easier to understand.


Tip #3: Write Clear and Useful Comments

Comments are an essential part of writing clean code. They can help explain the purpose, logic, or implementation details of your code. They can also help other developers understand your code better and avoid making mistakes or assumptions.


However, comments can also be harmful if they are unclear, redundant, outdated, or misleading. They can clutter your code and make it harder to read and maintain.


Some guidelines for writing clear and useful comments are:

- Write comments that explain why you are doing something, not what you are doing. The code itself should be clear enough to show what you are doing. If it is not clear enough, consider refactoring it instead of adding comments.

- Write comments that add value to your code, not repeat it. For example, avoid writing comments like // increment i by 1 , which is obvious from the code itself.

- Write comments that are relevant and up-to-date with your code. If you change your code, make sure to update or delete any related comments. Otherwise, they can become inaccurate or misleading over time.

- Write comments that are concise and grammatically correct. Avoid writing long paragraphs or sentences that are hard to read or understand. Also avoid writing comments that contain spelling or grammar errors.