user3635120
user3635120

Reputation: 73

Refactoring Code, Make parameter optional with if

I have code, sometimes I need to fill "Parameter1" with something and sometimes not, could I refactoring the code so I dont need to repeat code like: Name = "Adam", with If or something?

 public class Address
{
    public string Name { get; set; }
    public string City { get; set; }
    public string Parameter1 { get; set; }
}

public class Program
{
    static void Main(string[] args)
    {

        var VariableParameter1_On = false;
        var address = new Address();
        if (VariableParameter1_On)
        {
            address = new Address
            {
                Name = "Adam",
                City = "Paris",
                Parameter1 = "Test"
            };
        }
        else
        {
            address = new Address
            {
                Name = "Adam",
                City = "Paris",
                Parameter1 = "Test"  //Make this optional with if?
            };
        }

        Console.ReadKey();

    }
}

Upvotes: 0

Views: 219

Answers (4)

Inagnikai
Inagnikai

Reputation: 291

You're going to need to instantiate the object with the values for Name and City in either case. The only optional part is Parameter1's value, or lack thereof.

Your current code instantiates an empty Address object, and then immediately re-assigns it. All you really need to do is instantiate a new Address with the values for Name and Address assigned, then assign Parameter1 in your if statement

public class Program
{
    static void Main(string[] args)
    {

        var VariableParameter1_On = false;
        var address = new Address { Name = "Adam", City = "Paris" };

        if (VariableParameter1_On)
            address.Parameter1 = "Test";


        Console.ReadKey();

    }
}

Upvotes: 1

Fons
Fons

Reputation: 1975

Setting the field to null might be what you are looking for (reference). We can also use an inline if statement.

Replace your Main method with:

var VariableParameter1_On = false;
var address = new Address
{
    Name = "Adam",
    City = "Paris",
    Parameter1 = VariableParameter1_On ? "Test" : null;
};

Console.ReadKey();

I assume that Parameter1 is a reference type (e.g. a class), which can be set to null, and not a value type (e.g. a struct or enum), which must be assigned a value.

Upvotes: 3

gatsby
gatsby

Reputation: 1229

Try to use types as often as you can just in sake of readability and good naming. Maybe you can try to encapsulate the check in a specific method too to improve readability.

static void Main(string[] args)
{
    bool isParamaterMandatory = true;
    Address address = new Address()
    {
        City = "Paris",
        Name = "Adam",
        Parameter1 = GetParameterIfNeeded(isParamaterMandatory)
    };

    Console.ReadKey();

}

private static string GetParameterIfNeeded(bool isNeeded)
{
    if (isNeeded)
    {
        return "Mandatory";
    }

    return null;
}

Upvotes: 0

fstam
fstam

Reputation: 699

Use a constructor with an optional parameter.

//Both these calling examples are fine.
Address a = new Address("Peter", "New york");
Address b = new Address("Peter", "New york", "some parameter");

public class Address
{
    public Address(string name, string city, string parameter1 = null)
    {
        Name = name;
        City = city;
        Parameter1 = parameter1;
    }

    public string Name{ get; set; }
    public string City{ get; set; }
    public string Parameter1{ get; set; }
}

Then initialize a variable, maybe set it, always pass it to Address's constructor.

var parameter = null;
//Does not matter if the method returns an empty string or an actual value, we initialized it and will pass it anyway.
parameter = SetParameterBasedOnSettingsOrWhatever();
Address b = new Address("Peter", "New york", parameter);

Upvotes: 1

Related Questions