Long Parameter List

Description (What)

A method which has greater than 3 or 4 parameters in its parameter list

How to Locate it (Where)

Look for constructors, methods that take in more than 3 or 4 parameters,

especially parameters that are logically related and can be made part of the same object.

How It Manifests (Why)

Sometimes long parameter lists can manifest from refactoring and moving parameters, fields and variables around in a way that now methods that relied on fields now need to be given the same variable through its parameters.

Long parameter lists can also manifest because a method does more than required and can be further broken down into two or more methods to maintain more modular and maintainable methods.

How to Fix It (Possible Treatments)

By using the Introduce Parameter Object treatment, we created an object out of the logically similar parameters. This treatment can lead to the creation of Lazy Class or Data Class code smells so it is important to remember the consequences of introducing objects and classes.

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

Examples

Example 1

Before:

The method calculateDistance has 4 parameters (2 of which are logically similar)

Observed Code Smells:
- Long Parameter List (line 4)

After:

Applied 'Introduce Parameter Object' and added a LocationCoordinates class to hold the values with getters.

Refactoring Applied:
- Long Parameter List
    - Introduce Parameter Object (LocationCoordinates)
Observed Code Smells After Refactoring:
- None

Example 2

Before:

The logic of activatePromotion is there but instead of calling the relevant method to catch the logic,

it passes new unnecessary parameters.

After:

Applied Replace Parameter with Method Call to get rid of the long parameter list but preserve the logics.

Refactoring Applied:
- Long Parameter List
    - Replace Parameter with Method Call (line 45-66)
Observed Code Smells After Refactoring:
- None

When to Ignore

Don't shorten parameter lists if doing so causes unwanted dependencies between classes

More

More about Long Parameter Lists

001  class Shipment {
002   private final int EARTHRADIUS;
003  
004   public double calculateDistance(double originLat, double originLong, double destLat, double destLong) {
005   double p = Math.PI / 180;
006   double a = 0.5 - Math.cos((destLat - originLat) * p) / 2
007   + Math.cos(originLat * p) * Math.cos(destLat * p)
008   * (1 - Math.cos((destLong - originLong) * p)) / 2;
009   return 2 * this.EARTHRADIUS * Math.asin(Math.sqrt(a));
010   }
011  
012   public Shipment() {
013   this.EARTHRADIUS = 6371;
014   }
015  }
016  
017  public class LPLBE1 {
018   public static void main(String[] args) {
019   Shipment cst = new Shipment();
020   System.out.println(cst.calculateDistance(42.3555, 71.0565, 40.7128, 74.0060));
021   }
022  
023  }
024