Los Techies : Blogs about software and anything tech!

Refactoring Day 23 : Introduce Parameter Object


This refactoring comes from Fowler’s refactoring catalog and can be found here

Sometimes when working with a method that needs several parameters it becomes difficult to read the method signature because of five or more parameters being passed to the method like so:

   1: public class Registration
   2: {
   3:     public void Create(decimal amount, Student student, IEnumerable<Course> courses,
   4:         decimal credits)
   5:     {
   6:         // do work
   7:     }
   8: }

In this instances it’s useful to create a class who’s only responsibility is to carry parameters into the method. This helps make the code more flexible because to add more parameters, you need only to add another field to the parameter object. Be careful to only use this refactoring when you find that you have a large number of parameters to pass to the method however as it does add several more classes to your codebase and should be kept to a minimum.

   1: public class RegistrationContext
   2: {
   3:     public decimal Amount { get; set; }
   4:     public Student Student { get; set; }
   5:     public IEnumerable<Course> Courses { get; set; }
   6:     public decimal Credits { get; set; }
   7: }
   8:  
   9: public class Registration
  10: {
  11:     public void Create(RegistrationContext registrationContext)
  12:     {
  13:         // do work
  14:     }
  15: }

This is part of the 31 Days of Refactoring series. For a full list of Refactorings please see the original introductory post.

Kick It on DotNetKicks.com
Posted Aug 23 2009, 07:06 AM by schambers

Comments

Michael Kjörling wrote re: Refactoring Day 23 : Introduce Parameter Object
on 08-23-2009 7:58 AM

You also need to be very careful with input validation since (especially when adding fields to the parameter object class that is already used in multiple places throughout your codebase) it is very easy to forget one field/parameter out of the bunch. Much more so than if you need to go through the parameter list when calling the method; in that case, if you miss a spot, the compiler will alert you. In some cases it might even be worth creating a method on the RegistrationContext class such as Create() that takes a Registration object and calls Create() on that after verifying that all fields are properly filled in. This is a slight deviation from the separation of responsibility principle but since the RegistrationContext class cannot exist on its own anyway, it seems like it could be an acceptable trade-off. Something like this:

public class RegistrationContext

{

   // ... parameter fields go here ...

   public void Create(Registration registration)

   {

       // ... validate all context class fields and fail if appropriate ...

       registration.Create(this);

   }

}

schambers wrote re: Refactoring Day 23 : Introduce Parameter Object
on 08-23-2009 8:10 AM

Excellent point Michel. This leads into Day 25 where I talk about Design By Contract checks that would be an excellent example of input validation in a scenario like this.

ryzam wrote re: Refactoring Day 23 : Introduce Parameter Object
on 08-24-2009 12:17 AM

I think its time to thing parameter object as a message.Using message it would give you Ubiquitous Language which minimize the gap between domain expert and developer

Reflective Perspective - Chris Alcock » The Morning Brew #418 wrote Reflective Perspective - Chris Alcock &raquo; The Morning Brew #418
on 08-24-2009 3:35 AM

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #418

张荣华 wrote 31天重构指南之二十三:引入参数对象
on 10-16-2009 1:37 AM

“引入参数对象”重构来自于Martin Folwers的重构目录,你可以在这里找到原始的描述。 有时当一个方法所需要的参数大于5个后,读懂该方法的签名就会变得比较困难,就像下面的代码: 1: publ...

31 Days of Refactoring « Vincent Leung's .NET Tech Clips wrote 31 Days of Refactoring &laquo; Vincent Leung&#039;s .NET Tech Clips
on 10-28-2009 9:28 AM

Pingback from  31 Days of Refactoring « Vincent Leung's .NET Tech Clips

PetterLiu wrote 31 Days of Refactoring
on 11-27-2009 4:02 AM

Refactoring Day 1 : Encapsulate Collection Refactoring Day 2 : Move Method Refactoring Day 3 : Pull ...

PetterLiu wrote 31 Days of Refactoring
on 11-27-2009 4:04 AM

Refactoring Day 1 : Encapsulate Collection Refactoring Day 2 : Move Method Refactoring Day 3 : Pull ...

Fluent interface | Info@ wrote Fluent interface | Info@
on 03-09-2010 5:53 AM

Pingback from  Fluent interface |  Info@

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems