why is it illegal to use 'goto'?

preview_player
Показать описание
Should you use goto statements? What does a goto statement even do? Why are they bad? or... are they? In this video, we talk about what a goto statement is, when it's bad to use them and how to use them well in your code!

🔥🔥🔥 SOCIALS 🔥🔥🔥
Рекомендации по теме
Комментарии
Автор

"GOTOs make your code unreadable, therefore code without GOTOs is readable" is the "denying the antecedent" fallacy.

AstroTibs
Автор

I've been programming for over 50 years and goto has a definite place in the toolbox. Now for highly structured languages such as C/C++, Java, etc. goto can be somewhat confusing. But for procedural languages such as cobol (yes, cobol is still used today), Fortran, etc. goto can actually make the code more readable and, moreover, more efficient. For assembly language, goto is a necessity.

jimnoeth
Автор

Yeah agree. The error handling case mentioned here and sometimes breaking multi-layers of for loops is where goto make more sense than normal control flow. They are literally everywhere even in source code of linux kernel.

ishi_nomi
Автор

You can also write a function that does this for you and call that function with different parameters if you are scared of the unstoppable errors that might come with this approach.

ChunGzter
Автор

Another use of goto is for breaking out of a nested loop.

captainfordo
Автор

I've only had to use a goto once in ten years, to escape a highly-nested for loop. I could have "unrolled" the for loop and turned it into 5 functions, but I tried that, and it resulted in a code that was less readable than with a simple goto. My boss fought me for it, but in the end, dogmas and tradition matter less than producing code our teams can actually use and maintain easily.

Trekiros
Автор

Imo discussing why GOTO is shunned without talking about what it actually was when "GOTO considered harmful" was written does the discussion a misservice.

So, GOTO at the time a lot more powerful than what C is able to do. It was not only in most languages the primary way of handling control flow, it was also able to jump across function boundaries. Here an example (in C syntax because reasons):
void f(int i) {
label:
print(i);
}
void g() {
goto label;
}

This lead to the problem that you couldn't think of function like a black box. You needed to know what it does if you want to reason about your code. And because EVERY function was able to do that, you needed to practically know the contents of EVERY function.
And guess what, stuff like this was so widely done, that at the time "GOTO considered harmful" was published it was highly controversial ("You want to say I am not capable of doing this safely?").

kuhluhOG
Автор

In C, handling errors in resource acquire/release is already error prone, and goto specifically for this case and only for forward jumps reduces human error. This is a common pattern in kernel code itself. If you're using C++, you'd be doing it wrong by using goto since we've got Resource Acquisition Is Initialization there.

metal
Автор

Don't be afraid of gotos. At the core, the goto statement represents one of the most fundamental building blocks of computing; the jump. Without jumps there would not be touring completeness. I love the fact that C allows you to directly use these very low level bricks, as you can implement control flows that are impossible with ifs, switches, whiles and for loops. Once again, C gets you closer to your hardware and that's a feature, not a bug.

LogicEu
Автор

Looking at C code where you check the return type to see if it is an error makes me so thankful that Rust has Option, and Result enums.

oglothenerd
Автор

I was writing a parser, and I used goto because I wanted to break out of the switch statement inside a for loop. It was an elegant solution.

leightonmitchell
Автор

In general as a rule of thumb, I try to keep GOTOs in the same function scope, so just for loops and the like, it helps keep GOTO reasonably easy to read (and far safer!) but also still gives it a lot of flexibility. Also if I'm writing optimized code, I try to keep branches in general (not specifically GOTOs, they just tend to get messier), to be short and minimal, only things like small if statements, just to avoid the LOADS of cache misses you can cause by putting everything in their own function.

shadowchasernql
Автор

Finally someone (else) that shows evidence where goto's are useful. Folks, goto's aren't evil; they're just another tool in the toolbox to use when it makes sense given all of the factors. (Singletons in other languages are similar in this way.) Having said this, part of determining "when it makes sense" is to not take it too far and abuse it.

BlitterObject
Автор

I always love this argument. Writing high level code without GOTOs creates more readable code. This is mostly true. The funny part is, the compiler generates tons of jumps (gotos) in the final binary, the actual assembly language. Just disassemble a switch statement, or if-then-else. JMP (goto) is there.

marshallscarpulla
Автор

In pure C stuff like this is an at least decent to good way of handling things.
In most other languages you should rather use some sort of scope guards though (something RAII-like like objects in C++ and rust, or "using" resource statements in C# etc.)

sinom
Автор

If, while, for, underneath they are just "gotos"

jordixboy
Автор

Man you are the sole reason why I fell in love with assembly, couldn't thank you more.

larjunmnath
Автор

why is goto bad: it makes your code look more like assembly, and the whole point of programming languages is to not use assembly.

MatVeiQaaa
Автор

My favorite use for goto in *C++* is to mimick the for-else pattern from Python. For the unaware, else statements on for loops will only execute if the loop concluded by its condition, rather than a break. With iterator-based for loops, it's as simple as moving the condition inside the loop body and turning it into a break (that way, you can add additional code to that specific exit path). For range-based for loops, that is not an option. Instead, the solution is to replace all breaks with gotos which skip past a block of code immediately following the for loop's scope.

for (const auto& v : container)
{
if (some condition)
goto LOOP_EXIT;
SomeFunction(v);
}
std::println("All good!");
LOOP_EXIT:
...

Minty_Meeo
Автор

Thank you for making this video.
Many people always scream at the sight of a goto without considering how it can be beneficial to the circumstance.
I personally write C# more than anything else so I benefit from 'using' statements, however I still find gotos can be very useful when writing state machines or non-LINQ filtering logic.

Zullfix