• Brahvim@lemmy.kde.social
    link
    fedilink
    English
    arrow-up
    2
    ·
    7 hours ago

    Doing algorithms in year 2 of college and I use goto fearlessly. We have TOO MANY cults and stigmas in programming these days…!

    Aaaaand I love writing C. …Though it can be pretty painful.

    • Buddahriffic@lemmy.world
      link
      fedilink
      English
      arrow-up
      1
      ·
      3 hours ago

      Goto was risky and it was one big name on some BBS back in the day that pushed for a “never use goto” dogma, rather than figuring out rules for when it should or shouldn’t be used. And a whole bunch of people just jumped on that bandwagon, but not everyone.

      The main rule for using goto that I’m aware of is to never use it to jump into the middle of a scope. Jumping around in the same scope is fine, jumping out of a scope is fine (assuming you handle unwinding the stack correctly, which I think the C compiler tries to do, at least, but there are other constructs that can save/restore stack contexts to do the same thing as a goto but safer).

      Though programming language contstructs can also help eliminate the cases where using goto gives better code. Breaking out of nested loops is the main good case for goto that I’m aware of. Without a goto, you need to set up an exit flag and check it before starting a new iteration for each loop level you want to escape from. Unless your language supports labelled breaks/continues, in which case the compiler knows which loop the break/continue refers to instead of needing to always assume it means the deepest level.

      C/C++ is my favourite language, but I’ve used an enhanced C++ with labeled loops and it was nice.