سؤال

This has been nagging me for a while: What is the best layout for a switch statement, specifically in PHP?

I find myself doing it one of two ways, without even thinking about. Then, sometimes when I come back to the code I feel it doesn't look right and rewrite it the other way. Repeat!

Method 1

switch($action)
{
  case 'a':
    //do something
  break;

  case 'b':
    //do something
  break;
}

Advantages:

  • I feel the case/breaks line up like the brackets do in switch/if statements.
  • It looks better in my opinion

Disadvantages:

Method 2

switch($action)
{
  case 'a':
    //do something
    break;

  case 'b':
    //do something
    break;
}

Advantages:

Disadvantages:

  • When looking at code and I come across a break, I sometimes feel there's a missing end bracket from an if statement because it's indented so far.

So my question is, what is the proper way to layout switch statements? Am I wrong in using method 1?

هل كانت مفيدة؟

المحلول

Sorry to post this as an answer, but I couldn't fit it into a comment:

I prefer method 2, because:

  • the breaks don't interfere with the readability of the cases

  • and sometimes you'll have breaks inside conditions, like:

    if(...) {
      break;
    }
    

    so the case could fall down to the next case or something.

    And for my personal preference it would feel awkward to use method 1 in this scenario, as the break would appear to be indented "too much".

نصائح أخرى

Method 3

switch($action)
{
    case 'a' : 
    {
        //do 
        //something

        break;
    }

    case 'b' : 
    {
        //do 
        //something

        break;
    }
}

or a little more compact

switch( $action ) {
    case 'a' : {
        //do 
        //something

        break;
    }
    case 'b' : {
        //do 
        //something

        break;
    }
}

Completely optional but valid bracket syntax. Improves readability drastically imho, especially for very large case statements.

The following way of formatting is what I see most in other people's code, and is also the way I prefer to format it:

switch($action)
{
    case 'login': 
        $this->userLogin($username); 
        break;

    case 'post': 
        $this->userPost($username); 
        break;

    case 'update': 
        $this->userUpdate($username); 
        break;

    case 'logout': 
        $this->userLogout($username); 
        break;

}

I've also seen it used like this. Notice the indents? It makes sense actually, since the action (the code that does something) is only one indent away from the { and } brackets, just like a regular function or if statement. However, to me, it makes the already odd switch statement even more odd.

switch($action)
{
case 'login': 
    $this->userLogin($username); 
    break;

case 'post': 
    $this->userPost($username); 
    break;

case 'update': 
    $this->userUpdate($username); 
    break;

case 'logout': 
    $this->userLogout($username); 
    break;

}

When I have a long switch statement, each with just one action, I sometimes use the following approach. I think this makes it quite readable.

switch($action)
{
    case 'login'    : $this->userLogin($username); break;
    case 'post'     : $this->userPost($username); break;
    case 'update'   : $this->userUpdate($username); break;
    case 'logout'   : $this->userLogout($username); break;
}

Which looks better when using a return, in which case there is no need for break:

switch($action)
{
    case 'login'    : return $this->userLogin($username);
    case 'post'     : return $this->userPost($username);
    case 'update'   : return $this->userUpdate($username);
    case 'logout'   : return $this->userLogout($username);
}

Just as an example, it can even be formatted like this, where, instead of the { and } brackets, you use a : and an endswitch:

switch($action):
    case 'login'    : return $this->userLogin($username);
    case 'post'     : return $this->userPost($username);
    case 'update'   : return $this->userUpdate($username);
    case 'logout'   : return $this->userLogout($username);
endswitch;

There is no correct or incorrect method; The switch statement syntax is non-balanced, so there is no method to 'get it right'.

The defacto standard seems to be the second method. But if you are comfortable with you programming language, the indentation scheme does not matter all that much, as long as it is used consistently for the given code block.

Neither.

Not a "I have a better way and you should use that instead" but a "It doesnt matter."

A lot of coders get anal retentive about spacing, indentation, etc. The whole point though is that code is readable as it doesn't affect how it actually runs. You can make code readable via spacing, indentation, extra line returns, pretty comments separating sections, etc.

This is okay:

switch ('hi'){
    case 'hi': 
        PRINT "HI!";}

This is also okay:

switch ('hi')
{
    case 'hi': 
        PRINT "HI!";
}

Also okay:

    switch ('hi'){
        case 'hi': PRINT "HI!";
    }

Very nonstandard and timeconsuming so I don't know what you'd do it, but also okay:

// *
switch ('hi')
{
// ****   
case 'hi': PRINT "HI!";
// ****
}
// *

Seriously, as long as people can read it and easily discern what's going on, it's fine.

Unless your coworker and fellow coder is very intent on one style and OCD. At which point, you need to correct all of their code to a different style to make your workplace less boring.

Note: I'm joking about altering your coworkers' code to change their coding styles. That's generally seen as passive aggressive and will just make the workplace hostile.

switch($option){
case 'a': 
//do something; 
break;
case 'b': 
//do something; 
break;
default:
break;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top