views:

259

answers:

1

I recently found a situation in which I had two related variables that had several permutations upon which I wanted to do an action if the two variables were one of half a dozen permutations.

As an example of what the data range the variables were:

$months = array('January', 'February', 'March', 'April', 'May', 'June', 'July',
    'August', 'September', 'October', 'November', 'December');
$days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');

$month = $months[array_rand($months)];
$day = $days[array_rand($days)];
$week = rand(1, 4);

What I came up with involved a simple switch comparison of an array, like this:

function checkInputForCriteria($month, $day, $week)
{
    switch (array($month, $day))
    {
        case array('January', 'Wednesday'):
        case array('February', 'Monday'):
        case array('April', 'Tuesday'):
        case array('July', 'Saturday'):
        case array('September', 'Thursday'):
        case array('September', 'Tuesday'):
            return (in_array($week, array(1, 2)));
    }

    return FALSE;
}

Commonly this would be written as:

function checkInputForCriteria($month, $day, $week)
{
    if (
        ($month == 'January' && $day == 'Wednesday')
        || ($month == 'February' && $day == 'Monday')
        || ($month == 'April' && $day == 'Tuesday')
        || ($month == 'July' && $day == 'Saturday')
        || ($month == 'September' && $day == 'Thursday')
        || ($month == 'September' && $day == 'Tuesday')
    )
    {
        return ($week == 1 || $week == 2);
    }

    return FALSE;
}

While I realize the switch-case-array method isn't commonly used, I don't see any coding standard reason not to use it. PHP has no problem comparing the data in each element of the array exactly as it looks. It is compact with direct order relation between the value array elements and each comparison array element. The only reason I can see not to use this method is if it were CPU or memory intensive (which I haven't tested).

Based on just the coding standard implications, would you accept this usage in your own code? If you saw this in someone elses code just browsing past it would you have any trouble understanding what was happening?

If this type of structure is disallowed simply because it's not what we normally see/expect, what would be the way to introduce a structure to compare data like necessary in the above examples? Are basic if statements the only acceptable way to compare data to make logic decisions?

+1  A: 

In a language like PHP—which is largely interpreted at runtime—I think it's fine. Better than "fine" actually: the array switch is concise and to the point.

I think the reason such things aren't often seen is that languages like C++ or Java don't support them, so thought patterns which explore such solutions aren't really well developed. Certainly the if form will optimize better in C++, even if an overloaded method of writing approximately that in C++ were created.

wallyk