Skip to main content

Inappropriate coding practices - Unused properties

Need

Elimination of unused properties in the codebase

Context

  • Usage of Java 8 for developing applications with enhanced features and performance
  • Usage of javax.servlet-api for Java Servlet development

Description

Non compliant code

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UnusedPropertiesServlet extends HttpServlet {

private String unusedProperty;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String param = request.getParameter("param");
response.getWriter().write("Parameter received: " + param);
}
}

In the above code, we have a class UnusedPropertiesServlet that extends HttpServlet. The class has a private property unusedProperty which is declared but never used in the class.

This is an example of inappropriate coding practices where unused properties are defined. The unusedProperty is adding unnecessary complexity to the source code and could potentially lead to a suboptimal state that may affect the overall application performance.

Unused properties can also potentially lead to confusion for other developers who may be working on the same code, as they may spend time trying to understand why the property is there and how it is used. This could lead to wasted time and resources, and potentially introduce new bugs if the property is mistakenly used.

It is generally a good practice to remove unused code and properties to keep the codebase clean, simple, and efficient.

Steps

  • Identify and remove any unused properties in the code.
  • Review the code to ensure that all properties are being used appropriately.
  • Consider refactoring the code to remove any unnecessary complexity.
  • Perform testing to ensure that the changes do not affect the overall application functionality.

Compliant code

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UnusedPropertiesServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String param = request.getParameter("param");
response.getWriter().write("Parameter received: " + param);
}
}

The original code contained an unused property unusedProperty. This property was defined but never used in the code, which added unnecessary complexity and could potentially affect the overall application performance.

In the fixed code, the unused property unusedProperty has been removed. This simplifies the code and reduces the risk of potential performance issues.

After removing the unused property, it's important to review the rest of the code to ensure that all other properties are being used appropriately. If there are other properties that are not being used, they should also be removed.

Finally, after making these changes, it's important to perform testing to ensure that the removal of the unused property does not affect the overall functionality of the application. This could involve unit testing, integration testing, or other types of testing depending on the specific application and its requirements.

References