DEV Community

md Shafi Uddin
md Shafi Uddin

Posted on • Edited on

The Clean Code Best Practices

Clean Code and Good Coding Practices:
Clean code is the foundation of maintainable software. It emphasizes writing code that is organized, readable and adheres to coding standards. Clean code reduces complexity, making it easier to understand, modify, and maintain. It ensures that your code is efficient, error-free, and accessible to other developers who may collaborate on the project.

For example, consider the following Python code for calculating the area of a rectangle:

Image description
Meaningful Variable and Function Names:
Choosing descriptive names for variables and functions is vital. When you use meaningful names, it's easier for anyone reading the code to grasp the purpose and usage of these elements. Avoid cryptic abbreviations or encodings in names, as these can make the code less intuitive and harder to maintain.

For instance:

Image description
This is better than using cryptic names like calc or abbreviations.

Functions with Single Responsibility:
Following the single responsibility principle, each function should have a clear, single purpose. Functions that perform only one task are easier to understand and maintain. They should also have minimal arguments and avoid output arguments, ensuring they do one thing well.

Here's an example:

Image description
Splitting responsibilities into separate functions makes the code more maintainable.

High Cohesion and Low Coupling:
High cohesion means that related components, such as class members or functions, are grouped together, while low coupling indicates that these components are loosely interconnected. Object-oriented principles like encapsulation and information hiding are essential to achieve this. High cohesion and low coupling make your code more modular and maintainable

Here's an example:

Image description
The Student class has high cohesion as it relates to student information. The School class has low coupling because it interacts with the Student class without being tightly bound to it.

Judicious Use of Comments:
Comments should provide context and explanations for the code, focusing on the "why" rather than the "what." Code should ideally be self-documenting, but when necessary, comments should clarify intent, decisions, or unusual code behaviour.

Here's an example:

Not so useful comment
total = calculate_total(price, tax) # Calculate the total

A more useful comment
total = calculate_total(price, tax) # Add tax to the price to calculate the total cost

The latter comment provides context.
Other Good Practices:
Additional practices include separating concerns, writing effective unit tests, minimizing code duplication, using explanatory variables and named constants, and adhering to coding conventions and standards. These practices collectively contribute to clean, maintainable code and help improve collaboration among developers working on a project.

Separating concerns:

Image description

Writing unit tests:

Image description
Minimizing duplication:

Image description

Top comments (0)