====== Long Parameter List ====== ===== Alternative Names ===== * Too many parameters * Too many arguments ===== Context ===== * [[contexts:API Design]] * [[contexts:Implementation]] ===== Problem Statement ===== A method has a lot of parameters. ===== Description ===== * Methods with 0 and 1 arguments are fine * 2 parameters still good * 3 parameters can be considered OK * 4 and more parameters are usually too much ===== Causes ===== ===== Origin ===== {{page>resources:Refactoring#reference}} ===== Disadvantages ===== * [[principles:More Is More Complex|MIMC]] Obviously MIMC is violated by long parameter lists. * [[principles:Principle of Separate Understandability|PSU]]: Long methods are hard to read it is often necessary to look up what all the parameters mean. * [[principles:Murphy's Law|ML]]: Parameters can be easily misinterpreted or can be given in the wrong order. * [[principles:Easy to Use and Hard to Misuse|EUHM]]: Methods with long parameter lists are neither easy to use nor hard to misuse, they are just easy to write. * [[principles:Option-Operand Separation|OOS]]: Following OOS leads to short parameter lists. * [[principles:High Cohesion|HC]]: A long parameter list may be a sign of low cohesion. ===== Advantages ===== * [[principles:Keep It Simple Stupid|KISS]]: Introducing a method with many parameters is easy, adding a parameter is easy, neglecting refactoring is easy. Applying KISS here can be regarded sloppy or pragmatic, depending on the concrete situation. * [[principles:Generalization Principle|GP]]: Methods with many parameters are usually more potent than those with fewer parameters. But using GP to justify a long parameter list might also be a sign that the method should not have all these capabilities, i.e. it is lacking cohesion. ===== Refactorings ===== * [[refactorings:Replace Parameter With Method]] * [[refactorings:Introduce Parameter Object]] * [[refactorings:Preserve Whole Object]] ===== Anti-Pattern Collections ===== {{page>collections:Code Smells in "Refactoring"#Box}} ===== Examples ===== ==== Example 1: CreateProcess and how it is avoided ==== The Windows API function [[http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx|CreateProcess]] takes ten arguments most of which are even optional. The function is cumbersome to use and hard to read because of the large number of parameters. Developers tend to avoid the use of the function and use simpler ones like [[http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx|ShellExecute]] (which still has six parameters) instead, or write wrapper functions for it. Some might be even tempted to use [[http://msdn.microsoft.com/en-us/library/ms687393(VS.85).aspx|WinExec]] which is very easy to use but deprecated. In contrast to that the .NET method [[http://msdn.microsoft.com/de-de/library/system.diagnostics.process.start(v=vs.110).aspx|Process.Start]] can often be called with just one parameter. ===== Description Status ===== /* Choose one of the following and comment out the rest: */ /*[[wiki:Stub]]*/ [[wiki:Incomplete]] /*[[wiki:Complete]]*/ ===== Further Reading ===== * {{page>resources:Refactoring#reference}} ===== Discussion ===== Discuss this wiki article and the anti-pattern on the corresponding [[talk:anti-patterns:Long Parameter List|talk page]].