PHP switch语句的最佳布局?

psy*_*ott 14 php layout switch-statement code-layout

这一直困扰我一段时间:switch语句的最佳布局是什么,特别是在PHP中?

我发现自己这样做有两种方式,甚至没有考虑过.然后,有时当我回到代码时,我觉得它看起来不正确,并以另一种方式重写它.重复!

方法1

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

  case 'b':
    //do something
  break;
}
Run Code Online (Sandbox Code Playgroud)

好处:

  • 我觉得case/break与switch/if语句中的括号一样排列.
  • 在我看来,它看起来更好

缺点:

方法2

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

  case 'b':
    //do something
    break;
}
Run Code Online (Sandbox Code Playgroud)

好处:

缺点:

  • 在查看代码时,我遇到了一个休息时间,我有时觉得if语句中缺少一个端括号,因为它到目前为止缩进了.

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

myo*_*yol 21

方法3

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

        break;
    }

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

        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

或者更紧凑一点

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

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

        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

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


Jef*_*aal 7

以下格式化方式是我在其他人的代码中看到的最多,也是我喜欢格式化的方式:

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;

}
Run Code Online (Sandbox Code Playgroud)

我也看到它像这样使用.注意缩进?这是有道理的实际,因为操作(即做一些事情的代码)是唯一一个缩进距离{}括号,就像一个普通的functionif声明.然而,对我来说,这使得已经奇怪的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;

}
Run Code Online (Sandbox Code Playgroud)

当我有一个很长的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;
}
Run Code Online (Sandbox Code Playgroud)

使用a时看起来更好return,在这种情况下不需要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);
}
Run Code Online (Sandbox Code Playgroud)

就像一个例子,它甚至可以像这样格式化,而不是{}括号,你使用a :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;
Run Code Online (Sandbox Code Playgroud)


Yos*_*shi 5

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

我更喜欢方法2,因为:

  • 休息不会干扰案件的可读性

  • 有时你会在内部条件下休息,例如:

    if(...) {
      break;
    }
    
    Run Code Online (Sandbox Code Playgroud)

    所以案件可能会落到下一个案件或其他事情上.

    而对于我个人的偏好,在这种情况下使用方法1会感觉很尴尬,因为它break似乎缩进"太多".