508
u/EIGRP_OH Mar 12 '21
Hah this looks very intentional
86
u/vinivelloso_ Mar 12 '21
And funny :D
48
u/rocketman0739 Mar 12 '21
GOTO Statement Considered Kawaii
14
u/EdgarDrake Mar 12 '21
GOTO Statement Considered Kawaii, Considered Kawaii
1
u/AcharyaShri07 Mar 23 '21
GOTO Statement Considered Kawaii, Considered Kawaii, Considered Ka.... What.. heck..
break;
246
126
u/geon Mar 12 '21
I didn’t know you could goto cases.
179
u/ZainVadlin Mar 12 '21
This knowledge is best forgotten.
18
u/Speterius Mar 12 '21
Couldn't it be useful (and actually good practice) for state machines?
27
u/ZainVadlin Mar 12 '21
How would that be better than a standard switch inside a loop?
How would that even work? Normally the switch statement switches the state, but goto would avoid that altogether. So now you're not using your state variable and you lose tractability. Furthermore, what would be the point of the switch statement at all?
1
u/zenogias255 Mar 13 '21
In this case switch is a wrapper for containing effective states or state like concepts which are represented by the block of code in each case. You can think of the list of cases as a list of states.
1
u/ZainVadlin Mar 13 '21
That just explains a standard state machine using switch. How would goto's improve that? What variable are you switching if you're just jumping around cases all the time?
1
u/zenogias255 Mar 13 '21
Oh sorry, I’m not saying it’s an improvement necessarily, just a different way to model a state machine. In this case the switch/case is not switching a variable at all, it’s just an inventory of states and their corresponding code blocks. The “goto case X” statement is effectively a “switch to state X” in this model.
I didn’t mean to say it was an improvement versus any other way to model a state a machine, simply that it is a way to model one.
0
Mar 12 '21
[deleted]
14
u/gonengazit Mar 12 '21
I know of one time where goto is fine to use, when breaking out of a nested loop (goto end is much cleaner then using a ton of flag variables)
1
u/cheezballs Mar 30 '21
Na, just use break;
3
u/gonengazit Mar 30 '21 edited Mar 30 '21
That would only break out of a single loop though. Consider the following case
for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ if (condition){ //exit both loops } } }There’s no real elegant way to do this in most languages without goto
2
u/andouconfectionery Apr 10 '21
There is a break label construct in Java. You can label the outer loop and use a break statement.
Or, yknow, use a return statement. And a function.
2
u/gonengazit Apr 10 '21
You’re right, in Java there’s no reason to use this, because of the break label (which is basically a glorified goto). Functions are an option, but can also get quite messy if you need to change some variables defined before your loops
1
u/AutoModerator Mar 30 '21
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/gonengazit, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/cheezballs Mar 30 '21
Man, maybe I just suck but I've never needed to break out of a loop at a different level than I was in. I dunno, I suppose I'd need to see the use-case but I've never, in 15 years, had to do that. Then again, I'm largely a Java/C# kinda guy. Maybe in lower languages stuff like that is more common but I've never encountered a need for it yet.
10
u/ArchCypher Mar 12 '21
I sometimes see them used when a function has a number of reasons to potentially exit early, but always needs to run some cleanup before returning.
So they'll use "goto exit" instead of "return", which just jumps to an exit tag at the end of the function.
I don't know if that's best practice, but it's at least reasonable.
14
u/ZainVadlin Mar 12 '21
This is a semi-standard practice in C code programming because there are no try-catch blocks or other forms of error checking built-in and so they need to be built.
I am a C Programmer and this is one of the two cases I believe goto's are appropriate. (Although I normally wouldn't say this out loud, unless I was aiming to rile the crowd).
5
u/Speterius Mar 12 '21
Haha fair enough. I'm a python guy so switch statements haven't crossed my path yet. Although python 3.10 is introducing pattern matching.
So then the conclusion is that Goto is a great thing to use to complete the monthly Bad Coding Challenge!
1
u/Celdron Jun 16 '21
As others have pointed out there are particular cases where they are necessary. A great example of this is the implementation of co-routines or async/await in C#. Jon Skeet has a great series about both on his blog. But basically, because the state machine needs to be able to jump in and out of any context, including being able to jump out of a loop and back into it once the result is available, there is no alternative to
goto.1
u/PepperCheck Jun 16 '21
I figured there would be edge cases like this where it’s absolutely necessary. Of course, it’s not so bad when the code is well documented and well tested for each jump.
1
u/Maju-Ketchup Mar 12 '21
My university professor insisted of using Gotos for state machine since for this case it actually makes a lot of sense.
1
u/Mr2-1782Man Mar 12 '21
No.
If you've got a state machine you're better off just doing a regular set of switch statements. Once you do state minimization you end up with a table anyway so it'll translate a bit more directly with the added bonus that the code doesn't end up spaghetiifiying.5
u/Nall-ohki Mar 12 '21
It's actually very useful when you have cases that are the same as a common base case with slightly different preconditions.
2
1
u/jaysuchak33 ifelseifelseifelseifelseifelseifelseifelseifelseifelseifelse Mar 18 '21
Yeah I thought you could only break;
69
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 12 '21 edited Mar 12 '21
Image Transcription: Code
using System;
using System.Threading;
class OwO_whats_this
{
static void Main()
{
int OwO = 1;
switch (OwO)
{
case 1:
Console.Write("OwO ");
Thread.Sleep(1000);
goto case 2;
case 2:
Console.Write("UwU ");
Thread.Sleep(1000);
goto case 1;
}
}
}I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
85
u/AyoBruh Mar 12 '21
I really appreciate what you do, but now there’s two copies of this monstrosity of code in the world now.
48
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 12 '21
Hehehe, you've discovered my master plan
0
25
8
u/Tyfyter2002 Mar 12 '21
You missed the space after UwU
3
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 12 '21
Fixed; thanks for pointing it out!
2
Mar 14 '21
Hello fellow transcriber
1
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 14 '21
Oh, hi! :D
2
Mar 14 '21
I should transcribe more but now I'm considering deactivating my account and recreating it because I'm changing names.. is it really worth losing 715 karma tho..
1
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 14 '21
I mean, I'd urge you to go for what's more appealing to you -- does having the karma or having the new name matter more to you? If you figure that out, you've got your answer.
26
u/Syscrush Mar 12 '21
See Marge - I told you C# would let you goto a case label!
I didn't say it couldn't - I said YOU SHOULDN'T!!!
3
u/Giocri Mar 12 '21
To be honest it looks kinda good. For some specific situations.
2
u/Tyfyter2002 Mar 12 '21
Imo it's the best solution for "sub-states" in
cases where object oriented code is obviously a bad choice (i.e. states with only a single function)
10
17
u/cant_dodge_rodge Mar 12 '21
OwO
16
u/danishin99 Mar 12 '21
UwU
17
u/TheGeniki Mar 12 '21
OwO
15
u/alexalexalex09 Mar 12 '21
UwU
28
8
6
u/qoheletal Mar 12 '21
Not a C# guy, what's the "References" in this code here?
14
u/MrTaimander Mar 12 '21
I think the IDE just adds how many times the code references (uses) those methods/classes? Those are not part of the code.
6
3
u/timbar1234 Mar 12 '21
It means a code analysis has shown that nothing else in the code base calls this code. Reasonable, for an entry point.
1
1
0
Mar 12 '21
[deleted]
3
u/Dalimyr codes in C#...poorly Mar 12 '21 edited Mar 13 '21
Yup, it's a feature in Visual Studio...except it's a wee bit borked.
If multiple classes have a method with the same name and parameters, VS doesn't distinguish that there's one reference to ClassA.GetFullName() and there's one reference to ClassB.GetFullName(), instead it'd say above public string GetFullName() for both ClassA and ClassB that there are 2 references.This also applies to any variables belonging to the classes, so enjoy never knowing exactly how many times
ClassA.UIDhas been called compared to any other class that also happens to have a UID field.It can be kind of infuriating at times.
0
u/CHAiN76 Mar 12 '21
ehm wut? I've never seen this behavior in VS and I've used VS for 20 years. If VS could not separate between methods with same name+params for class A and B then FindAllReferences wouldn't work correctly either, which it does in my experience. Will be double checking this.
2
u/Dalimyr codes in C#...poorly Mar 13 '21
Having tested it again, it looks like it's not behaving that way for methods any longer...but does still happen for fields.
1
1
u/Gigibesi Mar 13 '21 edited Mar 14 '21
a comment from me:
i like how a few of you are, amused you learn that you can use goto in a switch statement
PLUS, i think i oughta add Thread.Sleep(1000); before starting the switch statement
5
5
4
4
Mar 12 '21
Is this the source code of OwO bot in discord? ¯_(ツ)_/¯
8
u/LimbRetrieval-Bot Mar 12 '21
You dropped this \
To prevent anymore lost limbs throughout Reddit, correctly escape the arms and shoulders by typing the shrug as
¯\\_(ツ)_/¯or¯\\_(ツ)_/¯
1
1
u/daltonjsm Mar 12 '21
TIL that switch's have a goto command.
2
u/Gigibesi Mar 13 '21 edited Mar 13 '21
yeah well
the keyword
caseanddefaultforswitchstatement is technically considered a label, as such both of them can be a subject to use w/gotokeywordyou can use
gototo jump to anothercase, or straight todefaultwithin the switch statement, or just jump out of theswitchstatement. but you can't usegototo jump IN to aswitchstatement
1
Mar 15 '21
That’s some recursive sleep
1
u/Gigibesi Mar 15 '21
not really
the reason adding
Thread.Sleepmethod was merely to add some delay time in the process
3
7
u/omniscientsudsy Mar 12 '21
I am a lead software engineer at a fortune 10 company, and TIL c# has goto.
5
u/Maeurer Mar 12 '21
If you look through the comments here, apparently it has proper uses when programming a state machine.
2
2
2
u/Promethean18 Mar 12 '21
After 100th time ... I transcended to psychedelic state . Owoooowoooowoooo
1
2
2
2
2
u/plasmarob Mar 12 '21
How not to code full stop.
goto is immoral and should be an international crime.
4
u/rahulfoa Mar 12 '21
People still use goto??? Wtffff
5
5
u/Nall-ohki Mar 12 '21
Goto switch is surprisingly useful.
Sometimes - rarely - a regular goto is actually a clearer solution, but usually requires special circumstances including speed requirements.
3
Mar 12 '21 edited Mar 12 '21
yes.
real examples from my life:
a quick fix for a realsense driver issue was to just put 2 lines and skip some code
---
I've also used it for a bit of code that needed to be fast and was already quite complex. using a goto was easier to read and faster than doing function calls with pointers (however I am not a wizard, so maybe with some super clever tricks or compiler settings, or maybe on different machines the profiling results would be different. no idea)
Interesting reads:
https://www.reddit.com/r/cpp_questions/comments/8k6qxp/when_is_goto_useful/
2
u/Giocri Mar 12 '21
Switch case statement is probably the rare instance in which it can make things clearer and more efficient.
3
1
Mar 12 '21
The goto case 2 can be implicit if you don't use a break
2
u/yaxamie Mar 12 '21
I think C# only let’s you fallthru if there’s an empty body for the case... otherwise it assumes you direct to break or return (or... goto, apparently)
2
Mar 12 '21
I checked the docs, you're right. I must have assumed the fall through behaviours acted like java.
1
1
u/c0de_m0nkey Mar 12 '21
Is the bad code that they don't need a goto in case 1
1
u/Tyfyter2002 Mar 12 '21
They do since C# only supports case fallthrough from empty cases, the bad code is that they should just be using a loop instead of the switch statement and goto, since it's repeating in a set order.
1
Mar 13 '21
I think the last time I've seen someone use a go-to was Fortran or MIPS which calls it jump
1
u/Alex_Lexi Mar 13 '21
I think I used a switch case once and said F it. I never liked how it looked, it’s my personal preference
1
u/ctesibius Mar 13 '21
I’m fairly sure what we are looking at is the skeleton of a finite state machine. Each case is one state, and some condition governs which state you move to next - although in this case there are only two states hence my suggestion that this is a skeleton for a work in progress.
It’s a common idiom to use goto for FSM transitions. You don’t want anything corresponding to a return or iteration, and goto is a good match to such a transition. As far as I know, it’s the only place where it is generally used in modern code.
1
1
u/Turkino Mar 12 '21
Was this written by someone transitioning over from javascript? Because I'm distinctly getting that vibe.
Not the "goto" which is horrible in and of itself, but the use of "1" instead of a bool.
3
0
0
u/dave_thenerd Apr 15 '21
Does this compile? If so, I think Microsoft has some explaining to do!
Also forward this to: ProgrammerAnimemes (reddit.com)
-1
-4
126
u/War-Whorese Mar 12 '21
I don’t get the owo and uwu. What does it mean? Is that some new hip computer lingo?