PHP switch语句的最佳布局?

时间:2022-05-22 05:25:01

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

这一直困扰我一段时间:switch语句的最佳布局是什么,特别是在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.
  • 我觉得case / break与switch / if语句中的括号一样排列。

  • 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.
  • 在查看代码时,我遇到了一个中断,我有时觉得if语句中缺少一个端括号,因为它到目前为止是缩进的。

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

所以我的问题是,布局switch语句的正确方法是什么?我在使用方法1时错了吗?

6 个解决方案

#1


5  

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

很抱歉发布此答案,但我无法将其纳入评论:

I prefer method 2, because:

我更喜欢方法2,因为:

  • 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".

    而对于我个人的偏好,在这种情况下使用方法1会感到很尴尬,因为中断似乎缩进“太多”。

#2


16  

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.

完全可选但有效的括号语法。极大地提高了可读性,特别是对于非常大的案例陈述。

#3


5  

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.

我也看到它像这样用过。注意缩进?实际上它是有道理的,因为动作(执行某些操作的代码)只是远离{和}括号的一个缩进,就像常规函数或if语句一样。然而,对我来说,它使得已经奇怪的开关语句更加奇怪。

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语句时,每个只有一个动作,我有时会使用以下方法。我认为这使它非常易读。

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:

就像一个例子,它甚至可以像这样格式化,而不是{和}括号,你使用:和一个endwitch:

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;

#4


1  

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

没有正确或不正确的方法; switch语句语法是非平衡的,因此没有方法可以“正确”。

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.

事实上的标准似乎是第二种方法。但是如果您对编程语言感到满意,那么缩进方案并不重要,只要它对于给定的代码块一致使用即可。

#5


-2  

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.

注意:我开玩笑说改变你的同事的代码来改变他们的编码风格。这通常被视为被动的侵略性,只会使工作场所充满敌意。

#6


-2  

switch($option){
case 'a': 
//do something; 
break;
case 'b': 
//do something; 
break;
default:
break;
}

#1


5  

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

很抱歉发布此答案,但我无法将其纳入评论:

I prefer method 2, because:

我更喜欢方法2,因为:

  • 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".

    而对于我个人的偏好,在这种情况下使用方法1会感到很尴尬,因为中断似乎缩进“太多”。

#2


16  

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.

完全可选但有效的括号语法。极大地提高了可读性,特别是对于非常大的案例陈述。

#3


5  

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.

我也看到它像这样用过。注意缩进?实际上它是有道理的,因为动作(执行某些操作的代码)只是远离{和}括号的一个缩进,就像常规函数或if语句一样。然而,对我来说,它使得已经奇怪的开关语句更加奇怪。

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语句时,每个只有一个动作,我有时会使用以下方法。我认为这使它非常易读。

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:

就像一个例子,它甚至可以像这样格式化,而不是{和}括号,你使用:和一个endwitch:

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;

#4


1  

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

没有正确或不正确的方法; switch语句语法是非平衡的,因此没有方法可以“正确”。

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.

事实上的标准似乎是第二种方法。但是如果您对编程语言感到满意,那么缩进方案并不重要,只要它对于给定的代码块一致使用即可。

#5


-2  

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.

注意:我开玩笑说改变你的同事的代码来改变他们的编码风格。这通常被视为被动的侵略性,只会使工作场所充满敌意。

#6


-2  

switch($option){
case 'a': 
//do something; 
break;
case 'b': 
//do something; 
break;
default:
break;
}