Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror

Comment Re:I see two unconditional jumps here (Score 1) 683

But I might have missed some. There is not much difference between "return 0;" and "ret=0; goto exit_function;". Both are unconditional jumps there is no rational reason why one should be "considered harmful" and the other not.

If there are resource allocations, or at any time in the future resource allocations may be added, there is a huge difference between these two in terms of maintainability. A single point of return makes it trivial to ensure that all allocations are properly freed.

There are two types of functions containing multiple points of return: those with bugs, and those that will have bugs. In a large project, with many engineers, you can reliably find bugs simply by searching for functions with multiple return statements. They're practically always buggy.

Comment Re:Eliminate Structured Programming? (Score 2, Insightful) 731

The one application of "goto" that I swear by is for cleaning up allocations on failure when coding in C.

Maintaining a huge library of legacy C code, one of the most common bugs we see is leaks due to people using multiple "return" statements and failing to clean up allocations. You can fairly reliably pick such a function at random and find a memory leak: people always get it wrong.

"goto cleanup;" however, is hard to mess up.

I've seen any number of clever tricks to avoid the "goto". Using "break" statements in a do {} while (0) loop, for example. All of them merely obfuscate the code, and make it more likely for bugs to appear.

Slashdot Top Deals

The road to ruin is always in good repair, and the travellers pay the expense of it. -- Josh Billings

Working...