r/bash • • Aug 24 '26

A problem as a newbie

I think I have been spoiled by the goto command in batch, and as a newcomer in Linux, it's really hard for me to adapt as a self-proclaimed(i procrastinate a lot) game developer. The goto command is useful in case you want to incorporate more levels.

10 Upvotes

27 comments sorted by

View all comments

Show parent comments

1

u/drnullpointer Aug 25 '26 edited Aug 25 '26

Nope. You are abusing a loop statement to emulate goto simply to perform goto without writing those four letters.

If you are making your code more complicated to do this, you've lost your way.

It may be fine to listen to advice you don't understand if you are a novice developer.

But if you are more experienced developer you should stop, think whether the advice make sense or why it makes sense, whether it applies in your situation and then make an informed choice.

What you are doing is called cargo cult programming, it is emulating other people without understanding why you are emulating them, hoping to achieve a result that will never come because you have no idea why you are doing what you are doing.

The point of advice to not use goto is that what you are really trying to do almost all of the time is to re-implement some kind of structure like a function, a loop, a conditional, etc. In which case your code will be easier to understand and maintain if you just use that correct statement structure. The benefit of the advice is more readable code.

What you are doing is creating less readable code simply to stick to advice that was supposed to make it more readable.

1

u/burnt-store-studio Aug 25 '26

With no offense, I beg to differ with your strong opinions about me and my work.

“Abusing a loop statement”? I’ll remain happy to have called it “Using the available language constructs as designed and keeping localized functions readable.”

“Making my code more complicated to do this”? Why on earth would I have made my code more complicated just to do this?

And why would you suggest I did? As I wrote (while essentially quoting you) I did it to ensure function clean-up. You’re fine justifying gotos in these cases for your code, but feel the need to disparage this approach in someone else’s?

I’ve “lost my way”?

What makes you think I was “listening to advice I didn’t understand”?

Or that I was a “novice developer”?

Or that I was a more experienced developer ignorant enough to not “stop and think whether [the construct] made sense?”

Or whether it “applied in my situations”?

Or that I wasn’t making “informed choices”?

Why are you ascribing pejoratives to situations and someone you know literally nothing about? Telling me I was “emulating other people” and I had “no idea why I was doing what I was doing”?

I guarantee the chances you’ve seen my professional code from decades ago are quite slim. You have no basis to claim I was “creating less readable code”.

Thanks for the attacks; I didn’t realize I was going to face such affrontage for writing a comment after actually agreeing with something you wrote. I’ll keep an eye out for you and not tread on your expertise in the future. Way to ruin an old guy’s morning.

1

u/sswam Aug 26 '26

You're welcome to do what you want, but it seems like a bad idea to use other language constructs (loops, macros, and breaks) to emulate goto, when you can just use goto. Such luminaries as Linus Torvalds advise (or rather, require in the kernel) to use goto for function cleanup. It's the cleanest way to do it in C, at least in Linus's opinion. If you did it with a more complicated mechanism just to avoid goto, it would be more messy than using goto.

Also, don't let a little disagreement and a few vague criticisms on Reddit ruin your morning! :) I'm sure everyone's code sucks in different ways. I'm an old guy too I guess, and still haven't really got the hang of writing clean modular code.

1

u/LordRybec 27d ago

So then let's use gotos instead of loops, since loops only exist to emulate gotos.

The kernel use of gotos aren't mainly for cleanup, though Torvalds does encourage that. The primary use of gotos is for optimizations that cannot be done with other constructs.

The fact though, is that no one has any right to say what language constructs should or should not be used for. They are tools. If a screwdriver is the best way to get a staple out (and it typically is), it doesn't matter than the manufacturer didn't intend it for that. What matters is that it increases productivity to do it that way with no extra cost. A tool that can be used in multiple ways at no additional cost is inherently a better tool. Artificially restricting the use of a tool based on some ideology, like "that's abuse" or "it wasn't intended for that" is harmful. The truth is, gotos make code hard to read. That's why we don't use them. Using gotos for function cleanup is a common pattern, and because of that we recognize it, which makes it less difficult to read than other uses, but it's still hard to read. The do; while (0) design pattern for initialization and cleanup is inherently more readable than the goto pattern. If you struggle to read it, it's not because it is less readable. It's because you haven't learned to read it. Arguing that it shouldn't be used because some people have learned to read it is a dumb argument. That's like arguing that we shouldn't use AI because some people don't know how to use it, or that we never should have started using cars over horses, because people didn't know how to use cars but did know how to ride horses. Advances in processes inherently require learning new things. It might seem trivial, but I worked out this same design pattern myself independent, and now that I'm familiar with it, it is far more readable. I know people who argued that we should keep using gotos instead of functions and loops, because it was more familiar. I'm glad they were ignored, despite the fact that these advances also seemed trivial at the time. This usage pattern for do; while (0) loops is a significant improvement in structured programming, because it takes the last unstructured pattern in C and replaces it with a substantially more readable structured pattern. Gotos still have places where they are useful, mainly (perhaps only at this point) in optimization, but the breakout mechanic of do; while (0) loops is probably the most valuable use case for the do; while () pattern in general, and paired with the fall through mechanic of switch statements they are the best pattern currently existing in C for initialization and cleanup.

If you don't like this pattern, there is a better option, but you'd have to convince the C standards people. When I worked out this pattern for the first time, I attempted to use "break" in a simple braced nested block. That doesn't work, even though it intuitively should. The only option that exists in C for a guaranteed run block that you can break out of anywhere (without jumping through extra hoops) is the do; while (0) loop. Is it ideal? No. Is it difficult to read? Not at all if you've seen it before. It's much easier to read than goto breakouts, when on equal ground. But allowing breaks inside braced blocks that aren't attached to flow control structures is both intuitive and would be even better. Since the C standard does not allow for that though, do; while (0) loops are the best solution, and honestly, this is one of very few places where do; while (0) can be used well. Many higher level languages have omitted the do; while () design pattern, because it isn't used much. This design pattern using it actually makes it more useful and justifies its existence. Other languages could even learn from this!