CS101++ - What is`goto`?

Поделиться
HTML-код
  • Опубликовано: 2 окт 2024

Комментарии • 20

  • @acf2802
    @acf2802 7 месяцев назад +4

    I love when modern, young programmers bring up "goto considered harmful" any time you mention goto. They are completely unaware of the context in which it was written and believe it's simply one of the 10 programming commandments "thou shall never use goto."

    • @bloodgain
      @bloodgain 6 месяцев назад

      This. And I was once one of those young programmers, as this was still the gospel when I was in college in the early '00s. The important thing to understand is that `goto` in modern languages, including C and C++, is not the same `goto` that Dijkstra was talking about. Modern `goto` is not allowed to cross function boundaries.
      The closest to old `goto` in C/C++ is `setjmp`/`longjmp`. Not only are there some reasonable uses of modern `goto`, the form Dijkstra warned about is still part of system languages for a reason.

    • @bloodgain
      @bloodgain 6 месяцев назад

      Also, people like to forget that there's "never" and "programmer never". The latter really means, "only use it when there's really no better or equivalent option, and then only if you really know what you're doing -- if you're not 110% sure, you probably don't". But that's a lot to say. It's easier to say, "*You* shouldn't do this."

    • @acf2802
      @acf2802 6 месяцев назад +2

      @@bloodgain My main point is that "goto considered harmful" was written in a time when "functions" were some newfangled invention that some FUDs hadn't yet adopted. I think the main point of it was to encourage people to start using functions and other higher level abstractions. Well, everyone is using functions today, so that paper is completely irrelevant now. People just quote the title and forget the reason why it was written.

    • @bloodgain
      @bloodgain 6 месяцев назад

      @@acf2802 That is an excellent point, indeed! Context is crucial.

  • @kyleolson8977
    @kyleolson8977 7 месяцев назад +2

    CS101++ Considered Harmful

  • @N....
    @N.... 7 месяцев назад +2

    The only times I've used goto are when I needed to break multiple levels out of nested loops and couldn't easily utilize a better option like returning or a lambda or invalidating the loop conditions etc. - once labeled break and continue are available I guess that will eliminate the last reasonable use of goto.

  • @thesorus
    @thesorus 7 месяцев назад +1

    I'm waiting for the gosub video.

  • @maxaafbackname5562
    @maxaafbackname5562 6 месяцев назад

    For everything there is a time & place.
    If it really was a thing to be never used, the keyword was removed from recent standards like C or C+×.
    Warned against bad usage should be still in place when learning programming.
    You always write structured code, and goto can have a place in that.
    You don't need goto to write spagetticode...
    Break out of multiple loops using goto is the most efficient way.
    For me more readable than additional loop conditions.
    Using goto for some exception handling is, for me, a nice way.

  • @TNothingFree
    @TNothingFree 6 месяцев назад

    Goto comes from a low level statement like jump zero or jump non zero
    Jump to location x is assembly level,
    When you use goto you fail to utilize higher level code features since in c or cpp you no longer control directly the code pointer but only data.
    In low level code whete the code itself is built to control its own code execution i may understand the use - in embedded or embedded asm.
    But in othet contexts, it shoulsnt be used as there are better, more understandable features.

  • @dwarftoad
    @dwarftoad 6 месяцев назад

    As you go on you should talk about how concepts build on each other. How is an if like a conditional goto; how is a function call and return like a superpowered goto. Can you create your own version of something like a function call and return in C++ with just goto? Also, a peek into assembly/machine pseudocode (e.g. simplified assembly code, Knuth's MIX, your own, etc.) that could be generated from these C++ concepts, and how that might be executed by a hypothetical CPU and memory system could be helpful too.

    • @cppweekly
      @cppweekly  6 месяцев назад

      This is a good point, and something I did do in C++ Weekly one time: ruclips.net/video/lW51OrNJAn8/видео.html

  • @Volker-Dirr
    @Volker-Dirr 7 месяцев назад

    Hmm... a little bit few c++ code in this episode. I thought you show use nice and modern algorithms that can be much faster if they use (also) goto instead of "normal" flow control of high languarges (only). (But is was fun to see that old code again. I started coding on a Comodore PET 2001 as a kid)

  • @LogicEu
    @LogicEu 6 месяцев назад

    Goto represents the unconditional jump statement and combined with the conditional jump, aka if, it's one of the most important building blocks in computing and a fundamental assembly instruction.

  • @Minty_Meeo
    @Minty_Meeo 7 месяцев назад

    I have used goto to emulate range-based for-else loops in C++ without a boolean flag. I have used goto to make one function more capable while avoiding code duplication. Goto is really neat, you just need to use it in moderation.

  • @kaanbalkaya
    @kaanbalkaya 7 месяцев назад

    gw basic 🎉

  • @nextlifeonearth
    @nextlifeonearth 7 месяцев назад +1

    I still use goto sometimes in C codebases. I think it's a reasonable alternative for RAII, given that C doesn't have it.

  • @seventyfive7597
    @seventyfive7597 6 месяцев назад

    goto is a **break** , goto is a **continue** , goto is a **return** that is not at the end of a function. All of these are allmost always indicative of bad factoring. Perhaps it's because I'm older than you Jason, but I consider all of these goto-s.

  • @ignacionr
    @ignacionr 7 месяцев назад

    it doesn’t seem like you have a lot of actual CS university style knowledge, am I far out of line? you lack a number of concepts that the most basic CS professor would use.

    • @acf2802
      @acf2802 7 месяцев назад +3

      I would consider that a complement.