|
Improve goto (no really!)
#395035
02/19/12 03:20
02/19/12 03:20
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
OP
Expert
|
OP
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
From the manual: The target label must be within the same winged brackets ({ .. }) as the goto instruction. This is unusual, as far as I've seen in C and C++ -- normally the only requirement is that the label is within the same function. I know use of goto is normally frowned upon, but consider the following small piece of code:
int i, j, k;
for (i=0; i<4; i++) {
for (j=i+1; j<4; j++) {
for (k=j+1; k<4; k++) {
... do stuff ...
goto foundFace;
}
}
}
foundFace:
...
We have some nested loops, in this case to consider the faces of a tetrahedron and stop as soon as we find a face that fits certain conditions. Normally we'd exit a loop with "break", but the only way to do so here would be to have a "break" within each loop, and set another variable to indicate it's time to break, which each loop would check, so it might as well be included as an extra condition in all but the inner-most loop. That's ugly and a little inefficient. Another option would be to put all the loops in a separate function, and use "return" instead of "goto", but this is the only instance where I need these loops since they serve a very specific purpose, and calling a new function for this purpose would be inefficient. Yes, if I'm so concerned about efficiency I could unroll the loops. But that's not the problem -- it's the balance of efficiency and readability, and the use of "goto" is the most readable in this case. "goto" can lead to spaghetti code, but it can also be used as a deep "break". So yeah, between the other stuff you guys have going on, if it's not too much trouble, I think it'd be nice if Lite-C was a little more C-like in this regard -- "goto" shouldn't be confined to a label in the same set of winged brackets
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: Improve goto (no really!)
[Re: Superku]
#395037
02/19/12 03:26
02/19/12 03:26
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
OP
Expert
|
OP
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
Oh true! How did I not think of that? That's what I'll do here. As an example of where goto would be useful, though, consider the case that the i, j, and k variables should be preserved for the rest of the function (which, thankfully, is not the case in my application). Thanks
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: Improve goto (no really!)
[Re: JibbSmart]
#395085
02/19/12 19:08
02/19/12 19:08
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
Superku, of course your approach is working, but it's not a good style from my point of view as it introduces new dependencies. Whenever you change the limit of the loops you have to remember that you have to adapt the inner assignment as well. That is likely to be forgotten.
I'd introduce a new function, extract the three nested loops to this function and use a return instead of the break.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Improve goto (no really!)
[Re: MrGuest]
#395114
02/19/12 23:10
02/19/12 23:10
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
OP
Expert
|
OP
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
MrGuest, that works, but it introduces conditions that will be checked for every loop. It's not much, but it's inefficient, and would be avoided if goto behaved a little more like C.
Uhrwerk, I don't want to introduce a function I'm only going to use once, but I agree it's the "cleanest" solution without goto.
I went with Superku's because it's a little more efficient, and I don't need to preserve i or j -- I do everything I need to with them within the loop.
I understand Lite-C isn't the same as C in every respect, but "goto" is really useless here.
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: Improve goto (no really!)
[Re: JibbSmart]
#396261
03/04/12 23:02
03/04/12 23:02
|
Joined: Jun 2004
Posts: 2,234 Wisconsin USA
FoxHound
Expert
|
Expert
Joined: Jun 2004
Posts: 2,234
Wisconsin USA
|
Just Copy those vara when you would have used got and the reassign those values back.
--------------------- There is no signature here.
QUIT LOOKING FOR ONE!
|
|
|
Re: Improve goto (no really!)
[Re: FoxHound]
#396262
03/04/12 23:44
03/04/12 23:44
|
Joined: Feb 2010
Posts: 886
Random
User
|
User
Joined: Feb 2010
Posts: 886
|
Would this work in a while loop?
|
|
|
Re: Improve goto (no really!)
[Re: Random]
#404149
07/03/12 15:35
07/03/12 15:35
|
Joined: Apr 2006
Posts: 159 Latvija
Arrovs
Member
|
Member
Joined: Apr 2006
Posts: 159
Latvija
|
i really really agree. I vant old goto - my code have statements vhich cant be vritten in svitch and in break place i have goto to get out of multiple state execution.
Arrovs once will publish game
|
|
|
|