Error handling style in C

  é™ˆçš“        2012-04-24 06:51:00       20,017        13    

Following are three error handling styles in C.Which one you like the most? Or you don't like any one?

1. /* Problem : Not enough. Easy to be wrong */
int foo(int bar)
{
        int return_value = 0;
        int doing_okay = 1;
        doing_okay = do_something( bar );
        if (doing_okay)
        {
                doing_okay = init_stuff();
        }
        if (doing_okay)
        {
                doing_okay = prepare_stuff();
        }
        if (doing_okay)
        {
                return_value = do_the_thing( bar );
        }
        return return_value;
}


2. /* Problem : Use goto is a bad idea */
int foo(int bar)
{
        if (!do_something( bar )) {
                goto error;
        }
        if (!init_stuff( bar )) {
                goto error;
        }
        if (!prepare_stuff( bar )) {
                goto error;
        }
        return do_the_thing( bar );
error:
        return 0;
}

3. /* Problem : Too many nested if statement. Hard to read */
int foo(int bar)
{
        int return_value = 0;
        if (do_something( bar )) {
                if (init_stuff( bar )) {
                        if (prepare_stuff( bar )) {
                                return_value = do_the_thing( bar );
                         }
                }
        }
        return return_value;
}

Author : Source : http://coolshell.cn/articles/551.html

C  ERROR HANDLING STYLE  GOTO  NESTED IF 

       

  RELATED


  13 COMMENTS


Mark [Reply]@ 2012-04-24 08:03:52
CException.sourceforge.net :)
Menny [Reply]@ 2012-04-24 08:05:00
Or this: int return_value = 0; if (do_something( bar ) && init_stuff( bar ) && prepare_stuff( bar )) { return_value = do_the_thing( bar ); } return return_value;
m0skit0 [Reply]@ 2012-04-24 08:09:11
@Menny: yeah, that's way more readable :P
Carl [Reply]@ 2012-04-24 08:30:24
I'm so sick of this "got is bad!" bullsh*t and FUD in C programming. There is nothing wrong with using goto. It's like saying scissors are bad because if you run around with them, you might trip and hurt yourself. Learn how to use the tool correctly instead of saying that it's "bad". Grep the Linux kernel source code for instances of goto and you will come up with over 500 occurances. Bad using goto? No. Just use it the proper way and it's fine.
Dengo [Reply]@ 2012-04-24 08:30:52
Why is using goto a bad idea?
kyle [Reply]@ 2012-04-24 08:38:33
I would do something like if ((do_something(bar) >= 0) && (init_stuff(bar) >= 0) && (prepare_stuff(bar) >= 0)) return_value = do_the_thing(bar); else warn("context"); return return_value;
luciofm [Reply]@ 2012-04-24 09:20:20
Stop reading on "Use goto is a bad idea" People continue to throw this as an undeniable truth.
clavijo [Reply]@ 2012-04-24 10:49:49
int ok = 1; ok = ok && foo(); ok = ok && bar(); ok = ok && baz(); return ok;
kyledrake [Reply]@ 2012-04-24 10:52:38
Goto was designed for stuff like this. See the K&R C book.
chenbo [Reply]@ 2012-04-24 11:26:22
I would say that all the three styles are OK if it's used in a clean & consistent way. Your example codes are all easy to read. I do not understand how could #1 to be easy to be wrong. For #2, using GOTO is all right. So as #3.
aguynamedryan [Reply]@ 2012-04-24 11:42:24
What about returning early out of the function? Avoids the nesting problem of #3 and the goto in #2: int foo(int bar) { int return_value = 0; if (!do_something( bar )) { return return_value; } if (!init_stuff( bar )) { return return_value; } if (!prepare_stuff( bar )) { return return_value; } return do_the_thing( bar ); }
webreac [Reply]@ 2012-04-24 12:40:07
Your first solution is not elegant in your case, but in some cases, the principle may be acceptable. There is nothing wrong with goto (except for bad teachers). It allows to have a structure very similar to exception handling which may be desirable in some cases. Each time, I find constructions that are more elegant with a goto, I try to use goto. It occurred 3 times in 20 years. Only one of them reached production: the two others ones disappeared when defects of the specifications were removed. Goto should not be avoided, but it is rarely the best solution. The third solution is the one we try to avoid. The most common solutions to avoid nested if are "short-circuiting boolean operators (&& ||)" or "usage of subroutines". Sometimes mixed with your first solution.
doodables [Reply]@ 2012-04-24 13:54:23
I used to work with a bunch of people who thought a do { } while(false); loop was a great solution to this problem, because you get to use goto functionality without using goto! Ridiculous. Go back and read the "goto considered harmful" essay and try to understand it and realize that it's complaining *misuses* of goto, not proper uses, which do exist.


  RANDOM FUN

Girlfriend vs compiler