Is it necessary to write else part in every if condition?

后端 未结 11 1606
-上瘾入骨i
-上瘾入骨i 2021-02-19 05:16

The question I asked might be closed, But i just want to know that is it necessary to write else part of every if condition. One of my senior programmer said me that \"you shoul

相关标签:
11条回答
  • 2021-02-19 05:19

    That's a horrible idea. You end up with code of the form:

    if (something) {
        doSomething();
    } else {
    }
    

    How anyone could think that's more readable or maintainable that not having an else at all is beyond me. It sounds like one of those rules made up by people who have too much free time on their hands. Get them fired as quickly as you can, or at least move away calmly and quietly :-)

    0 讨论(0)
  • 2021-02-19 05:28

    No, you don't have to ..

    Also, I don't think that it is a good idea for readability, since you will have lots of empty else blocks. which will not be pretty to see.

    0 讨论(0)
  • 2021-02-19 05:32

    No, but I personally choose to always include encapsulating braces to avoid

    if (someCondition)
        bar();
        notbar();  //won't be run conditionally, though it looks like it might
    
    foo();
    

    I'd write

     if (someCondition){
            bar();
            notbar();  //will be run
     }
     foo();
    
    0 讨论(0)
  • 2021-02-19 05:32

    Looking at this purely from a semantic point of view - I cannot think of a single case where there is not an implicit else for every if.

    if the car is not stopped before I reach the wall I will crash, else I will not crash.

    Semantics aside:

    The answer to that question depends on the environment, and what the result of a mistake is.

    Business code? Do what your coding standards say.
    IMHO you will find that spelling it out, although initially it seems like too much work, will become invaluable 10 years from now when you revisit that code. But, it certainly would not be the end of the world if you missed an important 'anti-condition'.

    However: Security, Safety or Life Critical code? That's a different story.

    In this case you want to do two things.
    First:Rather than testing for a fault, you want to prove there is not a fault. This requires a pessimistic view on entry to any module. You assume everything is wrong until you prove it is correct.
    Second: in life critical: You NEVER want to hurt a patient.:

    bool everyThingIsSafe = true;
    if(darnThereIsAProblem())
    {
       reportToUserEndOfWorld();
    }
    return everyThingIsSafe;
    

    Oops. I forgot to set everyThingIsSafe false.
    The routine that called this snippit is now lied to. Had I initialized evertThingIsSafe to false - I'm always safe, but now I need the else clause to indicate that there wasn't an error.
    And yes, I could have changed this to a positive test - but then I need the else to handle the fault.
    And yes, I could have assigned everyThingIsSafe() the immediate return of the check. And then tested the flag to report a problem. An implicit else, why not be explicit?
    Strictly speaking, the implicit else this represents is reasonable.
    To an FDA/safety auditor, maybe not.
    If it's explicit, can point to the test, its else, and that I handled both conditions clearly.

    I've been coding for medical devices for 25 years. In this case, you want the else, you want the default in the case, and they are never empty. You want to know exactly what is going to happen, or as near as you can. Because overlooking a condition could kill someone.

    Look up Therac-25. 8 severely injured. 3 dead.

    0 讨论(0)
  • 2021-02-19 05:33

    No, It's not required to write the else part for the if statement.

    In fact most of the developers prefer and recommend to avoid the else block.

    that is

    Instead of writing

    if (number >= 18) {
        let allow_user = true;
    } else {
        let allow_user = false;
    }
    

    Most of the developers prefer:

    let allow_user = false;
    if (number >= 18) {
        let allow_user = true;
    }
    
    0 讨论(0)
  • 2021-02-19 05:38

    Sometimes there is no else part....and including an empty one just makes the code less readable imho.

    0 讨论(0)
提交回复
热议问题