views:

552

answers:

6

I've tested this and it works fine, but it looks... weird... to me. Should I be concerned that this is nonstandard form which will be dropped in a future version of PHP, or that it may stop working? I've always had a default case as the final case, never as the first case...

switch($kind)
{
    default:
     // The kind wasn't valid, set it to the default
     $kind = 'kind1';
     // and fall through:

    case 'kind1':
     // Do some stuff for kind 1 here
     break;

    case 'kind2':
     // do some stuff for kind2 here
     break;

    // [...]

    case 'kindn':
     // do some stuff for kindn here
     break;

}

// some more stuff that uses $kind here...

(In case it's not obvious what I'm trying to do is ensure $kind is valid, hence the default: case. But the switch also performs some operations, and then $kind is used after the switch as well. That's why default: falls through to the first case, and also sets $kind)

Suggestions? Is this normal/valid syntax?

+1  A: 

Common practice is to define the default option as last option. But I see nothing wrong with your solution (if there is no predefined schema in your company how to layout your code)

Henrik P. Hessel
Cool thanks. As the lead developer of my company I get to set a precedence :-) (another reason I want to get other's opinions before choosing my path)
Josh
Then you should set the standard to default as last option in a switch statement. I looks "normal" to the majority of programmers.
Henrik P. Hessel
And since every other switch statement in the code is that way, I agree. Thanks!
Josh
You're not trying to set that as a precedence are you? that's going to make any future devvies cringe!
Mez
why not? I never saw Josh's layout before :) and I did a lot php years ago *g
Henrik P. Hessel
A: 

it looks... weird... to me

Me too! But if it makes syntactical sense to do this, I think you are fine.

Jeff
+7  A: 

It is an unusual idiom, it causes a little pause when you're reading it, a moment of "huh?". It works, but most people would probably expect to find the default case at the end:

switch($kind)
{
    case 'kind2':
        // do some stuff for kind2 here
        break;

    // [...]

    case 'kindn':
        // do some stuff for kindn here
        break;]

    case 'kind1':
    default: 
        // Assume kind1
        $kind = 'kind1';

        break;

}
Paul Dixon
I agree and every other switch I've ever written in my 10+ years of programming have been like that. But in this *one* case, if I write it like that I'd need two switch statements.
Josh
Ok, I take that back. I'd need: case 'kind1': default: $kind='kind1';
Josh
+1 This is definitely a more agreeable form
Justin Johnson
Ok, sorry, I don't know how I missed that in your answer. I may go that route just for clarity.
Josh
After so many people's initial reaction was the same as mine, I decided this was the way to go.
Josh
+2  A: 

Kind of made me twinge at first, but that's just because we're not use to seeing things that way.

I would suggest that you document this highly, since some might call this "tricky" code. A noob or some future maintainer might come along and move it to the bottom where they're more comfortable with it and break the side-effect that is has being at the top.

Justin Johnson
+2  A: 

I'd personally prefer to do

switch($kind)
{
    case 'kind2':
        // do some stuff for kind2 here
        break;

    // [...]

    case 'kindn':
        // do some stuff for kindn here
        break;

    case 'kind1':
    default:
        $kind = 'kind1'; // Redundant if it's already set as 'kind1', but that doesn't make any difference to the code.
        // Do some stuff for kind 1 here
        break;

}
Mez
Thanks, that's what I decided to do. I accepted Paul's answer though because he was first. But +1 for you :-)
Josh
am too slow tonight!
Mez
Yeah that happens to me all the time -- it sucks! Sorry :-)
Josh
A: 

This is how I'd probably do it... it's easy on the eye and keeps the functionality.

switch($kind)
{
    case 'kind1': default :
        // Do some stuff for kind 1 here
        break;
    case 'kind2':
        // do some stuff for kind2 here
        break;
    case 'kindn':
        // do some stuff for kindn here
        break;
}
Frankie