Return to site

CODE SMELL: The long parameter list ๐Ÿ“

January 7, 2023

The long parameter list code smell ๐Ÿ‘ƒ happens when method signatures require long lists of parameters. They may include, among other things, flag ๐Ÿšฉ parameters, which are a specially bad ๐Ÿ‘Ž pattern. Flag parameters are used solely to determine which conditional path the method must execute, and make it much harder to figure out the method's overall behavior and intention.

 

Why is it bad? ๐Ÿ‘Ž

Long parameter lists are harder ๐Ÿ˜ซ to maintain. For example, making intermediary parameters optional is a specially difficult task to achieve in most languages.

If we pass parameters in the wrong ๐Ÿ”ƒorder, we may very likely break ๐Ÿ”จ the function. It becomes increasingly difficult to figure out a function's invocation the longer its parameter list gets.

 

How to fix it? ๐Ÿ’ก

Substitute data parameters with a single data object ๐ŸŽ parameter.

If it's possible to obtain information about another parameter from a parameter, remove โŒ the additional parameter.

Avoid passing flag ๐Ÿšฉ parameters.