18: Good Programming Practices
Good Programming Practices
Coding is an engineering process. To streamline and efficiently program, we have to follow some practices that make our code easy to understand not for others but also for the developer itself. In the following chapter we will advise some practices. Following these processes is not mandatory for programming but your code will be clean, readable, easy to debug and understandable.
Label Naming
- We have two kinds of wordings in a language.
- First, the words provided by language creators are called keywords.
- Second, the label created by programmers who develop programs in language.
- In every program there are two types of labels created by the programmer.
- Variable Names
- Function Names
- You can use any valid name for your labels like, a, b, c, x, y, z.
- It is good practice to use proper names for your labels.
a = 10price = 10
Code 1: Bad vs Good Name of a Variable
- Both names are valid according to Python syntax but variable a is a bad name because we cannot find what is the purpose of it while price is a good name because it indicates a proper objective to use of this variable.
- There are three popular conventions for naming in Python (defined by PEP-8).
- Snake Case
- Camel Case
- Upper Case
- In the snake case we use _ (underscore) between the words of variable names such as student_name.
- This naming convention is valid for functions and variables.
- If a variable name consists of a single word, then it will remain in small letters like print, total.
- In camel casing we use capital of the starting letter of each word such as LinearRegression.
- This naming convention is suitable for class names or exceptions.
- Upper case convention uses for constant values. Constants are also variable but they will change their value during program execution.
- For example, we can use PI for pi value, EULER_NUMBER etc.
- There are some tools also available that make our code according to proper styles like Black, Pylint, Flake8, AutoPep8, etc.
Spacing
- Using a proper space makes code more readable.
- Again, there are no rules enforced by Python about it but some conventions as following.
- Add space before and after an operator. For example, code on line 2 is clean but code on line 1 is messy.
step=1step = 1
Code 2: Congested and Clean Code
- Use space after comma
my_list = [1, 2, 3, 4, 5]
Code 3: Space after Comma
Comments
- Comments are explanations of code in simple words.
- We can even write these comments in our own language rather than English.
- Comments start with #.
- All wording after # signs will never be interpreted. The language processor just ignores it.
- The # signs only work for a single line.
EU = 2.7 #EU is universal constant for Euler Number
Code 4: Writing Comments that Explain the Code
Other Practices
- KIS Rule: Keep It Simple. Make the code simple by using short-hand style
- DRY Rule: Write code in a way that it should not require to write again.
- Use Modular Approach: Write code in the form of functions as much as possible.
- Use try-except: If you are afraid to raise any exception, use try-except.








