Skip to content
Home / Fundamentals

Improving Readability and Maintainability with Meaningful Names in Python

Clarity

A good name should clearly and accurately describe the purpose or function of the element it represents. Avoid using abbreviations or acronyms that may not be familiar to all readers.

For example, consider the following code:

# Bad example
a = 5
b = 10
c = a + b

# Good example
num_students = 5
num_teachers = 10
total_people = num_students + num_teachers

In the bad example, the variables a, b, and c are not clear about their purpose or function. In contrast, the good example uses more meaningful names such as num_students, num_teachers, and total_people, which clearly and accurately describe their purpose.

Conciseness

Keep names as short as possible while still maintaining clarity. Long, cumbersome names can make code harder to read and understand.

For example, consider the following code:

# Bad example
number_of_students_enrolled_in_math_class = 5

# Good example
num_math_students = 5

In the bad example, the variable name is unnecessarily long and cumbersome, making it harder to read and understand. In contrast, the good example uses a shorter, more concise name that still maintains clarity.

Consistency

Use consistent naming conventions throughout your code. This can make it easier for others (and yourself) to understand and maintain the code.

For example, consider the following code:

# Bad example
student_count = 5
teacherCount = 10
TotalPeople = student_count + teacherCount

# Good example
student_count = 5
teacher_count = 10
total_people = student_count + teacher_count

In the bad example, the naming conventions are not consistent. Some names use camel case (e.g. teacherCount) while others use snake case (e.g. student_count). This can make the code harder to read and understand. In contrast, the good example uses a consistent naming convention (snake case) throughout the code.

Avoiding Ambiguity

Choose names that are specific and avoid using terms that could have multiple meanings.

For example, consider the following code:

# Bad example
x = 5
y = 10
z = x + y

# Good example
students = 5
teachers = 10
total = students + teachers

In the bad example, the variables x, y, and z are not specific and could refer to anything. In contrast, the good example uses more specific and meaningful names such as students, teachers, and total, which avoid ambiguity.

Context

Consider the context in which a name will be used. A name that makes sense in one context may not be appropriate in another.

For example, consider the following code:

# Bad example
items = [1, 2, 3, 4, 5]
for item in items:
    print(item)

# Good example
numbers = [1, 2, 3, 4, 5]
for number in numbers:
    print(number)

In the bad example, the variable items is not specific to the context in which it is used. The name items could refer to a list of any type of object, making it hard to understand the purpose of the code. In contrast, the good example uses the more specific and meaningful name numbers, which accurately describes the purpose of the variable in the context in which it is used.

It is important to choose names that are specific and appropriate for the context in which they are used. This can help make the code easier to understand and maintain.

Readability

Names should be easy to read and pronounce, especially if the code will be read aloud or reviewed by others.

For example, consider the following code:

# Bad example
fst_nm = "John"
lst_nm = "Doe"

# Good example
first_name = "John"
last_name = "Doe"

In the bad example, the variables fst_nm and lst_nm are difficult to read and pronounce. In contrast, the good example uses more readable and pronounceable names such as first_name and last_name.

Reusability

If a name is specific to a particular piece of code, it may not be appropriate to reuse it elsewhere in the codebase. Consider using more general names that can be reused in multiple contexts.

For example, consider the following code:

# Bad example
def calculate_student_grades(students):
    for student in students:
        # Calculate student grades
        student_grades = []
        for assignment in student.assignments:
            student_grades.append(assignment.grade)
        student.grades = student_grades

# Good example
def calculate_grades(people):
    for person in people:
        # Calculate grades
        grades = []
        for assignment in person.assignments:
            grades.append(assignment.grade)
        person.grades = grades

In the bad example, the name student_grades is specific to the context of calculating student grades and would not be appropriate to reuse elsewhere in the codebase. In contrast, the good example uses the more general name grades which can be reused in multiple contexts.

Common pitfalls avoid

Pitfall: Using Single-Letter Names

One common pitfall is using single-letter names for variables, especially if they do not have any meaning. Single-letter names can be confusing and make it hard to understand the purpose or function of the element they represent.

For example, consider the following code:

# Bad example
a = 5
b = 10
c = a + b

# Good example
num_students = 5
num_teachers = 10
total_people = num_students + num_teachers

In the bad example, the variables a, b, and c are single-letter names that do not have any meaning. In contrast, the good example uses more meaningful names such as num_students, num_teachers, and total_people, which clearly and accurately describe their purpose.

Pitfall: Using Ambiguous Names

Another common pitfall is using ambiguous names that could have multiple meanings. This can lead to confusion and make it hard to understand the code.

For example, consider the following code:

# Bad example
x = 5
y = 10
z = x + y

# Good example
students = 5
teachers = 10
total = students + teachers

In the bad example, the variables x, y, and z are ambiguous and could refer to anything. In contrast, the good example uses more specific and meaningful names such as students, teachers, and total, which avoid ambiguity.

Pitfall: Using Inconsistent Naming Conventions

Using inconsistent naming conventions throughout your code can make it harder to read and understand. It is important to choose a naming convention and stick to it consistently.

For example, consider the following code:

# Bad example
student_count = 5
teacherCount = 10
TotalPeople = student_count + teacherCount

# Good example
student_count = 5
teacher_count = 10
total_people = student_count + teacher_count

In the bad example, the naming conventions are not consistent. Some names use camel case (e.g. teacherCount) while others use snake case (e.g. student_count). This can make the code harder to read and understand. In contrast, the good example uses a consistent naming convention (snake case) throughout the code.