DEV Community

Cover image for 10 Python Code Habits You Must Avoid for Efficient Programming
Abhay Parashar
Abhay Parashar

Posted on

10 Python Code Habits You Must Avoid for Efficient Programming

Python has gained immense popularity among developers worldwide due to its readability and extensive range of libraries and frameworks. However, like any programming language, Python is not immune to mistakes and pitfalls.

In this article, we’re going to explore 10 examples of bad code habits in Python. These examples will showcase common writing errors, inefficient practices, and potential pitfalls that can cause problems and make your Python code less efficient. Whether you’re a coding newbie or a seasoned programmer, this article is packed with examples of what NOT to do when writing code in Python.

1. Mixing different naming conventions

Mixing different naming conventions to define different parts, such as using a camel case for functions and a Pascal case for variables makes the code inconsistent and harder to read.

def myFunction(num):
    MyVar = num/3.5
    return MyVar
Enter fullscreen mode Exit fullscreen mode

It’s better to follow a consistent naming convention throughout the source code to improve code readability and maintainability. One of the most used and widely adopted conventions is snake_case where identifiers are written in lowercase with underscores separating different words.

def my_function(num):
    my_var = num/3.5
    return my_var
Enter fullscreen mode Exit fullscreen mode

2. Ignoring code comments

Leaving incomplete or misleading comments, or not providing any documentation can impact the understandability of the code and create confusion among fellow developers.

import re
text = "abc@xyz.com for further information."+\
        "You can also give feedbacl at feedback@xyz.com"

emails = re.findall(r"[a-z0-9\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+", text)
print (emails)
Enter fullscreen mode Exit fullscreen mode

Clear comments and comprehensive documentation provide essential context and ensure smooth collaboration between developers. It also reduces the time and effort required for understanding the code. By documenting the code properly developers can ensure that future changes and updates can be implemented efficiently, which leads to a more robust and maintainable end product.

Read More

Top comments (0)