Argument Object

ArgumentObject is a pattern from JamesNoble's Arguments and Results paper. MartinFowler calls this pattern "IntroduceParameterObject" in his book RefactoringImprovingTheDesignOfExistingCode.

It kind of goes like this. You have a method which has too many arguments. Make a little ArgumentObject which bundles them and pass it to the method.

Why do this? Well, if you can come up with a good name for the ArgumentObject, it may make your code clearer. You can also reuse the ArgumentObject if you have several calls to the same method with almost but not quite the same values (CurriedObject). If the attributes of the ArgumentObject have good defaults, you may not have to set them all. Another option is to have a few canned ArgumentObjects hanging around that you can pass to the method when you need to. You might even move some behaviour into the ArgumentObject, because of a FeatureEnvySmell.

Also see RefactorParametersToMemberVariables and RefactorScopedVariableToParameter.


If you like compact, dynamic code, use an associative array.


Or for extra code-cleaning power, try the ArgumentInterface.

See also: ParameterObject


CategoryRefactoring


EditText of this page (last edited March 13, 2010) or FindPage with title or text search