Is it OK to have a class with just properties for refactoring purposes?

Xaisoft picture Xaisoft · Nov 10, 2011 · Viewed 7.6k times · Source

I have a method that takes 30 parameters. I took the parameters and put them into one class, so that I could just pass one parameter (the class) into the method. Is it perfectly fine in the case of refactoring to pass in an object that encapsulates all the parameters even if that is all it contains.

Answer

McKay picture McKay · Nov 10, 2011

That is a great idea. It is typically how data contracts are done in WCF for example.

One advantage of this model is that if you add a new parameter, the consumer of the class doesn't need to change just to add the parameter.

As David Heffernan mentions, it can help self document the code:

FrobRequest frobRequest = new FrobRequest
{
    FrobTarget = "Joe",
    Url = new Uri("http://example.com"),
    Count = 42,
};
FrobResult frobResult = Frob(frobRequest);