mercredi 12 septembre 2018

Most effective and elegant way to check 3 parameters using if conditons

I have 3 parameters to check with some conditions.

As an example my code as follows,

public string checkIfConnditions(string msg, int score, int age)
{
    var response = "";

    if (msg == "hello" && score > 20 && age < 25)
    {
        response = "All para success";
    }
    if (msg != "hello" && score > 20 && age < 25)
    {
        response = "Unmatching message";
    }
    if (msg == "hello" && score < 20 && age < 25)
    {
        response = "Score not satisfied";
    }
    if (msg == "hello" && score > 20 && age > 25)
    {
        response = "Age not satisfied";
    }
    if (msg != "hello" && score < 20 && age < 25)
    {
        response = "Unmatiching message & Score not satisfied ";
    }
    if (msg != "hello" && score > 20 && age > 25)
    {
        response = "Unmatiching message & Age not satisfied";
    }
    if (msg == "hello" && score > 20 && age > 25)
    {
        response = "Age & Score not satisfied";
    }
    if (msg != "hello" && score < 20 && age > 25)
    {
        response = "All parameter unsatisfied";
    }
    return response;
}

There have 3 parameters and 8 probability can happen based on its values. Here I check those as above code. But it looks ugly and I think it's not the best way to do this. what is most efficient and elegant way to do this

Aucun commentaire:

Enregistrer un commentaire