76

To be honest, she warned me that the code was badly written, but...

Comments
  • 13
    and I thought goto is a bad habit
  • 10
    What you on about that's a very descriptive variable 😂
  • 2
    @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.
  • 1
    @gitpush I try to never use the goto, it can be very dangerous if used out of context (like the image).
  • 0
    @Artemix I can't post the whole code for obvious reasons, but she used that "goto" to exit from an if 😰
  • 2
    @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
  • 3
    Since 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
  • 0
    ^this, what I want to do when non developer boss says my code is too messy
  • 0
    Without seeing the rest of the code I can't judge whether the goto was necessary, but why not just "jump:;" ?
  • 1
    @rtannerf nobody teaches it because everyone wants it to die
  • 1
    There is always a better way than goto
  • 0
    Edgar Dijkstra's 'goto considered harmful' paper needs a relink!

    Show her this. Everyone needs Dijkstra.

    http://u.arizona.edu/~rubinson/...
  • 0
    @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
  • 0
    @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.
  • 1
    @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 ;)
  • 0
    @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
  • 0
    @Artemix thanks, I'll check some examples out.
  • 0
    Last 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?
  • 1
    @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
  • 0
    Fuuuuuuu... does anyone know a proper way to write code in a comment on devRant?
  • 0
    @Artemix but why would i go for threading if can simply use goto?^^
  • 0
    @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
  • 0
    @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
  • 1
    @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
  • 1
    @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/
  • 1
    @Byomeer that is incredible!
Add Comment