-2

what is the best approach to pass information to a function? should we pass a list of parameters? or we should create a class and just pass the instance of that class, so that whenever, there's a change in the parameter list we don't have to change all the flow?

Passing the class variable:

public class FooParams
{
    string Name{get;set;}
    string Password{get;set;}
}

public void GetFoo(Foo foo)
{
}

Passing a list of parameters

public void GetFoo(string name, string password)
{
}
David Pilkington
  • 13,528
  • 3
  • 41
  • 73
Asad Mehmood
  • 514
  • 2
  • 12

3 Answers3

2

There are many things to consider here.
It depends on the context, how many parameters, readability, immutability, and even personal preferences.

Here are just few cases to consider:

  • If it's just simple string and int parameters that you are passing in, then maybe you should just pass them like void foo (string s, int i)

  • If You need to mutability on these values, then you can wrap them in a class and pass it in.

  • If these parameters always come together, then consider wrapping them up in a class or struct:

    void foo(int height, int width)
    void foo(Size size)
    
  • If there are many different parameters, you might consider wrapping them up in a DTO or two and pass it for better readability. For example:

    void SendTo (string firstName, string LastName, string street, int apartmentNumber, string City)
    void SendTo (Person person, Address address)
    
Ofir Winegarten
  • 9,215
  • 2
  • 21
  • 27
0

IMO, passing a list of parameters is the way to go in your case, sometimes you want to generate a compile time errors instead of hunting down for all usages when you add something.

If you have a large list of parameters, then you can create a struct in order to copy by value (no need for a class - reference types).

And what you are looking at as an alternative to a FooParams are optional parameters:

public void GetFoo(string name, string password, int somethingNew = 0)
{
}

By using optional parameters, you can actually specify what is mandatory and what is optional for this function. This approach is often used instead of implementing and maintaining half a dozen of overloads for example.

Zein Makki
  • 29,485
  • 6
  • 52
  • 63
0

You shouldn't create a class/struct just for the sake of bundling parameters into a single object unless you expect to use that parameter object in other places. In your example, if it's just for a single method, then using parameters would be simplest both in terms of design and performance:

public void GetFoo(string name, string password)
{
    /// Do stuff
}

If, however, this is part of a library of methods that can inter-operate with each other, then you could consider making a struct as an information-holder. (This is, put simply, the purpose of the Point struct in the System.Drawing namespace.)

(I should point out that, while it's true using parameters instead of a class/struct is better for performance both in the sense that the program will execute faster and take up less memory, the practical difference is entirely negligible. The design aspect is still a valid concern, though.)

Abion47
  • 22,211
  • 4
  • 65
  • 88