32

I found this question: How to implement Do while loop only using GOTO in c language?

It's about how to implement the loop

do {
    // Code
} while(condition);

using goto.

In normal situations, a "don't do it, because it's bad practice" would probably be considered as 'Not An Answer' or something, but in this situation, I can pretty safely say that no valid use case exists at all.

But my question here is if that should be considered an answer. In my opinion, it's not. It's a very good thing to add to an answer, but on its own, it does not answer the question. The answer looks like this:

Using jump statements upwards in C is considered very bad practice, also known as spaghetti programming. So the answer to your question is: there exists no valid use-case in C for goto upwards, you should never write such code. Period.

goto downwards may be acceptable in some special cases like error handlers.

Everything in this post is correct and valuable, but it does not answer the question. I think the code for doing the same thing with goto is needed for completeness.

Also, I'd like to consider this situation. Let's say we have the above question and answer. Then comes another person and asks basically the same thing, but rephrased as:

How do I change a do-while to a goto loop? I'm perfectly aware that it's bad practice, and I'm not planing to use it in production code. I'm just curious about how it should be done.

I have seen a lot of these type of questions, and they seem to be welcomed. I've even asked a few of those myself. But the thing is, if we accept "don't do it" as an answer to the first question, then the second question cannot be closed as a duplicate to the first. And IMO, that's not desirable.

What do you say? Is this an ok answer?


Note: this question discusses specific Q&A pair (as signified by tags). We already have a canonical question covering the general topic: A car with square wheels.

16
  • Also related meta.stackoverflow.com/questions/321877/…
    – Suraj Rao
    Oct 29, 2021 at 14:25
  • Why not edit the question to rephrase it in the acceptable format? Oct 29, 2021 at 15:37
  • 1
    It's probably worth pointing out that the author claims to have posted it to see how people would react. "Another perfect example of this popped up here even as I was reading this. Normally I'd just leave a comment, but to experiment I posted the correct answer instead of answering what the OP asked for. Social experiment, watch me get down voted to oblivion :)" comment by Lundin Oct 29, 2021 at 16:46
  • 4
    So, I think it would be more interesting to discuss what to do about answers that are made – not with the intention to answer a question – but with the intention to misattribute votes to make some completely unrelated point. Frankly, I'm disgusted. Oct 29, 2021 at 17:01
  • If you want this question to be about a specific question, it should be phrased as such.
    – TylerH
    Oct 29, 2021 at 20:23
  • As an aside, as the canonical (A car with square wheels) mentions, you should take pains to ask OP why rather than assume certain things. I see a lot of those assumptions in this Meta question (e.g. "I can pretty safely say that no valid use case exists at all."; "Everything in this post is correct and valuable").
    – TylerH
    Oct 29, 2021 at 20:28
  • 3
    Even if this question here were about a general case, I don't think it matches A car with square wheels. The situation doesn't seem to be about someone trying to build a car with square wheels because they think it's appropriate, but rather because it is educational/interesting. They know that there are cars with round wheels. Oct 29, 2021 at 20:30
  • 1
    Leave the GOTOs (JMP/JNE/BRE/etc.) to the compiler. Just pretend they don't exist :) Oct 29, 2021 at 20:52
  • 1
    @TylerH Yes, I agree that that was a bit bold. I'm wrong sometimes. But this is one of those times when I'm pretty sure of it. There are constructs that are bad in 99.99% of the cases but useful in some very rare occasions. But I actually doubt this is one of them. However, I would be very happy if you could prove me wrong. Note though that I think one should be VERY cautious to claim that something is always bad. For instance, some people say that goto is always bad, but that's actually pretty easy to disprove.
    – klutt
    Oct 29, 2021 at 23:42
  • 4
    @AndrewMorton Gotos have their uses in C. Two good and common uses are cleanups in functions after a fail and breaking out of nested loops. But I have never seen a good use of a backwards jump.
    – klutt
    Oct 29, 2021 at 23:44
  • 3
    Situation: I want to build a car with square wheels to evaluate the performance of my anti-shake device. In this situation, "don't use square wheels because X" is a very annoying answer.
    – anatolyg
    Oct 31, 2021 at 15:12
  • I wonder if "using GOTO" (bad practice) can be compared to "storing passwords in plaintext" (serious security weakness) kind of thing. I'm not saying they should or can be compared, I'm wondering if they can be compared at all.
    – Nuclear241
    Oct 31, 2021 at 16:41
  • Hmm. Is answering "How to parse HTLM with regex" with "You can't parse HTML with regex" a valid answer. Oct 31, 2021 at 18:27
  • 2
    @AdrianMole Yes, because while you can emulate while with goto, you cannot parse HTML with regex.
    – GSerg
    Oct 31, 2021 at 19:11
  • @AdrianMole Pointing out errors in askers assumptions is of course ok. That's a different thing than coming with opinions.
    – klutt
    Nov 1, 2021 at 13:03

6 Answers 6

69

It is technically¹ an answer but not a good one.


The question very specifically asks about the equivalent of a while implemented via goto. The question is not asking about implementing a better while via goto, nor is it asking about some abstract loop-with-condition suspected to be best implemented via goto.
There is no indication the OP wants the "goto equivalent" for anything but educational purposes. They clearly know that the appropriate tool for the job is while.

Lecturing the OP that what they want for educational purposes is not what they should want for practical purposes is, at best, missing the point. That is not useful.


¹In the sense of Not An Answer criteria.

5
  • 2
    Just for clarity. I assume you think that means it is worth (or at least could be worth) a down vote?
    – klutt
    Oct 29, 2021 at 23:46
  • 4
    @klutt Yes, definitely. Oct 30, 2021 at 6:18
  • 27
    The "GOTO considered harmful" stricture is commonly attributed to Dijkstra, but was actually Wirth's doing. Dijkstra subsequently wrote "Please don't fall into the trap of believing that I am terribly dogmatical about [the goto statement]. I have the uncomfortable feeling that others are making a religion out of it, as if the conceptual problems of programming could be solved by a single trick, by a simple form of coding discipline!". Hence I'd suggest that not even Dijkstra would have responded "don't even ask about it", if the discussion was in an educational context. Oct 30, 2021 at 18:20
  • "Lecturing the OP that what they want for educational purposes is not what they should want for practical purposes is, at best, missing the point. That is not useful." -- good point, but one that I have occasionally ignored. Sometimes it is hard to resist pointing out a better way. Oct 31, 2021 at 13:37
  • 12
    @JohnColeman it's likely better to answer question first, then lecture the OP if you feel like it.
    – Evk
    Oct 31, 2021 at 14:35
10

Given that the question has the preferred solution and it is asking how to implement the non-preferred solution, I think it is very clearly an educational question. A question asked to learn more about the system not to use as a solution to a programming problem.

As such, the “use case” you say doesn’t exist IS THE QUESTION ITSELF.

So, the question is a 100% valid and useful question, and an answer of “don’t do that” serves no purpose. It may not quite qualify as NAA, but it should certainly be downvoted into oblivion.

6

TL;DR Before giving advice, use a comment to learn why OP wishes to use goto. Do not assume; ask.

The proper action would be to comment on the question, asking OP for their use case. Without details or clarification, the question may need to be closed, as there are multiple possible interpretations of OP's intentions. Below are the 5 most obvious ones; there may be more.

Notice how different interpretations represent different problems and result in different answers. To me that is a clear indication that a question deserves to be refined first. Answers posted before refinement are possibly premature.

1. Educational

As pointed out by others, the question sounds like an exercise to make students aware of branch instructions generated by a C compiler.

Ways to handle homework questions are discussed elsewhere.

2. Optimization

An answer would focus on debunking some myth that favors goto over do-while.

3. XY problem

For example, OP is running into the problem that a variable declared inside a do-while loop cannot be accessed by statements following the loop. OP knows goto does not create a nested scope, and sees this as the solution.

An answer would focus on explaining a slight refactoring could solve the problem without discarding do-while.

4. Problematic compiler

For example, OP wants to nest 17 do-while loops, but there is this archaic compiler that limits nesting depth to 16.

Answers could focus on a refactoring to put loops in different functions, or on finding a better compiler (though this could be considered off-topic for SO).

(Of course, it could also be an XY problem, but that's just my poor example.)

5. Code generation

Potentially a proper use case for goto. It could be that OP is forced to work with a simplistic code generator that knows only primitive statements. At best, if-goto; no braces. This could leave OP wondering how to implement a loop.

Note: in generated code, the typical readability/maintainability argument against goto is irrelevant.

8
  • 5
    The question is self-contained, and has a clear and precise description of what a practical answer should be. It may not be a useful question, but it is on-topic. It isn't anybody's place to play nanny and govern the OP into revealing their intentions. Advice is great, but not when it comes with a patronising disregard of the question itself. Oct 31, 2021 at 18:24
  • 1
    @StoryTeller Apologies if I didn't make my intentions clear. I am not criticizing the question; I am criticizing answers that provide advice without first inquiring what problem OP is trying to solve. Oct 31, 2021 at 18:45
  • Added an introductory TL;DR - HTH. Oct 31, 2021 at 18:58
  • I agree that asking the OP first is the right choice. However the only valid case I can support is 5. 4 is a subset of 3, 2 is dubious at best without more information or facts about what needs to be optimized, and I struggle with 1 as an appropriate scope on Stack Overflow that isn't supplanted by books or other actual documentation on C.
    – Makoto
    Oct 31, 2021 at 22:54
  • 4
    @Makato Something being covered elsewhere has no bearing on whether it should be covered by SO. Plus, especially for educational purposes SO seems to be more suitable to provide findable, on-point explanations instead of lengthy book chapters. Nov 1, 2021 at 7:42
  • 1
    Another common intention: OP just wants to know how to do that because he is curious and nothing else. It's not the same as homework (but similar to self-educational).
    – Evk
    Nov 1, 2021 at 11:39
  • @Makoto "However the only valid case I can support is 5." Exactly. So how could a single response be a match for all 5 cases? Remember, this discussion is about the quality of the answer, not the quality of the question. Nov 1, 2021 at 18:00
  • The quality of the question is always in question. It is perfectly reasonable to want to have an exit strategy for a question that suddenly turns into too much.
    – Makoto
    Nov 1, 2021 at 18:12
1

This answer was purposely posted as a social experiment - I suspected it would get heavily down-voted. I was reading this A plea against regex dogmatism while browsing questions in the background, then this goto question of a similar nature popped up.

The regex meta discussion was essentially about: should we post a different answer than the one asked for, if it is a better technical solution of higher quality? The meta consensus was generally - yes we should give the best technical solution.

Mostly such questions are "XY questions" where the OP has a problem, think they have a solution to the problem, then ask about problems they have in implement that flawed solution, rather than asking about the actual problem they are trying to solve. Infamously so with regex in particular, the "I solved the problem using regex and now I have 2 problems" meme.

Below that regex question, I posted a comment:

Another perfect example of this popped up here even as I was reading this. Normally I'd just leave a comment, but to experiment I posted the correct answer instead of answering what the OP asked for. Social experiment, watch me get down voted to oblivion :)

Said and done.

As for whether my answer is technically correct or not, a more detailed one on the same topic can be found here: https://stackoverflow.com/a/10995589/584518.

Programming in general, and C programming in particular suffers greatly from "Keep it needlessly complicated" poser syndrome, where someone does really stupid things in order to seem smart. Instead of using the "Keep it simple, stupid" (KISS) principle, which is wildly recognized good practice.

There is no doubt in my mind that the OP will not benefit in the slightest to any of the answers that solves their flawed XY problem question. The other posted answers are of the "write production code like this and lose your job" nature. The main problems is that they will get lured into a false sense of "I'm doing the right thing". And then future readers will also think the same. Overall the post will just sit there as a way to make all future readers worse programmers.

10
  • 5
    I don't really understand. It seems like a completely valid answer to me. I don't understand why it was downvoted, much less deleted. Nov 2, 2021 at 13:09
  • 1
    @CodyGray Same reason as the XY question got 17(!!!) up-votes: because of the herp derp meta effect. Some random people finds some random post through meta, then cast random votes in a topic they don't even understand anything about.
    – Lundin
    Nov 2, 2021 at 13:44
  • 3
    @Lundin I would say that it's a pretty big difference with the regex examples, and that is that those are typically on the form "How do I do Y with X?" while this was on the form "How do I do X?". So now you're comparing the answers "You can do Y with Z instead" with "You should not do X". And another very important factor here is that you completely refused to add missing information even after I asked several times. Sure, an experiment and I don't mind being a part of it, but that is a thing you cannot exclude when analyzing the results.
    – klutt
    Nov 2, 2021 at 14:48
  • @Lundin Plus, do note that I NEVER said that you should not post the text you did. I was and am completely supporting that. The only thing I asked for was that you ALSO included how you can do what was asked, for completeness.
    – klutt
    Nov 2, 2021 at 14:49
  • 1
    @klutt If it isn't an "XY question" then surely someone can give a valid use-case where do-while should be replaced with goto and the reason why. Or if it is a XY question, the first thing that comes to mind should be "why would you want to do that?" or alternatively "what is the actual problem that this solves?"
    – Lundin
    Nov 2, 2021 at 14:52
  • 1
    @Lundin If you were considering it an XY-question, then the right move would probably have been to use comment section to ask for clarification before you answer.
    – klutt
    Nov 2, 2021 at 14:54
  • @Lundin Plus, I once had a quite annoying experience related to this. I had a question on the form, "How do I do X with Y". I knew the Y-part was not the best way to do it. But it was enforced by rules. I got "answers" like that I should talk to those in charge or even get a job somewhere else.
    – klutt
    Nov 2, 2021 at 14:56
  • I mean, they weren't wrong. The best answer to a question is the one that will apply to the most future visitors. It's dangerous to answer such a question without addressing the fact that the solution is awful even though it's the only one that works given the scenario.
    – Kevin B
    Nov 2, 2021 at 15:06
  • @klutt Yes, I complete agree that the most correct course of action is to prompt the OP for details about why they want to do this and not post an answer until that is clarified.
    – Lundin
    Nov 2, 2021 at 15:06
  • @KevinB I completely agree. Bad solutions should be pointed out. That's a very good thing.
    – klutt
    Nov 3, 2021 at 7:33
1

I have thought about this a lot and I have come to some conclusions.

It's often best to ask for clarifications

Firstly, before deciding if you should answer the actual question, propose a better method, or both, remember that it's never a bad thing to use the comment section to ask for clarifications. Many misunderstandings can be avoided by simply asking "Regex does not seem to be a good solution here. Are you sure you really want that, or are you open for other solutions too"

When they have answered, you can ask them to edit the question to add either "It's possible that regex is not a good solution, but it is a requirement from my professor/employer" or "I thought regex was the way to go, but I'm completely open for alternate solutions". Or you can simply edit the question yourself after OP has clarified it.

Blindly answering the exact question

It can be a bad thing to not point out flaws in the method OP wants to use. Blindly just answering the question can mislead both OP and future readers into writing bad code. If it's clear that the proposed method is indeed a bad method, then it should be pointed out. But it's always a good thing to inform about pros and cons with both the proposed method and alternate methods.

Refusing to answer the question

But it can also be a bad thing to simply refuse to answer the actual question for several reasons. First and foremost, simple curiosity IS a valid use case. There are a lot of questions where the aim simply is to understand details and corner cases better. These often have the tag . Here is an example about that Is it safe to do something like foo(x, &x)? where the use case was to be able to improve another answer I was writing.

However, there is also often the case that it's a matter of opinion. Also, the use case may be something different than technical reasons. A prime example was when I was wondering about using char[] for passwords instead of String in Java. It was this question: How do I send a password without using String from java to postgresql?

Granted, I learned a lot. But here the use case was that our customers simple required us to use char[]. It didn't matter if it was a pointless requirement. It was still a requirement I needed to follow, and that is a valid use case.

Analyzis

I think a very important thing is to avoid lecturing people. Help them, but do not lecture them. It's a very big difference in how you express yourself. I think these are completely ok:

Other answers have given good examples of how to do this with a regex, so I won't go into details about that. Instead, I'd like to suggest an alternate method. Notice how easy it is.

A regex is not very suitable here. Instead, I'd do like this:

But just for completeness, here is a regex that does what you want:

The answer relies on other answers for completeness, or include a short solution.

An answer that does not answer the question, but is still ok:

I did try to find a regex solution, but I failed. It was trickier than I thought, but I did manage to solve it like this. Hope it helps.

If it's really that hard to find a regex that does it, then it's a very good sign that it's not a good solution. I think that's a completely ok answer. No objections.

Slightly more pushy, but still ok in my opinion:

A regex is not a very preferable solution here. Sure it's possible to use, but the time complexity would be horrible, and it will be very hard to maintain it. Instead, please consider this:

It does not lecture. It simply tells what the drawbacks are, which is ok. If OP has explicitly said that they want a regex, then it would be preferable to add a sentence like "I know you wrote that you really want a regex, but for future readers, I'd really like to add a few lines about why it's a bad idea"

But this is an answer that might be a bit to lecturing:

Regex is the wrong way to go here. Instead, use this.

No explanation. Just stating that you should not do it. This can easily be perceived as opinions from those who blindly follows what they believe is best practice. In my opinion, this is very much on the edge of what is acceptable. If OP has explicitly stated that they want a regex, then I would definitely consider this a bad answer that deserves a down vote, and possibly even a not an answer flag.

Writing an answer without answering the actual question can be completely ok. But if you get the response "Thank you, but I really want to solve this with method X" then take a step back.

But it's quite tricky to come up with a simple rule that is applicable for all cases. It has to be judged from case to case. But both blindly answering the question without informing about the drawbacks and refusing to answer the question can deserve a down vote. It depends on the situation.

Conclusion

The two best things are:

  • asking OP for clarifications

  • answer the actual question, explain why the method is bad, and propose a better method

0

I would say yes, a valid answer can be "the ABC approach you are using is bad, here is why", and if appropriate offer XYZ approach to solve the problem instead. I do this on a number of answers. It may not be the answer OP is looking for, but it may still be useful for others coming across the question.

Now, the exception here is for clearly-mentioned homework questions. For those I think it's better to make sure that the asked-for approach is followed, but it would still be good to explain "while this is what your instructor is looking for, this approach should be generally avoided for the following reasons", optionally offering alternative approaches for that scenario. I know homework questions are often met with vitriol here but if the asker is up front about it and is trying to understand the problem rather than simply trying to get a community-sourced answer I really don't have a problem with those questions being asked.

7
  • 1
    There are no questions tagged [homework], since it's a meta tag. People can ask homework questions all they want (without the meta tag); nobody's stopping them. It would be nice if they would show their research, but it's not required. Oct 29, 2021 at 16:16
  • 1
    My mistake, I thought that was a valid tag in both places. Still, I feel the same sentiment applies, if the asker is up front in that they are trying to understand an assignment, why they were wrong, etc. as opposed to simply asking for the answer.
    – codewario
    Oct 29, 2021 at 16:19
  • @MisterMiyagi I felt it was necessary as an exception to my answer as a case where offering a more standard or best practice approach wouldn't really fit the bill, since it's ultimately a learning question and we still do need to understand all programmatic constructs, even if some of them only have a niche positive use case. Beyond that, I do feel that if unstated as an assignment/homework/etc, it is acceptable to steer the asker away from oncoming traffic if you can see the headlights in the distance (get them going in a better direction before they go too far down a problematic path).
    – codewario
    Oct 29, 2021 at 17:02
  • 1
    @MisterMiyagi Of course not. But if someone comes to SO about a homework problem or trying to understand the result of their work, it doesn't really benefit the asker if an answerer throws away the requirements and says "do it this way instead". This is the same sentiment as with IRL requirements, but student requirements are much less flexible than work requirements. Either way, it is still good to explain why required ABC approach is bad, or explain cases where it is appropriate. Whether it's hw or not though if there is a better approach I would hope a good answer includes that as well.
    – codewario
    Oct 29, 2021 at 17:12
  • Conversely, if I feel a normal (as in non-homework/assignment) question is asking for help with a bad solution, such as trying to implement a substitution cipher in place of encryption, I'm not going to give them that answer and will steer them in the correct direction. I might not give them all the code but I will arm them with the knowledge on how to start researching the correct/safe/best practice way of achieving their goal.
    – codewario
    Oct 29, 2021 at 17:17
  • OP asked a question and I gave an answer which fits my view of the abstract question they are asking. If they weren't looking for an answer then why ask the question?
    – codewario
    Oct 29, 2021 at 17:19
  • @MisterMiyagi Sure, but that was only an example of this meta question. The abstract question here is whether "X approach is bad and here is why" is a valid answer to a question. OP happens to disagree with me here but that is okay, and is why I explained more in my answer about the nuance of my stance.
    – codewario
    Oct 29, 2021 at 17:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .