Inappropriate coding practices - Unused properties - Python

Inappropriate coding practices - Unused properties - Python

Need

Elimination of unused properties in the codebase

Context

  • Usage of Python 3 for scripting and application development
  • Usage of Django for building web applications in Python

Description

Non compliant code

        from django.db import models

class User(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField(unique=True)
    password = models.CharField(max_length=100)
    unused_property = models.CharField(max_length=200)  # Unused property

    def __str__(self):
        return self.name
        
        

The above code defines a Django model User with four properties: name, email, password, and unused_property.

The name, email, and password properties are used to store user's information while the unused_property is not used anywhere in the application.

The presence of this unused_property adds unnecessary complexity to the code and can potentially lead to decreased performance of the application.

This is because every time a User object is created, Django also allocates memory for unused_property, despite it not being used. This can lead to increased memory usage and slower performance, especially when dealing with a large number of User objects.

Furthermore, unused properties can also lead to confusion for other developers who may be working on the codebase, as they may spend time trying to understand why the property is there and where it is used.

Steps

  • Identify the unused properties in your models. In this case, 'unused_property' is not being used.
  • Remove the unused properties from the model definition. This includes both the property declaration and any references to it in methods or functions.
  • Run your tests to ensure that removing the property does not break any functionality.
  • Commit your changes and document the reason for the removal in the commit message.

Compliant code

        from django.db import models

class User(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField(unique=True)
    password = models.CharField(max_length=100)

    def __str__(self):
        return self.name
        
        

The original code had a property named 'unused_property' in the User model which was not being used anywhere in the code. This unused property was adding unnecessary complexity to the source code and could potentially affect the overall application performance.

In the revised code, the 'unused_property' has been removed from the User model. This simplifies the model definition and eliminates the potential performance issue.

After making this change, it's important to run all tests to ensure that removing the property does not break any functionality. If all tests pass, the change can be committed. The commit message should document the reason for the removal of the 'unused_property'. This will help future developers understand why this change was made.

References