DevPinoy.org
A Filipino Developers Community
   
Long parameter lists
I don't know if I'm just biased but I really don't like having long parameter lists in functions/methods. It turns me off to the point of avoiding use of such a function if the parameter list requires five or more parameters. Four seems to be pushing it already.

What's your take on this?


Posted 02-26-2006 11:59 PM by cruizer

Comments

bonskijr wrote re: Long parameter lists
on 02-27-2006 1:14 AM
same here, case in point in a DAL it is not uncommon to have a save method w/ params like this:

public bool Save(int empNo, string firstName, string lastName, string gender, int positionCode)
{
...
}

so instead of remembering all the params, I use the object itself to be passed"
public bool Save (Employee employee)
{
...
}

Although I like optional parameters(vb.net) I wouldn't like to burden a method with lots of parameters. Having said that however, using stored proc you can't have the same save method as the second one and that you have to pass every value to it. Hopefully future version of Sql will let you pass the table datatype to at least have that same functionality.
jstangroome wrote re: Long parameter lists
on 02-27-2006 2:05 AM
I don't like long parameter lists because I like to be able to read code on a "normal" resolution without scrolling sideways. I also don't like using VB's line continuation characters due to the default indentation behaviour and the way the background compiler handles continued lines. As a result I avoid long parameter lists at all costs.

Microsoft has handled the situation well with their EventArgs class. Now each event just has a sender and an eventArgs parameter instead of some of the long lists I remember from VB6 days.

As bonskijr mentioned, the DAL is a common place to find these long lists. I haven't tried implementing it yet but SQL Server 2005 allows you to use .NET classes as a user-defined data type. Perhaps you could pass a custom class to a stored procedure in the same manner as EventArgs.
jokiz wrote re: Long parameter lists
on 02-27-2006 3:46 AM
i remember chris (gwapo) introduces a struct just to encapsulte these primitive types.
cruizer wrote re: Long parameter lists
on 02-27-2006 5:15 AM
my sentiments, exactly. you'll be better off passing a DTO (data transfer object) than a long parameter list that nobody will remember without right-clicking on "Go to definition" :P
rdagumampan wrote re: Long parameter lists
on 02-27-2006 7:00 PM
yes i rather encapsualte those parameters into one or more ADTs than passing along as   plain strings and ints. Jokiz onced turn me down in using this style Delete(int employeePk), though it might be major modification to my exisitng code, I prefer to refactor my codes into Delete(Employee employee).

thanks.
cruizer wrote re: Long parameter lists
on 02-27-2006 9:53 PM
well better yet you can provide two versions (overloads) of the Delete() method to allow for either call
keithrull wrote re: Long parameter lists
on 02-28-2006 10:50 AM
Delete(int employeePk) i think is more simplistic than passing a whole class to the function since it would only use the integer variable. my usual approach on this is calling the delete method inside the class:

employee.Delete();

wherein employee is the class that has a method called Delete().

I would then access the EmployeeId defined inside my class execute my delete procudure.
cruizer wrote re: Long parameter lists
on 02-28-2006 11:58 PM
ah keithrull, that would work for you if you use something like the active record pattern. but if you use the DAO pattern, the Delete() method will have to be located outside the DTO (Employee) class.
keithrull wrote re: Long parameter lists
on 03-01-2006 5:17 PM
yeah.. i guess it all boils down to what pattern you are using. :)
Copyright DevPinoy 2005-2008