Long Parameter List

When a function or method has a large number of parameters.

Sign of Smell

The Long Parameter List is a code smell that occurs when a function or method has a large number of parameters. In general, if a method has more than three or four parameters, it is considered too long.

Reason of Smell

  • Reduced Readability: Methods with a long parameter list can be challenging to read and comprehend. Developers need to remember the order and purpose of each parameter, which can lead to confusion and mistakes.

  • Code Duplication: In some cases, methods with long parameter lists may result in code duplication, as developers might create multiple similar functions to avoid passing numerous parameters.

  • Increased Coupling: Methods with long parameter lists are tightly coupled to the calling code, as the calling code needs to know and provide all the required parameters. This can make it difficult to modify the function without impacting the calling code.

  • Violation of Single Responsibility Principle (SRP): Methods with many parameters may indicate that the function is trying to do too much and violates the SRP. Ideally, a function should have a clear and single responsibility.

  • Brittleness: When a function has many parameters, any changes to the order or type of parameters can break existing calls to that function, leading to bugs that are hard to detect.

  • Testing Challenges: Writing comprehensive unit tests for methods with long parameter lists can be cumbersome, as testers need to provide various combinations of values for each parameter to cover different scenarios.

  • Maintenance Difficulty: As the codebase evolves, adding new parameters to a function with a long parameter list becomes increasingly cumbersome and can lead to further code smells like Shotgun Surgery.

Refactoring Recipe

  • Replace Parameter with Method Call

  • Introduce Parameter Object

  • Preserve Whole Object

  • Remove Parameter

  • Replace Parameter with Explicit Methods

Reference

https://refactoring.guru/smells/long-parameter-list

https://refactoring.guru/replace-parameter-with-method-call

Last updated