Skip to main content

Inappropriate coding practices - Unnecessary imports

Need

Elimination of unnecessary imports to optimize resource usage

Context

  • Usage of Dart 2.12.0 for developing cross-platform applications
  • Usage of dart:convert for encoding and decoding data in Dart
  • Usage of dart:io for performing input/output operations in Dart
  • Usage of the shelf package in Dart for building web applications and HTTP middleware
  • Usage of Shelf for building HTTP servers in Dart with shelf_io.dart package
  • Usage of shelf_router.dart for creating routing logic in a Shelf server
  • Usage of an unused_module.dart package in Dart development

Description

Non compliant code

import 'dart:convert';
import 'dart:io';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'package:shelf_router/shelf_router.dart';
import 'package:unused_module/unused_module.dart';

void main() {
var app = Router();

app.get('/hello-world', (Request request) {
return Response.ok('Hello, World!');
});

var server = await io.serve(app, 'localhost', 8080);
print('Serving at http://${server.address.host}:${server.port}');
}

This Dart code is a simple backend server application using the Shelf framework. It includes the necessary imports for creating a server, defining routes, and handling requests.

However, there is a vulnerability in this code related to inappropriate coding practices - specifically, unnecessary imports. The line import 'package:unused_module/unused_module.dart'; imports a module that is not used anywhere in the code.

This is a bad practice because it loads the unused_module into memory when the application starts, even though the module is not used. This unnecessarily increases the memory footprint of the application and could potentially slow down the startup time.

In a larger application with many unused imports, this could lead to significant inefficiencies. Additionally, it can make the code harder to understand and maintain, as it's not clear why these modules are imported.

Steps

  • Identify the unused modules in your code. In this case, 'dart:convert', 'dart:io' and 'unused_module' are not used.
  • Remove the import statements for these unused modules from your code.
  • Ensure that the removal of these modules does not affect the functionality of your code. Test your code thoroughly after removing the unused imports.
  • Regularly review your code to check for unused imports and remove them to keep your code clean and efficient.

Compliant code

import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'package:shelf_router/shelf_router.dart';

void main() {
var app = Router();

app.get('/hello-world', (Request request) {
return Response.ok('Hello, World!');
});

var server = await io.serve(app, 'localhost', 8080);
print('Serving at http://${server.address.host}:${server.port}');
}

The updated code removes the unnecessary imports that were present in the original code. The modules 'dart:convert', 'dart:io' and 'unused_module' were imported but not used anywhere in the code. This is considered a bad practice because it loads modules that are not used, unnecessarily increasing the load.

The updated code only imports the modules that are actually used:

  • 'package:shelf/shelf.dart': This package provides the core functionalities for handling HTTP requests and responses in the Shelf framework.
  • 'package:shelf/shelf_io.dart': This package provides the IO adapter for the Shelf framework, which is used to serve the application.
  • 'package:shelf_router/shelf_router.dart': This package provides the router for the Shelf framework, which is used to define the application's routes.

By removing the unnecessary imports, the code becomes cleaner and more efficient. It's important to regularly review your code to check for unused imports and remove them. After removing unused imports, you should thoroughly test your code to ensure that the removal of these modules does not affect the functionality of your code.

References