Do you consider this technique “BAD”?

前端 未结 28 1438
借酒劲吻你
借酒劲吻你 2021-02-02 10:57

Sometimes you need to skip execution of part of a method under certain non-critical error conditions. You can use exceptions for that, but exceptions generally are not

相关标签:
28条回答
  • 2021-02-02 11:42

    Refactor. The clean solution will in most cases be to split this code out into a smaller helper function, from which you can return, rather than breaking out of your not-actually-a-loop.

    Then you can substitute your break's for return's, and now people can immediately make sense of your code when reading it, instead of having to stop and wonder why you made this loop which doesn't actually loop.

    Yes, I'd say that simply because it doesn't behave as the reader would expect, it's a bad practice. The principle of least surprise, and so on. When I see a loop, I expect it to loop, and if it doesn't, I have to stop and wonder why.

    0 讨论(0)
  • 2021-02-02 11:46

    I have used this idiom for years. I find it preferable to the similar nested or serial ifs , the "goto onError", or having multiple returns. The above example with the nested loop is something to watch out for. I always add a comment on the "do" to make it clear to anyone new to the idiom.

    0 讨论(0)
  • 2021-02-02 11:46

    Its a very strange idiom. It uses a loop for something its not intended and may cause confusion. I'd imagine this is going to span more than one page, and it would be a surprise to most people that this is never run more than once.

    How about using more usual language features like functions?

    bool success = someSensibleFunctionName();
    
    if(success)
    {
       ...
    }
    
    someCommonCodeInAnotherFunction();
    
    0 讨论(0)
  • 2021-02-02 11:46

    If splitting code between if(!isGood) break; into separate functions, one can end up with dozens of functions containing of just a couple of lines, so that doers not simplify anything. I could not use return because I am not ready to leave the function, there is still stuf I want to do there. I accept that probably I should just settle for separate if(isGood) {...} condition for every code part which I want to execute, but sometimes that would lead to A LOT of curly braces. But I guess I accept that people does not really like that type of construction, so conditions for everything winds!
    Thanks for your answers.

    0 讨论(0)
  • 2021-02-02 11:48

    If your code is doing something other than the plain meaning of the constructs in place, it's a good sign you've ventured into "cute" territory.

    In this case you have a "loop" that will only run once. Any reader of the code will need to do a double-take to figure out what's going on.

    If the case where it isn't "good" is truly exceptional, then throwing exceptions would be the better choice.

    0 讨论(0)
  • 2021-02-02 11:49

    You're pretty much just disguising a "goto" as a fake loop. Whether you like gotos or not, you'd be just as far ahead using a real undisguised goto.

    Personally, I'd just write it as

    bool isGood = true;
    
       .... some code
    
       if(isGood)
       {
       .... some more code
       }
    
       if(isGood)
       {
       .... some more code
       }
    
    0 讨论(0)
提交回复
热议问题