Question

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?

Was it helpful?

Solution

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. 
} 

OTHER TIPS

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) {...}

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.

Regarding Skeets comment on my example above that he would use a class instead of a structure and maybe make it clearer where to use a class and where to use a structure i post this too. I think there are other out there who are curious about this too.

The main reason to use a class as I could see was you could make it immutable, but thats possible with structures too?

for example:

struct user 
{ 
public user(string Username, string LastName) 
{ 
    _username = Username; 
} 
private string _username; 
public string UserName { 
    get { return _username; } 
} 

}

I have long time felt that I dont know the differences anymore between classes and structures now when we can have propertys, initializers, fields and exactly everything that a class has in a structure too. I know classes are refernce types and structures are value types but what difference does it make in the case above when using it as a parameter in a function?

I found this description of the differences on the site http://www.startvbdotnet.com/oop/structure.aspx and that description is exactly how I mapped it in my head:

Structures can be defined as a tool for handling a group of logically related data items. They are user-defined and provide a method for packing together data of different types. Structures are very similar to Classes. Like Classes, they too can contain members such as fields and methods. The main difference between classes and structures is, classes are reference types and structures are value types. In practical terms, structures are used for smaller lightweight objects that do not persist for long and classes are used for larger objects that are expected to exist in memory for long periods.

Maybe this should be a own question but I felt it was related when we all had different views on the structure vs class-thing as parameter.

I assume you're using C# 4 and can just use named parameters:

FireEmployee(
    first: "Frank",
    middle: "",
    last: "Krueger",
    id: 338);

These make the code almost as readable as VB or Smalltalk. :-)

If not, I would go with what Dave Markle has to say.

If this is library code that will see a lot of use, and if some of the parameters have typical values that are candidates for default values, then you should consider Dave Markle's advice, and provide a selectio of overloads with progressively fewer parameters. This is the approach recommended in the Microsoft Framework Design Guidelines.

Alternately, you can get a similar effect with Stefan's approach, by setting default values with member initializers and using a progression of ctor overloads.

If you really don't want to pass in your arguments separately I would suggest creating a new class which encapsulates all of your arguments. You can (in Java and most likely in C#) declare a public inner class inside the class containing the gnarly method for this purpose. This avoids having classes floating around which are really just helper types.

I would say pass them individually as well. I don't like the idea of creating a class, then passing that class through as an argument. Its a form of stamp coupling, which means making changes will be harder since one class uses the other. And reusing one class means you would have to reuse the other as well.

You could use an interface to reduce stamp coupling, but that's too much overhead for my tastes, so that's why I like to pass the arguments individually.

Do you really need 8-9 parameters for a single function? It seems to me that if you need that many parameters, then you're probably doing too many different things in that function. Try refactoring the code into separate functions so that each function has exactly one purpose.

Do not pass them as an array unless the function acts on an array, I wouldn't create a new data structure either to group the parameters for the following reasones

  1. Passing a new data structure hides what the function really needs as input (does it need all the data structure/part of it?)

  2. Related to 1 it makes UTs more difficult (when writing a UT you need to recreate the entire data structure)

  3. If the input parameters are not related you end up with a new data structure that groups unrelated data types for no other reason than to make a function call look neater

  4. If you chose to pass the new data structure to your function the function can not be used in a scope where the new datastructure was defined

Really the only disadvantage to passing each paramater to the function is that you might not be able to fit the function in one line of code, but don't forget the lines you need before the function call in which you will fill up your data structure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top