Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
@Artemix Ya I've read it somewhere few years ago that goto is meant for really specific cases, say exiting many nested loops, but in-case of just wanting to end a function a return will do it.
-
@gitpush I try to never use the goto, it can be very dangerous if used out of context (like the image).
-
@Artemix I can't post the whole code for obvious reasons, but she used that "goto" to exit from an if 😰
-
@allibragi Exactly that's why I always avaoid it, though like what @Artemix said, having multiple returns in the function sometimes isn't a good idea, but I tend to put noticeable comments before each return if I have it in the middle of the function so that I can easily notice it
-
Salmakis6737ySince ages coding in differend environments, languages and project types i never saw a reason to use goto, the last time i used it was in qBasic or something back in the 90ties
-
Without seeing the rest of the code I can't judge whether the goto was necessary, but why not just "jump:;" ?
-
Kyliroth2307yEdgar Dijkstra's 'goto considered harmful' paper needs a relink!
Show her this. Everyone needs Dijkstra.
http://u.arizona.edu/~rubinson/... -
Byomeer22097y@Salmakis @codumke How do you exit out of a for-loop inside another for-loop? That's the last reason to use goto. Just show me another way of doing it... =D
-
Kyliroth2307y@Byomeer separate the work into functions. If you need to break out the parent loop too then it sounds like your work is done... therefore it's a return statement.
Each loop needs to take care of it's own flow. -
Byomeer22097y@Kyliroth The question at this point is: Which alternative is easier to understand? Functions may at this point lead to more confusion than goto. And btw: How do you also return the parent function at the same time? You would need an if for this and THAT would be really ugly code. Gonna stick to goto in this special case ;)
-
Kyliroth2307y@Byomeer .. unless you're using anonymous functions it shouldn't be that confusing. The names should be explanatory and it will make the code reusable and modular.
https://msdn.microsoft.com/en-us/...(v=vs.71).aspx
The example here can be rewritten to:
https://justpaste.it/19p64
Now it seems more readable and allows that code to be reused.
Because it's an MSDN example I doubt it's the best use of goto though. Haha
Can you send a snippet too where goto is preferred over functions? I'm intrigued! :D -
Byomeer22097yLast time i encountered this rare case: Imagine you have a while loop for all frames coming from a camera and inside of it another loop. This iterates over an undefined amount of devices. If the user presses a button on any of those devices, the outer loop is breaked and some post-calculation starts. You can only access a boolean to know if a button is pressed.
// code snippet in C++
while(true) {
// do something with frame
for(auto device : devices) {
if(device.pressedButton()) {
goto exit;
}
}
}
exit:
// post calculations
How can i achieve this without my - in this case - beloved goto? -
Byomeer22097y@Byomeer OMG please let me use 2 spaces to indent code o____O
// code snippet in C++ [tab version]
while(true) {
// do something with frame
for(auto device : devices) {
if(device.pressedButton()) {
goto exit;
}
}
}
exit:
// post calculations -
Kyliroth2307y@Byomeer ha, unfortunately we can't format code.. think it's to limit posts with code to be honest... there's a full rant discussing it anyway.. I'll try and find link..
In that case there, I'd just wrap it up in a function. goto doesn't seem to be any more beneficial in this case.
https://justpaste.it/19q0v -
Byomeer22097y@Kyliroth thanks for the information on formatting =)
I think there actually are some other ways of doing it but goto is by far the easiest method. Not using goto just because it's goto seems a bit narrow-minded to me =D -
Kyliroth2307y@Byomeer No worries. I can't find link with quick search.. but will keep an eye out anyway!
;) If it feels easier to you and doesn't cause issues then all is definitely swell.
Definitely good to have open discussions about it.
Never felt the need to use it myself, so intrigued with how people use it. Especially in higher level languages -
Byomeer22097y@Kyliroth Another pro side of the discussion: I searched stackoverflow for related questions and got a shitload of inspiration for new rants =D
This is by far the most mindfucked goto I've seen in my life... https://devrant.io/rants/755400/
Related Rants
To be honest, she warned me that the code was badly written, but...
undefined
goto
jump
c#
badcode