tags:

views:

33

answers:

3

I asked a fellow colleague yesterday if a function has too many parameters whether it would be better to create a class with properties instead. Are there any guidelines that I can follow?

+2  A: 

I think that might depend on the language you are using, and the number of parameters in question, and are some of them allowed to be left out when calling the function.

VB has optional params, and C#3+ will allow for instantiation using params.

Will the new class have any other use except running that function, or will the state of that class be relevant later in the code?

astander
+1  A: 

When the number of parameters exceeds 5 I usually start thinking about refactoring the method. There's no absolute number, but this is my general rule. It may make sense to group the data in a data class, or sometimes it means that I should move the method to be closer to the data.

brianegge
A: 

It all depends on the context.

Eg.

  1. If it is not a database operations we can do as per the design of the system. Break the module and try to create sub modules.

  2. If it is a database system I always prefer to write a separate bean class for the Fields and DAO class for operations.

Umesh Aawte