Live data from Hacker News

GOTOphobia considered harmful in C

blog.joren.ga

151–160 of 319 posts

Re: GOTOphobia considered harmful in C

#151
post #20

If you’ve ever actually written more than toy C, you’ll know gotos are essential for resource cleanup and error handling. Even the famous goto fail was not a goto error, it was a block error (named because a cleanup statement `goto fail;` always executed because of a brace-less if). goto can be abused like any other language construct, but it’s uniquely useful and makes code more simple and easy to reason about when…

of course nobody should be writing C any more, for exactly the reason you're demonstrating in this comment

If you're steeped in C and its quirks, have good coding patterns that allow for it, I say knock yourself out.

I'm absolutely coding in C again — writing some old-school games using SDL. After working for decades with various retain/release, garbage-collected, magic-memory™ languages, going back to C feels like programming again. I like it.

Re: GOTOphobia considered harmful in C

#152
Anybody that dogmatically avoids something like a cult probably doesn't practice the holistic thinking necessarily to design large systems.

People mocking the use of goto is a bozo bit switch for me. It can switch back, but offering pithy out of context absolutisms and trying to pass it off as wisdom is a hard point to recover from.

It's almost always in one's interest to play dumb and not be sure of anything. That's what I see the smart people do.

Re: GOTOphobia considered harmful in C

#153
post #77
post #50

I always encourage people to go read Dijkstra's GOTO paper, instead of just its title. It's a short and easy read, almost like a blog post. If you pay attention, you can see that the he was talking about spaghetti code vs structured code. It's better when the lexical structure of the source code maps to the execution structure. That is, if you know what is the current line being executed, you have a good idea of what…

The thing is that many people today have never encountered the sort of spaghetti code that Dijkstra was talking about in 1968. There's plenty of confusing and messy code around, but true spaghetti code that GOTOs all over the place and is nigh-impossible to follow has been extremely rare for a long time. I can't recall encountering it in the last 30 years. It easy to misunderstand what he was even talking about becau…

The last time I encountered that kind of code was when I wrote it myself in VB6 as a kid. I’ve never seen it in my decade and a half work life.

Re: GOTOphobia considered harmful in C

#154
post #153
post #77

Earlier quoted context omitted.

The thing is that many people today have never encountered the sort of spaghetti code that Dijkstra was talking about in 1968. There's plenty of confusing and messy code around, but true spaghetti code that GOTOs all over the place and is nigh-impossible to follow has been extremely rare for a long time. I can't recall encountering it in the last 30 years. It easy to misunderstand what he was even talking about becau…

The last time I encountered that kind of code was when I wrote it myself in VB6 as a kid. I’ve never seen it in my decade and a half work life.

You're lucky. If you want to feel comfortable on a plane, don't work on avionics systems written in the 1970s-1980s (and probably a lot from the 1990s). Some horrifically bad code running some planes.

Re: GOTOphobia considered harmful in C

#155
post #50

I always encourage people to go read Dijkstra's GOTO paper, instead of just its title. It's a short and easy read, almost like a blog post. If you pay attention, you can see that the he was talking about spaghetti code vs structured code. It's better when the lexical structure of the source code maps to the execution structure. That is, if you know what is the current line being executed, you have a good idea of what…

Sure, it's a good letter, and indeed it's about this problem where people use a go-to or jump control flow with no structure whereas by the time he wrote that letter there were well understood structures (like: functions) to prefer.

However, while I sympathise with a C programmer who feels goto is necessary in their language, I think that speaks to a problem in the language rather than the programmer. If you have better structural support in your language you can express the things the author (of the link, not Dijkstra) wants to express without needing this unstructured go-to.

For example Rust's break 'label value; allows us to mark any compound expression with the 'label, and then say from anywhere inside that expression but nowhere else that we've decided the value of the expression overall and here's what it is.

This doesn't feel that different from what is being done here with goto, except for two crucial things as a result of being structured:

1. Rust will type check this, if this region of the program picks a Dog, our break needs to provide a Dog, it can't just shrug and expect the program to continue without one. This means maintenance programmers don't need non-local reasoning, this region of the program does, in fact, always pick a Dog, albeit the break 'label value is something to look closely at if you're reading that region itself.

2. We cannot do this, even by mistake (e.g. as a result of copy-paste) across scopes. If you try to break 'label result from the cat care loop into the dog loop earlier in the same function, that just doesn't compile, whereas the C goto has no problem attempting that (a good C compiler should notice if you try to do something really egregious, but good luck).

Re: GOTOphobia considered harmful in C

#156
post #50

I always encourage people to go read Dijkstra's GOTO paper, instead of just its title. It's a short and easy read, almost like a blog post. If you pay attention, you can see that the he was talking about spaghetti code vs structured code. It's better when the lexical structure of the source code maps to the execution structure. That is, if you know what is the current line being executed, you have a good idea of what…

[deleted]

Re: GOTOphobia considered harmful in C

#157
I had a debate at work about whether guards are considered spaghetti code. The same person also thought that you should never use GOTO's. He learned these rules in his comp-sci classes and never questioned them; even after years in the real world.

Personally, I use guards heavily to reduce nesting and if the language supports goto's, I'll use them if it make sense to improve code flow. However, its been a very long time since I've needed to use goto :)

Re: GOTOphobia considered harmful in C

#158

I'm browsing this, and I'm not seeing the way I do it, which is sort-of-like #5 but not quite... I tend to wrap the code that has multiple exits-to-label in a do...while(0) loop, and use break to get there... So it might look like: do { if (false == call_func1()) { cleanup_any_state(); break; } if (false == call_func2()) { cleanup_any_state(); break; } } while (0); At any point you can branch to the common exit-state…

I don't like the code pattern if ( false == func() ). Function func() already returns a boolean, no need to compare that to a second boolean (false or true) to generate a third boolean. Sorry, a little off topic I know.

It's there for illustration, that's all. I don't actually write code like that, but I want to make sure that you understand the condition in the code snippet.

Re: GOTOphobia considered harmful in C

#159

The problem with goto is that it is an unbelievably primitive operator. It can be used to implement any logic at all, and therefore it does not express any logic very clearly. It's a bad way to express intent in code. Aside from the exceptions discussed in the article, there is always a better, clearer way to express logic than to use goto statements. The same is true of while loops. Aside from a few cases where they…

This has been called the https://en.wikipedia.org/wiki/Rule_of_least_power, and it’s a good way to make legible what you’re doing and especially to exclude what you aren’t doing.

Re: GOTOphobia considered harmful in C

#160
post #116
post #54

Earlier quoted context omitted.

Where do I imply other tools are 1000% safer? Even 30% safer is a win. 30% safer, 30% more readable, and 30% more productive would be even better. > When was the last time you did “cut your arm” with goto specifically? It's been a while since I've used C, and even longer since I've personally written goto statements. I do remember frequently getting tripped up on them right after undergrad. It's not friendly, and I d…

I do remember frequently getting tripped up on them right after undergrad. It's not friendly, and I don't ever wish to touch them again. So it’s something bad from the undergrad past, no details. Must we take an advice based on that? I’m not sure I will. I'm working in a C++ game engine project right now and it's constantly segfaulting. I can't imagine that setting register jumps manually in complex higher level code…

> So it’s something bad from the undergrad past

I was writing laser projector video games that played on the side of skyscrapers in undergrad. Speak for yourself.

Post reply on HN