【问题标题】:C# switch clause on boolean variable布尔变量上的 C# switch 子句
【发布时间】:2018-12-20 12:19:32
【问题描述】:

我有一段代码,即检查布尔变量是否为真,并根据条件执行相应的操作:

bool result = true;
bool isTrue = false;
CheckIsTrue(ref isTrue);

if (result)
{
   if (isTrue)
   //Perform action
}

如果变量设置为false,我需要执行另一个操作:

 if (result)
 {
    if (isTrue)
    {
      //Perform action
    } 
    else if(actionType == 6)
    {
      //Perform action, isTrue = false.
    }
 }

出于可读性和可维护性的原因,我决定将以上内容更改为:

 if (result)
 {
     switch (isTrue)
     {
       case true:
               //perform action on isTrue
           break;
              default:
                   switch (actionType)
                   {
                      case 6:
                         //Perform action on 6
                       break;
                          default:
                        //Perform default action        
                       break;
                   }
            break;
     } 
}

我的问题是:在布尔变量上使用swicth.. case... 是否明智? 这是我考虑过的简化代码的最佳方式,但我不确定这到底有多正确。

【问题讨论】:

  • 我相信这个问题更适合code review网站。如果您想保持代码可读性,请尝试仅使用一级缩进。
  • 我想说这主要是基于意见的,但就个人而言,您的第二个代码示例比使用多个嵌套 switch 语句(如您的第三个)更具可读性。如果您有很多 actionType 值需要不同的执行路径,我个人会使用 if (isTrue) { ... } else { switch (actionType) { ....} }
  • 对 bool 使用 switch 语句不是好的编码习惯,所以你应该避免它。只需将 if/else 用于 bool 并在内部切换或调用另一个函数来处理 switch 语句。
  • 如果你想使用 switch,请考虑我的回答,因为这是在你的情况下使用 switch 的一种非常易读的方式。
  • @SergeyBerezovskiy 示例,像这样的存根或假设代码在代码审查中是题外话,很快就会关闭。请参阅codereview.meta.stackexchange.com/questions/5777/… 了解更多信息。

标签: c# switch-statement boolean


【解决方案1】:

只有一级缩进和正确的变量名:

if (!actionShouldBePerformed) // instead of result
   return;

if (defaultActionShouldBePerformed) // insted of isTrue
{
   //Perform action
   return;
}

if (actionType == ActionType.NameOfType) // instead of magic number 6
   // Perform another action   

延伸阅读:Replace Nested Conditional with Guard Clauses

【讨论】:

    【解决方案2】:

    我认为switch 语句不是布尔变量的好选择。只需比较这些代码:

    if(boolVariable)
    {
      //...
    }
    else
    {
      //...
    }
    

    等价的

    switch(boolVariable)
    {
      case true:
        //...
        break;
      case false: // or default:
        //...
        break;
    }
    

    IMO if 语句更清晰、可读性和可维护性更高:)

    【讨论】:

      【解决方案3】:

      这并没有错,但我不认为最后一个代码块更具可读性。 就个人而言,我会坚持使用if ... else,就像这样:

      if (result) {
        if (isTrue) {
          // perform action
          return;
        } else if (actionType == 6) {
          isTrue = false;
          // perform action
          return;
        }
        // perform default action
      }
      

      【讨论】:

        【解决方案4】:

        我不会对 bool 值使用 switch 子句。更易读的代码形式是:

                if (!result)
                    return;
        
                if (isTrue)
                {
                    // do action
                }
                else if (actionType == 6)
                {
                    // do something
                }
                else
                {
                    // do the default action
                }
        

        但这不是 OOP 代码的好例子,我建议你阅读 SOLID 原则。

        【讨论】:

        • 我知道 SOLID.. 但不幸的是,很久以前编写的代码将我绑定到这种方法。
        • @BarrJ 我仍然认为我回答你问题的方法比这更具可读性(并且它使用 switch)
        • 是的。我将更新我的答案以显示我的结论:)
        【解决方案5】:

        在我看来,第一个代码比那个怪物更具可读性。如果您使用的是 C# 7 或更高版本,则可以这样编写代码:

        switch (result)
        {
            case true when isTrue:
                //Here is the code when both result and isTrue are true
            break;
            case true when actionType == 6:
                //Here is the code when both result and actionType is 6
            break;
            default:
                //Here defaultaction
            break;
        }
        

        【讨论】:

          【解决方案6】:

          感谢大家的帮助!由于多种原因,我最终做的是:

           if (result)
           {
              if (isTrue)
              {
                //Perform action
              } 
              else 
              {
                 switch (actionType)
                 {
                    case 6:
                        //Perform action on 6
                          break;
                             default:
                                //no condition mathed - write to log     
                          break;
                 }
              }
           }
          

          我确实更喜欢这种方法,原因是:

          首先 - 如果actionType 变量的类型发生变化,我需要做的只是对大小写做一个小改动,而不是在else-if 子句中处理它。

          第二,如果添加了另一个条件,我只需要添加另一个案例,而不是多年来构建一个大的嵌套else-if 子句。

          第三 - 如果没有匹配的情况,在默认情况下更容易维护日志记录。

          我在周末考虑了所有答案并接受了@Sergey Berezovskiy 的答案,因为他发布的答案和链接帮助我得出了最终结论。

          【讨论】:

            猜你喜欢
            • 2017-08-19
            • 2014-08-18
            • 2013-10-09
            • 2011-12-19
            • 2014-11-06
            • 1970-01-01
            • 2023-01-19
            • 2011-08-03
            • 1970-01-01
            相关资源
            最近更新 更多