vendredi 4 juin 2021

Methodologies or patterns for evaluating a sequence of conditions

Are there any methodologies or patterns for how one might evaluate a sequence of conditions instead of writing out a bunch of if statements?

As an example, when computing whether a year is a leap year, one could do the below, but I bet that there is a pattern for evaluating a series of conditions like this without using a sequence of ifs:

public static bool IsLeapYear(int year)
{
    if (year % 400 == 0) return true;
    if (year % 100 == 0) return false;
    if (year % 4 == 0) return true;
    return false;
}

Aucun commentaire:

Enregistrer un commentaire