Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regarding Passing Many Parameters

I have around 8-9 parameters to pass in a function which returns an array. I would like to know that its better to pass those parameters directly in the function or pass an array instead? Which will be a better way and why?

like image 324
Samiksha Avatar asked Dec 02 '08 04:12

Samiksha


3 Answers

If I would do anything, then it would be to create an structure that holds all parameters to get nice intellisence and strong names.

public struct user 
{ 
    public string FirstName; 
    public string LastName; 
    public string zilionotherproperties; 
    public bool SearchByLastNameOnly; 
} 
public user[] GetUserData(user usr) 
{ 
    //search for users using passed data and return an array of users. 
} 
like image 111
Stefan Avatar answered Nov 04 '22 17:11

Stefan


Pass them individually, because:

  • that is the type-safe way.
  • IntelliSense will pick it up in Visual Studio and when you write your calling functions, you will know what's what.
  • It is faster to execute that way.

If the parameter really IS the array, though, then pass the array. Example:

For functions which look like this, use this notation:

Array FireEmployee(string first, string middle, string last, int id) {...}

For functions that look like this, use the array:

Array FireEmployees(Employee[] unionWorkers) {...}
like image 37
Dave Markle Avatar answered Nov 04 '22 17:11

Dave Markle


Your scenario is covered by the Introduce Parameter Object refactoring in Martin Fowler's refactoring book. The book is well worth owning, but for those who don't, the refactoring is described here. There's also a preview on the publisher's site, and on Google books. It recommends replacing the parameters not with an array, but a new object.

like image 29
Don Kirkby Avatar answered Nov 04 '22 15:11

Don Kirkby