Do you consider this technique “BAD”?

前端 未结 28 1512
借酒劲吻你
借酒劲吻你 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:38

    For better or worse, I have used the construct in a few places. The start of it is clearly documented, though:

        /* This is a one-cycle loop that simplifies error handling */
        do
        {
            ...modestly complex code, including a nested loop...
        } while (0);
    

    This is in C, rather than C++ - so exceptions aren't an option. If I were using C++, I would consider seriously using exceptions to handle exceptions. The repeated test idiom suggested by Jeremy is also reasonable; I have used that more frequently. RAII would help me greatly, too; sadly, C does not support that easily. And using more functions would help. Handling breaks from inside the nested loop is done by repeated test.

    I would not classify it as a great style; I would not automatically categorize it as "BAD".

提交回复
热议问题