Skip to main content

Inappropriate coding practices - Unnecessary imports

Need

Elimination of unnecessary imports to improve code efficiency and reduce resource consumption

Context

  • Usage of Scala for building scalable and functional applications
  • Usage of Play Framework for building web applications
  • Usage of play.api.mvc for handling HTTP requests and building web applications in Play Framework
  • Usage of play.api.data for handling form data in Play Framework
  • Usage of play.api.data.Forms for handling form data in Play Framework
  • Usage of play.api.libs.json for JSON parsing and manipulation in Play Framework
  • Usage of play.api.libs.functional.syntax for functional programming and JSON manipulation in Play Framework
  • Usage of play.api.i18n for internationalization and localization in Play Framework
  • Usage of java.io.File for file input/output operations in Java
  • Usage of java.net.URL for handling network connections and URLs in Java
  • Usage of scala.io.Source for reading and processing input sources in Scala

Description

Non compliant code

import play.api._
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import play.api.libs.json._
import play.api.libs.functional.syntax._
import play.api.i18n._
import java.io.File
import java.net.URL
import scala.io.Source

class HomeController @Inject()(val controllerComponents: ControllerComponents) extends BaseController {

def index() = Action { implicit request: Request[AnyContent] =>
Ok("Hello, World!")
}

}

In the above Scala code, we are importing several modules from the Play framework and some other libraries like java.io.File, java.net.URL, and scala.io.Source. However, in the HomeController class, we are only using a few of them.

The play.api._, play.api.mvc._, play.api.data._, play.api.data.Forms._, play.api.libs.json._, play.api.libs.functional.syntax._, and play.api.i18n._ imports are used in the code, but java.io.File, java.net.URL, and scala.io.Source are not being used at all.

This is an example of a bad coding practice - unnecessary imports. Importing modules that are not used increases the load unnecessarily. It can also lead to confusion for other developers who might be trying to understand the code, as they may spend time trying to figure out where and how the imported but unused modules are being used.

Steps

  • Identify and remove any unnecessary imports in the code.
  • Review the code and determine which modules are actually used.
  • Remove the imports of any modules that are not used.
  • Ensure that the application still functions correctly after removing the unnecessary imports.

Compliant code

import play.api.mvc._
import play.api.i18n._

class HomeController @Inject()(val controllerComponents: ControllerComponents) extends BaseController {

def index() = Action { implicit request: Request[AnyContent] =>
Ok("Hello, World!")
}

}

The original code had several import statements, many of which were not used in the actual code. This is considered a bad practice as it unnecessarily increases the load by importing modules that are not used.

The fixed code only imports the necessary modules:

  • play.api.mvc._ : This is necessary for the Action, Request, AnyContent, Ok and BaseController classes used in the code.
  • play.api.i18n._ : This is necessary for the @Inject() annotation used in the code.

All other imports have been removed as they were not used in the code. This makes the code cleaner, easier to read, and reduces unnecessary load.

Remember to always review your code and remove any unnecessary imports to keep your code efficient and maintainable.

References