代码之家  ›  专栏  ›  技术社区  ›  Barr J

布尔变量上的C switch子句

  •  0
  • Barr J  · 技术社区  · 6 年前

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

    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... 关于布尔变量? 这是我考虑过的简化代码的最佳方法,但是我不确定这是多么正确。

    6 回复  |  直到 6 年前
        1
  •  2
  •   Sergey Berezovskiy    6 年前

    只有一级缩进和适当的变量名:

    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
  •  2
  •   Michał Turczyn    6 年前

    我想 switch 语句不适合布尔变量。只需比较这些代码:

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

    它相当于

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

    依我所见 if 语句更清晰、可读、可维护:)

        3
  •  1
  •   Teun van der Wijst    6 年前

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

    if (result) {
      if (isTrue) {
        // perform action
        return;
      } else if (actionType == 6) {
        isTrue = false;
        // perform action
        return;
      }
      // perform default action
    }
    
        4
  •  1
  •   Buda Gavril    6 年前

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

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

    但这不是OOP代码的一个好例子,我建议您阅读一些坚实的原则。

        5
  •  1
  •   Ricardo Alves    6 年前

    依我看,第一个代码比那个怪物更可读。如果您使用的是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
  •  0
  •   Barr J    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 条款。

    第二 ,如果添加了另一个条件,我只需要添加另一个事例,而不是构建一个大型嵌套 否则如果 多年来的条款。

    第三 -如果没有匹配的案例,则在默认案例中更容易维护日志记录。

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