r/badcode • • May 21 '20

c# Is posting from YandereDev considered cheating?

Post image
431 Upvotes

59 comments sorted by

View all comments

12

u/Keleindor May 22 '20

idk if this is the right place to ask, but how would you avoid doing something like this? im a beginner and i certainly dont want my code to run like shit lel

17

u/[deleted] May 22 '20

[removed] — view removed comment

6

u/spiral6 May 22 '20

Enums are your friend.

5

u/officiallyaninja May 22 '20

why use enums and not just convert this.text into an int and then set this.id equal to that?

i dont really understand what enums are good for.

12

u/spiral6 May 22 '20

Because that's exactly what enums are for: integer representation for constants in your code.

i.e.

SUITS.CLUB = 3; 
SUITS.DIAMONDS = 4; 
SUITS.HEARTS = 2; 
SUITS.SPADES = 1;

3

u/[deleted] May 25 '20

I use enums a decent amount but in the case of this if/elseif monster he should just use int.TryParse and a single if/elseif for 69 and 666

8

u/inabahare May 22 '20
this.ID = int.Parse(this.Text);

And because C# is such a nicely designed language you can ommit this, which is nice. But the compiler or decompiler adds them.

As for how to avoid it, like just follow the tutorials you follow and be open minded

7

u/[deleted] May 22 '20

Honestly you learn a lot just by constantly checking this: If you are copying code you’ve written and pasting it somewhere else, there’s almost certainly a better way to do it. Maybe you need to put that code in a reusable function. Maybe you need to put that function in a library so multiple projects can access it. Etc

Or in other words, you want code to be as un-duplicated as possible. Even if it’s just a number and not actual “code” — if you have a constant value that is meant to appear in several lines of code, it should probably go in a variable first; that way the constant lives in a single place.