Rémi
Rémi

Reputation: 3967

What is the best practice concerning overload

I wonder what is the best practice or at least what is more standar about overload. I'm using c# 3.5 so I don't have optional parameter. Let say I have the following method :

Foo(string param1, string param2)
{
    SqlConnection connection = SM.Program.GetConnection();
    SqlCommand command = connection.CreateCommand();

    command.CommandText = "UPDATE Table" +
                          "SET Pla = @pla " +
                          "WHERE Foo = @foo";

    try
    {
        command.Parameters.AddWithValue("@pla", param1);
        command.Parameters.AddWithValue("@foo", param2);
        connection.Open();
        command.ExecuteNonQuery();
    }
    finally
    {
        connection.Dispose();
        command.Dispose();
    }
}

And that I need an overload with another parameter, let say an sqltransaction

Foo(string param1, string param2, SqlTransaction trans)

The second method would basically be the same but it would do the operation in a transaction

Now I'm wondering what should I do? Only 1 method that accep a null argurment as the transaction and in that case don't use any or 2 methods but pretty mutch copy/paste the code in except for the transaction?

What is considered the best practice for that kind of thing?

EDIT : I think the general idea seem to do the chaining between overload. But I still wonder in that case isn't accepting null argument in a method is a kind of bad thing?

Upvotes: 4

Views: 4320

Answers (3)

Bas
Bas

Reputation: 27095

Going by the DRY principle, you should never copy-paste code.

Therefore, having two methods like this, you should always have some kind of chaining.

public void Foo(string param1, param2) {
       Foo(param1, param2, null);
}

public void Foo(string param1, string param2, SqlTransaction trans) {
      //do stuff, handle null value for trans
}

Update regarding default parameters

Supplying a default parameter with a single method instead of overloading could be considered bad practice in some situations, especially when writing libraries:

//assembly1.dll
public void Foo(int a, int b, int c = 3) { ... }

//assembly2.dll
void Bar() {
     Foo(1,2);
}

What happens is that the compiler replaces the call Foo(1,2) with Foo(1,2,3).

Now, let's say we want to change the default for c to 4 and update assembly1.dll without changing assembly2.dll.

We would expect to call Foo(1,2,4) but in reality, Foo(1,2,3) is still invoked, because the default value is stored in the caller's location!

By using overloads, the default value 3 is stored in assembly1.dll, where it belongs.

Upvotes: 15

ebram khalil
ebram khalil

Reputation: 8321

let's suppose that your function take x arguments. With just one function then you 'll need x if else to check what to do if the specific argument value is null or not.

Method overloading is more much cleaner and readable.

Upvotes: 0

mario
mario

Reputation: 1248

I think overloading is clearer than passing null for some arguments. I would implement it like this:

Foo(string param1, string param2)
{
    Foo("param", "param", null);
}

Foo(string param1, string param2, SqlTransaction trans)
{
    //Do stuff...
}

This way you do not have to copy and paste code.

Upvotes: 1

Related Questions