r/badcode • • Mar 12 '21

c# How NOT to use switch statement

Post image
1.8k Upvotes

165 comments sorted by

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?

296

u/LinuxGeek747 Mar 12 '21

*Sits down and grabs a popcorn, waiting for replies to this comment*

139

u/Marcusaralius76 Mar 12 '21

I'm just gonna sit here, and wait for the furries (of which I am not) to out themselves.

260

u/[deleted] Mar 12 '21

*boops chu wif my fuzzy wuzzy paw* hewwo!! UwU *nuzzwey wuzzwes you wif my adowabwe fuzzy muzzy and huggies da fwuff out of chu~~ *ahem* I have com to expwain da meaning of OwO and UwU. its juwst my cute face :3 U,,w,,U *wags my taiw and barks woof woof

80

u/funkless_eck Mar 12 '21

notices your bulge and musk

65

u/Marcusaralius76 Mar 12 '21

Back! Back, I say! Back to the pit from whence you came!

1

u/[deleted] Mar 14 '21

No.

32

u/-_BABASURA_- Mar 12 '21

I though I was ready for this, oh boy, I wasn’t.

24

u/random-string Mar 12 '21

username checks out

10

u/averagecrazyliberal Mar 12 '21

I have no idea what's happening here but I love it

30

u/Giocri Mar 12 '21

OwO fox ear peaks out of a corner

12

u/Marcusaralius76 Mar 12 '21

Back! Back, I say! Back to the pit from whence you came!

9

u/Giocri Mar 12 '21

Goes back to his den to prepare some pizza.

41

u/Skipcast Mar 12 '21

It has nothing to do with furries does it? Or atleast that's not the origin as far as I know (which admittedly is not a lot)

35

u/Maks244 Mar 12 '21

No but they use it a lot

9

u/Skipcast Mar 12 '21

Ahh i see

17

u/_PM_ME_PANGOLINS_ Mar 12 '21

They’re animal faces

21

u/nicksfurry Mar 12 '21

i mean its not like we’re hiding uwu

15

u/[deleted] Mar 12 '21

hewwo bwothew owo *pokes ur fuzzes

22

u/nicksfurry Mar 12 '21

i take it back maybe we should be hiding

2

u/[deleted] Mar 13 '21

furries make the internet go

nyaa

well that and trans people

1

u/dexter3player Mar 31 '21

furries make the internet go

Mmh, I see you're a purrson of culture.

1

u/[deleted] Mar 14 '21

Yes much

0

u/[deleted] Mar 12 '21

[deleted]

47

u/uxp Mar 12 '21

The Venn Diagram of those two groups can frequently overlap significantly.

-2

u/JohnMcPineapple code! Mar 12 '21 edited Oct 08 '24

...

33

u/FinalRun Mar 12 '21

68

u/uwuwizard Mar 12 '21

· · · Bleep bloop, I'm a bot. Comment requested by u/FinalRun

I don’t get teh uwu awnd uwu. What does iwt mean? Iws dat some new hip c-computew wingo?


If you think this comment does not belong here, reply with "delete" (blacklisted users cannot delete)

Tag me to uwuwize comments uwuwizard (Info, Request disable)

14

u/erinyesita shameless Mar 12 '21

keep

-6

u/william_323 Mar 12 '21

delete

2

u/uwuwizard Mar 12 '21

Popular comments require manual deletion. Comment will be reviewed soon by the bot's operator.


Tag me to uwuwize comments, ain't much but it's honest work uwu uwuwizard .~³

0

u/[deleted] Mar 12 '21

[deleted]

13

u/uwuwizard Mar 12 '21

Popular comments require manual deletion. Comment will be reviewed soon by the bot's operator.


Tag me to uwuwize comments, ain't much but it's honest work uwu uwuwizard .~³

43

u/LoafyLemon Mar 12 '21

Rawr x3 *nuzzles* how are you *pounces on you* you're so warm o3o *notices you have a bulge* o: someone's happy ;) *nuzzles your necky wecky~* murr~ hehehe *rubbies your bulgy wolgy* you're so big :oooo *rubbies more on your bulgy wolgy* it doesn't stop growing ·///· *kisses you and lickies your necky* daddy likies (; *nuzzles wuzzles* I hope daddy really likes $: *wiggles butt and squirms* I want to see your big daddy meat~ *wiggles butt* I have a little itch o3o *wags tail* can you please get my itch~ *puts paws on your chest* nyea~ its a seven inch itch *rubs your chest* can you help me pwease *squirms* pwetty pwease *sad face* I need to be punished *runs paws down your chest and bites lip* like I need to be punished really good~ *paws on your bulge as I lick my lips* I'm getting thirsty. I can go for some milk *unbuttons your pants as my eyes glow* you smell so musky :v *licks shaft* mmmm~ so musky *drools all over your cock* your daddy meat I like *fondles* Mr. Fuzzy Balls hehe *puts snout on balls and inhales deeply* oh god im so hard~ *licks balls* punish me daddy~ nyea~ *squirms more and wiggles butt* I love your musky goodness *bites lip* please punish me *licks lips* nyea~ *suckles on your tip* so good *licks pre of your cock* salty goodness~ *eyes role back and goes balls deep* mmmm~ *moans and suckles* o3o

34

u/Steelsfix Mar 12 '21

Kill me now

24

u/alexalexalex09 Mar 12 '21

93

u/War-Whorese Mar 12 '21

Thank u vewy muwch, kind stwangew. I get it now.

77

u/alexalexalex09 Mar 12 '21

Oh dear God what have I done

20

u/ThePsion5 Mar 12 '21

"It is too late mother, I've already seen everything."

21

u/alexalexalex09 Mar 12 '21

"it is too wate mothew, i've awweady seen evewything."

8

u/Teln0 Mar 12 '21

Those are emotes, the w is a mouth

1

u/Gigibesi Jun 16 '21

sorry for late comment but

if only i can pin your comment xd

2

u/War-Whorese Jun 18 '21

UwU thanks OP.

4

u/Thriven Mar 12 '21

I imagine how you speak to a baby and then imagine speaking to your best friend that way and they reciprocate in that manner.

owo and uwu are emoticons with the o's and u's are the eyes expressions I believe.

Unfortunately it goes further with a whole language of altered words as you'd speak to a baby.

Ron Swanson would not approve.

5

u/genitalgore Mar 12 '21

they don't really mean anything, they're just text emoticons

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

u/xSilverMC Mar 12 '21

Because while(true) was too much effort

65

u/speyck Mar 12 '21

for( ; ; )

93

u/[deleted] Mar 12 '21

[removed] — view removed comment

13

u/[deleted] Mar 14 '21 edited Mar 14 '21

for( ;/*-*/; )

;-;

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

u/[deleted] 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

u/[deleted] Mar 13 '21

Switch statement is Turing complete

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

u/[deleted] Mar 12 '21

[deleted]

10

u/FeniulaPyra Mar 12 '21

They aren't a bot

25

u/Emix98 Mar 12 '21

Good human

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

u/[deleted] Mar 14 '21

Hello fellow transcriber

1

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 14 '21

Oh, hi! :D

2

u/[deleted] 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

u/Az-Bats Mar 12 '21

So this is the code bomb which defeated the Borg.

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

u/5p4n911 Mar 12 '21

Yeah, it's just Visual Studio.

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

u/Max5923 Mar 13 '21

Basically, its how many times you’ve mentioned the function.

1

u/qoheletal Mar 15 '21

I wonder if emacs has something like this for Python

0

u/[deleted] 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.UID has 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

u/Maeurer Mar 12 '21

it is! I can recommend Visual Studio 2019 a lot!

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

u/gnobling Mar 12 '21

I see nothing wrong here

5

u/TheGoldenMinion Mar 12 '21

you can do that? i never knew also what the fuck

4

u/Mango-D wicked Mar 12 '21

But what if OwO is modified by another thread?

4

u/[deleted] 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 ¯\\_(ツ)_/¯

^^Click here to see why this is necessary

1

u/[deleted] Mar 12 '21

Idk this looks good to me. Kinda makes me doubt your ability

1

u/Gigibesi Mar 12 '21

i'm sorry...

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 case and default for switch statement is technically considered a label, as such both of them can be a subject to use w/ goto keyword

you can use goto to jump to another case, or straight to default within the switch statement, or just jump out of the switch statement. but you can't use goto to jump IN to a switch statement

1

u/[deleted] Mar 15 '21

That’s some recursive sleep

1

u/Gigibesi Mar 15 '21

not really

the reason adding Thread.Sleep method was merely to add some delay time in the process

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

u/CyraxSputnik Mar 12 '21

AwA de UwU

2

u/aramayis_ Mar 12 '21

Found one case how to not use switch case

2

u/Promethean18 Mar 12 '21

After 100th time ... I transcended to psychedelic state . Owoooowoooowoooo

2

u/w0lven Mar 12 '21

Switch switch bish Another one in the brackets

2

u/[deleted] Mar 12 '21

Ahh, the switch loop

2

u/nathan_lesage Mar 12 '21

The code: UwU

Me: (╯°□°)╯︵ ┻━┻

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

u/cashewbiscuit Mar 12 '21

Languages still have goto?

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

u/[deleted] 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/

http://david.tribble.com/text/goto.html

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

u/mthaBOSS Mar 12 '21

I lost it when i saw the light mode

1

u/[deleted] 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

u/[deleted] Mar 12 '21

I checked the docs, you're right. I must have assumed the fall through behaviours acted like java.

1

u/[deleted] Mar 12 '21

Why is it all on the same line. That annoys me

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

u/[deleted] 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

u/OkPreference6 Coding The Declaration of Independance Mar 12 '21

What the fuck

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

u/skabde Mar 12 '21

1 would be ok, it‘s the 2 that‘s messed up.

1

u/Turkino Mar 12 '21

oh wow, I didn't even notice that.

0

u/Maeurer Mar 12 '21

can they please remove goto from c#? it only brings pain

1

u/Jrbdog Mar 12 '21

Also remove it from every other language while they're at it.

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)

-4

u/[deleted] Mar 12 '21

How NOT to be funny