Suppose you did not read this article and you continued writing code as usual. After a half year or a year, you need to change some functionality. You come back to the same code, your code is like a maze where you can enter easily but don’t know way out. Isn’t it frustrating? Now you going to waste a lot of time understanding the code which you have written.

Do not worry, this blog will help you to save a lot of time which you can spend on other productive work. So put on your seat belt, ride is going to begin

1. Always use Docstrings

Docstring is used for documentation purposes. Docstring starts and ends with ”’ or “””. It is used below the declaration of any class, function, and method name. In docstring we generally specify

  1. What is class, method or function
  2. Description of method or function parameters
  3. What this method or function returns
class Calculator:
    """Implements basic airthmetic operation add"""

    def __init__(self, a, b):
        """
        This constructor method initializes two instance variables.
        
        Params:
        a : int
            First operand
        b : int
            Second operand
        
        Return:
            instance of class Calculator
        """
    
    def add(self):
        """
        This method performs addition

        Return: int
            sum of operands
        """

2. Always use comments

Always make sure to put a comment which explains what the following code is doing. Try to cover all functionality of a function, method or script by putting comments. This helps other developers to understand code easily.

3. Follow proper naming conventions

A lot of beginners do this mistake, they do not follow the proper naming conventions. Follow proper variable, function, method, class naming conventions specified in PEP8. This help developer to differentiate among function, method, class, and variable names. Try expressive name for variable, function, method, and class.

4. One function for one purpose

Function should define a purpose. Sometimes we use function for multiple actions. Sometimes we use many parameters in a function which makes function hard to debug and test.

5. Avoid messy code

  1. Follow DRY( Do not repeat yourself) concept to make code modular
  2. Use python packages and modules
  3. Follow SOLID principles

Although we want our code to be short and nice but also keep readability, maintenance, performance.