Inappropriate coding practices - Unused properties - Dart

Inappropriate coding practices - Unused properties - Dart

Need

Elimination of unused properties in the codebase

Context

  • Usage of Dart for building cross-platform mobile, web, and desktop applications
  • Usage of Shelf for managing and organizing data in a Node.js application

Description

Non compliant code

        import 'package:shelf/shelf.dart' as shelf;

class UnusedProperties {
  final String unusedString;
  final int unusedInt;

  UnusedProperties(this.unusedString, this.unusedInt);

  shelf.Response handleRequest(shelf.Request request) {
    return shelf.Response.ok('Hello, World!');
  }
}
        
        

In the above Dart code, we have a class UnusedProperties that defines two properties: unusedString and unusedInt. These properties are initialized through the class constructor but are not used anywhere in the class methods or elsewhere in the code.

This is an example of inappropriate coding practices where unused properties are defined. These unused properties add unnecessary complexity to the source code and can lead to a suboptimal state that may affect the overall application performance.

In the worst-case scenario, these unused properties could consume memory and other system resources, slowing down the application and causing a poor user experience. Furthermore, it makes the code harder to read and maintain, as other developers may spend time trying to understand why these properties are defined and where they are used.

Therefore, it is recommended to remove any unused properties to improve the code quality and performance of your application.

Steps

  • Identify the unused properties in the class. In this case, 'unusedString' and 'unusedInt' are the unused properties.
  • Remove the unused properties from the class definition. This includes removing them from the class constructor as well.
  • Ensure that the removal of these properties does not affect other parts of the code. If these properties are referenced elsewhere, you will need to remove or modify those references.
  • Test the application to ensure that it still works as expected after the removal of the unused properties.

Compliant code

        import 'package:shelf/shelf.dart' as shelf;

class UnusedProperties {

  UnusedProperties();

  shelf.Response handleRequest(shelf.Request request) {
    return shelf.Response.ok('Hello, World!');
  }
}
        
        

The original code had two properties, unusedString and unusedInt, that were defined but not used anywhere in the class. This is considered a bad coding practice as it adds unnecessary complexity to the code and can potentially affect the performance of the application.

To fix this vulnerability, the unused properties were removed from the class definition. This included removing them from the class constructor as well.

The UnusedProperties class now only contains the handleRequest method, which is responsible for handling requests. The class is now simpler and more efficient, as it does not contain any unused properties.

After making these changes, it's important to test the application to ensure that it still works as expected. If the unused properties were referenced elsewhere in the code, those references would need to be removed or modified. However, in this case, the properties were not used anywhere else, so no further changes were necessary.

References