Skip to main content

Inappropriate coding practices - Unnecessary imports

Need

Elimination of unnecessary imports to improve application performance and reduce resource consumption

Context

  • Usage of C# for building robust and scalable applications
  • Usage of Microsoft.AspNetCore.Mvc for building web applications with ASP.NET Core
  • Usage of Microsoft.Extensions.Logging for logging and tracing in .NET applications

Description

Non compliant code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using UnusedNamespace; // This is the unnecessary import

namespace WebApplication.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly ILogger<WeatherForecastController> _logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}

[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
}

The above code is a simple ASP.NET Core application that has a controller named WeatherForecastController. This controller has a Get method that returns weather forecasts.

However, the code has a vulnerability due to inappropriate coding practices. The application unnecessarily imports a namespace UnusedNamespace which is not used anywhere in the code.

This is a bad practice because it loads modules that will not be used, and doing so unnecessarily increases the load. This could potentially slow down the application, especially if the unused import is a large library or module. It also makes the code harder to read and maintain, as other developers may be confused about why the import is there if it's not being used.

Steps

  • Identify and remove any unnecessary imports in the code.
  • Review the code and determine which modules are not being used.
  • Remove the unnecessary imports to reduce the load on the application.
  • Ensure that the code still functions correctly after removing the unnecessary imports.

Compliant code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace WebApplication.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly ILogger<WeatherForecastController> _logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}

[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
}

The original code contained an unnecessary import statement (using UnusedNamespace;). This import was not being used anywhere in the code, and thus was unnecessarily increasing the load on the application.

The fixed code removes this unnecessary import, reducing the load on the application. The rest of the code remains the same, and the functionality of the application is not affected by this change.

It's important to regularly review your code and remove any unnecessary imports to ensure your application runs as efficiently as possible.

References