How do you handle huge if-conditions?

后端 未结 21 2103
一向
一向 2021-01-31 17:14

It\'s something that\'s bugged me in every language I\'ve used, I have an if statement but the conditional part has so many checks that I have to split it over multiple lines, u

21条回答
  •  不要未来只要你来
    2021-01-31 17:39

        if (   (condition_A)
            && (condition_B)
            && (condition_C)
            && (condition_D)
            && (condition_E)
            && (condition_F)
           )
        {
           ...
        }
    

    as opposed to

        if (condition_A) {
           if (condition_B) {
              if (condition_C) {
                 if (condition_D) {
                    if (condition_E) {
                       if (condition_F) {
                          ...
                       }
                    }
                 }
              }
           }
        }
    

    and

        if (   (   (condition_A)
                && (condition_B)
               )
            || (   (condition_C)
                && (condition_D)
               )
            || (   (condition_E)
                && (condition_F)
               )
           )
        {
           do_this_same_thing();
        }
    

    as opposed to

        if (condition_A && condition_B) {
           do_this_same_thing();
        }
        if (condition_C && (condition_D) {
           do_this_same_thing();
        }
        if (condition_E && condition_F) {
           do_this_same_thing();
        }
    

    Most of the static analysis tools for examining code will complain if multiple conditional expressions do not use explicit parenthesis dictating expression analysis, instead of relying on operator precedence rules and fewer parenthesis.

    Vertical alignment at the same indent level of open/close braces {}, open close parenthesis (), conditional expressions with parenthesis and operators on the left is an very useful practice, which greatly ENHANCES readability and clarity of the code as opposed to jamming everything that can possibly be jammed onto a single line, sans vertical alignment, spaces or parenthesis

    Operator precedence rules are tricky, e.g. && has higher precedence than ||, but | has precedence than &&

    So, ...

        if (expr_A & expr_B || expr_C | expr_D & expr_E || expr_E && expr_F & expr_G || expr_H {
        }
    

    is a really easy multiple conditional expression for mere humans to read and evaluate improperly.

        if (   (  (expr_A)
                & (expr_B)
               )
            || (  (expr_C)
                | (  (expr_D)
                   & (expr_E)
                  )
               )
            || (   (expr_E)
                && (  (expr_F)
                    & (expr_G)
                   )
               )
            || (expr_H)
           )
        {
        }
    

    There is nothing wrong with horizontal space (linefeeds), vertical alignment, or explicit parenthesis guiding expression evaluation, all of which ENHANCES readability and clarity

提交回复
热议问题