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