Re: Wouldn't it be simpler and more readable to use a goto instead of a pseudo-loop?
Re: Wouldn't it be simpler and more readable to use a goto instead of a pseudo-loop?
Posted Dec 25, 2014 22:23 UTC (Thu) by mchapman (subscriber, #66589)In reply to: Re: Wouldn't it be simpler and more readable to use a goto instead of a pseudo-loop? by ldo
Parent article: The "too small to fail" memory-allocation rule
There would be precisely as many "goto" statements as you have "break" statements. It's much of a muchness, in my opinion.
Posted Dec 25, 2014 23:07 UTC (Thu)
by ldo (guest, #40946)
[Link] (26 responses)
And you would need a different label for each. And the extra visual burden of ensuring that each goto is paired with the right label. Which is unnecessary with a break, since that can only ever terminate one enclosing construct.
And the further burden of getting things wrong when you try refactoring the code.
Like I said: NO GOTOs!
Posted Dec 26, 2014 0:01 UTC (Fri)
by Cyberax (✭ supporter ✭, #52523)
[Link] (5 responses)
So, YES to gotos for error handling!
Posted Dec 26, 2014 0:37 UTC (Fri)
by ldo (guest, #40946)
[Link]
Posted Dec 26, 2014 7:38 UTC (Fri)
by epa (subscriber, #39769)
[Link] (3 responses)
Posted Dec 26, 2014 7:43 UTC (Fri)
by Cyberax (✭ supporter ✭, #52523)
[Link]
Posted Jan 16, 2015 5:10 UTC (Fri)
by CameronNemo (guest, #94700)
[Link]
Posted Jan 16, 2015 18:23 UTC (Fri)
by zlynx (guest, #2285)
[Link]
That almost always made the code cleaner, and let me exit the block (which was now a function) with a simple "return."
Posted Dec 26, 2014 7:34 UTC (Fri)
by WolfWings (subscriber, #56790)
[Link] (19 responses)
Um... since when? You can use a single label for any number of goto's, so each label is specifically describing it's use case (freeAndExit perhaps, or whatever semantic name you want to give it) and there's no more/less lines than your do{}while(false); construct but copy-pasting code inside/outside the strcture can't break it as easily. Just saying you're wrong about the claim of needing a billion labels, goto is many-to-one not one-to-one in that regard.
Posted Dec 26, 2014 21:15 UTC (Fri)
by ldo (guest, #40946)
[Link] (17 responses)
Posted Dec 27, 2014 0:47 UTC (Sat)
by reubenhwk (guest, #75803)
[Link] (7 responses)
The deeply nested allocations example *should* be solved by factoring out local/static helper functions...In other words: if your code is too complicated to use GOTO's properly for cleanup, then your code is too complicated.
Posted Dec 27, 2014 20:42 UTC (Sat)
by ldo (guest, #40946)
[Link] (6 responses)
Posted Dec 27, 2014 20:56 UTC (Sat)
by Cyberax (✭ supporter ✭, #52523)
[Link] (1 responses)
What else do you want?
Posted Dec 27, 2014 21:33 UTC (Sat)
by ldo (guest, #40946)
[Link]
Posted Jan 1, 2015 0:27 UTC (Thu)
by reubenhwk (guest, #75803)
[Link] (3 responses)
Posted Jan 1, 2015 4:23 UTC (Thu)
by flussence (guest, #85566)
[Link] (1 responses)
Posted Jan 1, 2015 4:40 UTC (Thu)
by reubenhwk (guest, #75803)
[Link]
Posted May 23, 2017 13:10 UTC (Tue)
by mirabilos (subscriber, #84359)
[Link]
Posted Dec 27, 2014 17:44 UTC (Sat)
by itvirta (guest, #49997)
[Link] (8 responses)
> for(...) {
I'm not sure I'd agree that breaking out of a loop just to test the same error condition,
Like someone mentioned, some other programming languages have dedicated constructs
(The thing with absolute rules is, that they absolutely forbid one from thinking. That usually
> Fine. Try reworking the example I gave above, then. That has allocations nested up to three levels
Reducing the nesting level and using a less space-hungry indenting style would be what I'd start with,
Posted Dec 27, 2014 20:46 UTC (Sat)
by ldo (guest, #40946)
[Link] (7 responses)
Posted Dec 27, 2014 20:57 UTC (Sat)
by Cyberax (✭ supporter ✭, #52523)
[Link] (1 responses)
Or here: https://github.com/ldo/dvd_menu_animator/blob/master/spuh... ?
I actually don't see complicated cleanups anywhere in your code. Posted Dec 28, 2014 3:27 UTC (Sun)
by reubenhwk (guest, #75803)
[Link] (4 responses)
Better yet, use a function for each of those nested levels. Worried about spaghetti code? Then factor out the nesting and call smaller functions with less looping and less nested resource management. Use the return value to propagate error conditions onward.
Posted Dec 28, 2014 6:34 UTC (Sun)
by ldo (guest, #40946)
[Link] (3 responses)
Posted Dec 29, 2014 2:58 UTC (Mon)
by reubenhwk (guest, #75803)
[Link]
Posted Dec 29, 2014 3:19 UTC (Mon)
by bronson (subscriber, #4806)
[Link] (1 responses)
Posted Dec 29, 2014 17:30 UTC (Mon)
by nix (subscriber, #2304)
[Link]
The argument from popularity is not a good one, but if something is not popular it is not terribly wise to imply that in fact it is.
Posted May 23, 2017 13:09 UTC (Tue)
by mirabilos (subscriber, #84359)
[Link]
Avoiding GOTO at all cost is overreacting. The “rule” is probably good to teach to novice programmers, but experts are beyond it. Error handling is *the* prime example in favour of GOTO, although I agree there may be others.
Re: There would be precisely as many "goto" statements
Re: There would be precisely as many "goto" statements
Re: So, YES to gotos for error handling!
Re: There would be precisely as many "goto" statements
Re: There would be precisely as many "goto" statements
Re: There would be precisely as many "goto" statements
Re: There would be precisely as many "goto" statements
Re: There would be precisely as many "goto" statements
if (error1) {
goto label;
}
/* ... more code ... */
if (error2) {
goto label;
}
/* ... more code ... */
label: free(pointer);
Re: you're wrong about the claim of needing a billion labels
Re: you're wrong about the claim of needing a billion labels
Re: deeply nested allocations example *should* be solved by factoring out
Re: deeply nested allocations example *should* be solved by factoring out
Re: What else do you want?
Re: deeply nested allocations example *should* be solved by factoring out
Re: deeply nested allocations example *should* be solved by factoring out
Re: deeply nested allocations example *should* be solved by factoring out
Re: deeply nested allocations example *should* be solved by factoring out
goto bike shed
> example code I linked above, and think how it would look with GOTOs all over the place.
> Avoid GOTOs!
> if (PyErr_Occurred()) break;
> }
> if (PyErr_Occurred()) break;
to then only break out of another loop is any clearer than just jumping out directly.
(like exceptions) that can break out of multiple loops. It's not very different from what goto
was recommended for in this case.
doesn't lead to anything good, so I would advise against it.)
> deep, as well as loops. See how well that works using GOTOs.
if I were to do this exercise for you.
Re: not sure I'd agree that breaking out of a loop just to test the same error condition
Re: not sure I'd agree that breaking out of a loop just to test the same error condition
Re: not sure I'd agree that breaking out of a loop just to test the same error condition
>> propagating the error condition onwards. That is the whole point of the nesting.
Re: Better yet, use a function for each of those nested levels.
Re: Better yet, use a function for each of those nested levels.
Re: Better yet, use a function for each of those nested levels.
Re: Better yet, use a function for each of those nested levels.
Re: There would be precisely as many "goto" statements