goto

Valid use of goto for error management in C?

自闭症网瘾萝莉.ら 提交于 2019-11-26 00:52:35
问题 This question is actually a result of an interesting discussion at programming.reddit.com a while ago. It basically boils down to the following code: int foo(int bar) { int return_value = 0; if (!do_something( bar )) { goto error_1; } if (!init_stuff( bar )) { goto error_2; } if (!prepare_stuff( bar )) { goto error_3; } return_value = do_the_thing( bar ); error_3: cleanup_3(); error_2: cleanup_2(); error_1: cleanup_1(); return return_value; } The usage of goto here appears to be the best way

What is wrong with using goto? [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-11-26 00:08:24
问题 This question already has answers here : Closed 9 years ago . Possible Duplicates: Why is it bad to use goto? GOTO still considered harmful? I was ramdomming through xkcd and saw this one (if also read some negative texts about them some years ago): What is actually wrong with it? Why are goto\'s even possible in C++ then? Why should I not use them? 回答1: Because they lead to spaghetti code. In the past, programming languages didn't have while loops, if statements, etc., and programmers used

GOTO still considered harmful? [closed]

China☆狼群 提交于 2019-11-25 22:56:09
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 4 years ago . Everyone is aware of Dijkstra\'s Letters to the editor: go to statement considered harmful (also here .html transcript and here .pdf) and there has been a formidable push since that time to eschew the goto statement whenever possible. While it\'s possible to use goto to

Valid use of goto for error management in C?

瘦欲@ 提交于 2019-11-25 18:57:58
This question is actually a result of an interesting discussion at programming.reddit.com a while ago. It basically boils down to the following code: int foo(int bar) { int return_value = 0; if (!do_something( bar )) { goto error_1; } if (!init_stuff( bar )) { goto error_2; } if (!prepare_stuff( bar )) { goto error_3; } return_value = do_the_thing( bar ); error_3: cleanup_3(); error_2: cleanup_2(); error_1: cleanup_1(); return return_value; } The usage of goto here appears to be the best way to go, resulting in the cleanest and most efficient code of all possibilities, or at least so it seems