Reduce the number of arguments

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
(New page: A method with a large number of arguments can be very hard to read, such a method should be considered for redesigning. A method which requires large number of arguments can be break down ...)
 
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
A method with a large number of arguments can be very hard to read, such a method should be considered for redesigning. A method which requires large number of arguments can be break down into several methods with smaller arguments. Another way to overcome this problem is to encapsulate all of the arguments into a separate entity, for instance a class, then pass the class around rather than individual attributes.
+
A method with a large number of arguments can be very hard to read; such a method should be considered for redesign. A method which requires a large number of arguments can be broken down into several methods with fewer arguments. Another way to overcome this problem is to encapsulate all of the arguments into a separate entity, for instance a class, then pass the class around rather than the individual attributes.
  
The creation method is made as an exception for this role.
+
Johnson and Foote argue that any method with more than around six parameters is hard to read and the number of arguments should therefore be reduced. This will not only make the method more readable but will also increase the likelihood of it being similar to other methods so that it can be given the same name as similar methods. Johnson and Foote mention creation method (constructors) as an exception to this rule.
 +
 
 +
== See Also ==
 +
* [[Long parameter list smell]]
 +
 
 +
[[Category:Johnson and Foote's heuristics]]

Latest revision as of 01:00, 20 August 2009

A method with a large number of arguments can be very hard to read; such a method should be considered for redesign. A method which requires a large number of arguments can be broken down into several methods with fewer arguments. Another way to overcome this problem is to encapsulate all of the arguments into a separate entity, for instance a class, then pass the class around rather than the individual attributes.

Johnson and Foote argue that any method with more than around six parameters is hard to read and the number of arguments should therefore be reduced. This will not only make the method more readable but will also increase the likelihood of it being similar to other methods so that it can be given the same name as similar methods. Johnson and Foote mention creation method (constructors) as an exception to this rule.

See Also

Personal tools