Dead Code

Description (What)

Dead code is any piece of code (method, line, field) that is unused or unreachable.

How to Locate It (Where)

Most modern IDEs and Linters have inbuilt dead code detection for public methods and fields and gray out or highlight dead code.

In cases where a field or method is private, your IDE might not gray about dead code, in which case it is imperative to go through your class to make sure any private method or field is not going unused.

How It Manifests (Why)

Sometimes better, newer or simply different methods are created to superseed older methods. In these cases, the older method become dead code since they are not used anymore. Sometimes methods and fields or variables are created in anticipation of being used in the future but simply remain unused.

How to Fix It (Possible Treatments)

Simply delete the unused (dead) methods or fields or variables.

Other treatments are also possible based on the specific scenario, they can be found here

Examples

Example 1

Before:

Shipment class has dead method calculateDistance which has been

superseeded by method calculateDistanceFast and thus also making

method degToRad dead.

Observed Code Smells:
- Dead Code (lines 9-14, 27-29)

After:

Deleted unused dead methods calculateDistance and degToRad.

Refactoring Applied:
- Dead Code
    - Deleted unused methods (calculateDistance and degToRad)
Observed Code Smells After Refactoring:
- None

Example 2

Before:

The method calculate2018BlackFridayPrice is no longer applicable.

Need to be refactored or deleted.

Observed Code Smells:
-  Dead Code (line 34-50)

After:

Simple delete the code.

Refactoring Applied:
- Dead Code:
    - Remove Method (No longer exist).
Observed Code Smells After Refactoring:
- None

When to Ignore

None

More

More about Dead Code

001  class Shipment {
002   private final double VEHICLESPEEDKMPH;
003   private final int EARTHRADIUS;
004   LocationCoordinates originCity;
005   LocationCoordinates destCity;
006  
007   public double calculateDistanceFast() {
008   double p = Math.PI / 180;
009   double a = 0.5 - Math.cos((destCity.getLatitude() - originCity.getLatitude()) * p) / 2
010   + Math.cos(originCity.getLatitude() * p) * Math.cos(destCity.getLatitude() * p)
011   * (1 - Math.cos((destCity.getLongitude() - originCity.getLongitude()) * p)) / 2;
012   return 2 * this.EARTHRADIUS * Math.asin(Math.sqrt(a));
013   }
014  
015   public double calculateDistance() {
016   double dLat = degToRad(destCity.getLatitude() - originCity.getLatitude());
017   double dLon = degToRad(destCity.getLongitude() - originCity.getLongitude());
018   double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
019   Math.cos(degToRad(originCity.getLatitude())) * Math.cos(degToRad(destCity.getLatitude())) *
020   Math.sin(dLon / 2) * Math.sin(dLon / 2);
021   double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
022   double d = this.EARTHRADIUS * c;
023   return d;
024   }
025  
026   private double degToRad(double deg) {
027   return deg * (Math.PI / 180);
028   }
029  
030   public double calculateTimeInHours() {
031   return calculateDistanceFast() / this.VEHICLESPEEDKMPH;
032   }
033  
034   public Shipment(LocationCoordinates city1, LocationCoordinates city2) {
035   this.EARTHRADIUS = 6371;
036   this.VEHICLESPEEDKMPH = 55.4;
037   this.originCity = city1;
038   this.destCity = city2;
039   }
040  }
041  
042  class LocationCoordinates {
043   private double longitude;
044   private double latitude;
045  
046   public double getLatitude() {
047   return latitude;
048   }
049  
050   public double getLongitude() {
051   return longitude;
052   }
053  
054   public LocationCoordinates(double longitude, double latitude) {
055   this.longitude = longitude;
056   this.latitude = latitude;
057   }
058  
059   public double getRelativePositionToPoles(String direction) {
060   if(direction.equals("North")) {
061   return 90 - latitude;
062   } else if(direction.equals("South")) {
063   return 90 + latitude;
064   } else {
065   return 181;
066   }
067   }
068  
069  }
070  
071  public class DEDCBE1 {
072   public static void main(String[] args) {
073   LocationCoordinates boston = new LocationCoordinates(42.3555, 71.0565);
074   LocationCoordinates newYork = new LocationCoordinates(40.7128, 74.0060);
075   Shipment cst = new Shipment(boston, newYork);
076   System.out.println(cst.calculateTimeInHours());
077   System.out.println(boston.getRelativePositionToPoles("North"));
078   }
079  
080  }
081