Tony Stark
Tony Stark

Reputation: 156

Passing strings vs class object as argument to a function

I need to pass two values to a Function

both these values are in a class Object

i have two options

  1. passing the required values as string
  2. passing class object

Problem: Class object has many members other than what i want

Question: Which is the best option?

Example

This function is using for database query

FunctionName(string opt1,string opt2) 
{
    using (SqlConnection connection = this.Connection)
    {
        connection.Execute("select count(*) from table where opt1 = opt1 and opt2 = opt2", new { opt1 = opt1, opt2 = opt2 });
    }
}

or

FunctionName(className obj)
{
    using (SqlConnection connection = this.Connection)
    {
        connection.Execute("select count(*) from table where opt1 = opt1 and opt2 = opt2", new { opt1 = obj.opt1, opt2 = obj.opt2 });
    }
}

Function calls are like below

 class className {
 public string opt1;
 public string opt2;
 public string opt3;
 public string opt4;// and more
 }

 className obj1

 FunctionName(obj1.opt1,obj1.opt2) Or
 FunctionName(obj1) 

Upvotes: 1

Views: 2610

Answers (2)

Kjartan
Kjartan

Reputation: 19151

I would say this is sort of opinion-based, but I`ll still attempt an answer: My rule of thumb is that if there are few arguments (no more than three - preferably no more than two), then the arguments can be passed directly. Just because that is the simplest solution in most cases.

If you have more than three arguments, you should consider adding a class just for plain clarity. As you point out however, you might not want to pass a class with lots of extra values. If so, you have several options, eg.:

  • Create a new class specifically for this purpose, which has exactly the members you need to transfer
  • Create a new base class that your main class can inherit from, and move the members you want to expose up into this class. Keep the members you do not want to expose in your original class. Now you can pass the new base class with the stuff you need, and use the sub class internally, with the extra stuff that your function does not need to know about.

Upvotes: 1

Luaan
Luaan

Reputation: 63772

3 - Passing a new class / structure, specifically designed for the job :)

However, if you only care about efficiency, not security, passing a class reference doesn't depend on how many fields the class has. Passing a class with no fields at all takes exactly as much time as passing a class that has 100 MiB of data. Unless it's a struct.

Don't serialize classes to a string and back again just to pass them to a method. That's just a bad idea all around. You lose type safety, it's going to be expensive, and the compiler loses a lot of options to warn you that you're doing something wrong.

Of course, if you just pass the parameters, this needn't matter. This can have some benefits, mostly in ensuring that (as long as you don't use any mutable reference types) you're also going to stay thread-safe, and it limits possible side-effects (e.g. from modifying the passed class - it's not a copy, it's the same instance; again, unless you're passing a struct).

Upvotes: 2

Related Questions