r/badcode • • Feb 02 '22

c# Oh hey, its more of my code.

Post image
902 Upvotes

95 comments sorted by

254

u/Ok-Mulberry-4600 Feb 02 '22

Seems like someone was keen to use a switch statement

33

u/[deleted] Feb 03 '22

Just an FYI, the code is meant to convert unity playerprefs bools Into my apps bools, unity stores these bools as int32 hence my code.

30

u/R3D3-1 Feb 03 '22

But why equate 2 with false? The usual convention I know is "0 is false, everything else is true".

I'd either expect this to be followed or the default case to raise an Exception (if strictly only 0 and 1 are expected).

Though you did post in in r/badcode, so maybe that's the point of the post?

1

u/[deleted] Feb 03 '22

Let me explain, this is meant to get the numerical value of what a bool would be, 1 = true, 0 = false, the default was just there cuz I thought it would be useful.

→ More replies (1)

1

u/[deleted] Feb 05 '22

Well, since you wouldn't have a bool value for 2, it's either on or off, true or false.

2

u/R3D3-1 Feb 05 '22

... still confused what you mean.

Maybe 2 is FILE_NOT_FOUND?

→ More replies (2)
→ More replies (4)

8

u/ciknay Feb 03 '22

I've personally found that using getters/setters for playerprefs works really nicely.

public bool storedBool
{
    get { return PlayerPrefsGetInt("nameOfPref", 0) == 1; } 
    set { PlayerPrefsSetInt("nameOfPref", value ? 1 : 0);
}

That way you store the value as an int, but do none of the legwork to convert the incoming values and can use the variable without needing to fuck around with it.

1

u/[deleted] Feb 05 '22

Sorry mate, I tried, I am using .NET 6.0. I cannot use the unity library

2

u/ciknay Feb 05 '22

The same principle applies though.

That being said, I'm real confused why you're grabbing unity player prefs but not developing a unity app. Out of curiosity, what is it you're making?

→ More replies (1)

78

u/XDracam Feb 03 '22
public static bool ToBool(this int input) => input == 1;

51

u/ChocolateBunny Feb 03 '22

I think that was their first problem.

1

u/McKapucna Feb 03 '22

I fuckin love switch statements. I put them everywhere but then i realize i just needed to make a fucking list or smth and i get so upset.

39

u/OriAfias Feb 02 '22

so 2 is false?

7

u/[deleted] Feb 03 '22

This was meant to convert registry keys to bools, like if a reg key was true or int32 one, this would return true.

2

u/MeatyLabia Feb 03 '22

In C# when you use Convert.ToBoolean 1 is true and every other value is false.

3

u/Dealiner Feb 05 '22

That's not true, only 0 is false.

→ More replies (2)

140

u/catithebathtub Feb 02 '22

th- this isn't just bad, it's wrong

32

u/[deleted] Feb 02 '22

Funny thing is: it works.

96

u/Willinton06 Feb 02 '22

Cause not only is it wrong, but you’re also using it wrong, so it evens out

6

u/FrezoreR Feb 03 '22

So, two wrongs makes a right after all?

13

u/Willinton06 Feb 03 '22

false && false == true

16

u/GaiasWrath Feb 03 '22

Sounds like a Javascript feature

44

u/Knuffya Feb 02 '22

it is wrong though.

int(34) should be true. just as any integer > 0

5

u/Xiaopai2 Feb 03 '22

Why? That's just convention.

→ More replies (1)

1

u/0Flight64 Feb 03 '22 edited Feb 03 '22

Not exactly. The language looks like Java to me. At least in Java 8, 1 is not equivalent to true and 0 is not false. Even in the tutorials I see geeksforgeeks, they are using an if - else statement for the conversion. Edit: Just realised this is C#. Anyway, same issue here so. Implicit conversion from int to bool is not allowed.

4

u/MrBananaStorm depraved Feb 03 '22

It's C#, and apparently they are using the unity engine which stores bools as 0 or 1 integers, which is why this works. They are only converting that 0 or 1 to false or true, in theory it should never be a 2 or 3, if it is, something is messing up elsewhere.

Doesn't excuse the fact it's still badcode tho lol.

→ More replies (4)
→ More replies (9)

42

u/[deleted] Feb 02 '22

I think it's more conventional for the default case to be true

8

u/[deleted] Feb 03 '22

Not really, booleans are typically stored as a byte, and it's value can range from 0 - 255. 0 is false, and everything else is normally true. In this instance, everything but 1 is false.

3

u/RFC793 Feb 03 '22

What environment normally stores integers as a byte? Yes, it is never a “bit” as that isn’t addressable. More typically the machine’s word size, 32 bit, or the min(32, word size)…

10

u/[deleted] Feb 03 '22

Let's all be thankful that bit-endianess is generally only a headache of hardware developers.

→ More replies (1)
→ More replies (2)

4

u/Biyama Feb 02 '22

It works as long as no one shots you in the leg.

132

u/joske79 Feb 02 '22

You should add cases for all possible int values.

18

u/Eluvatar_the_second Feb 03 '22

Enter source generators

1

u/aaaantoine Feb 03 '22 edited Feb 03 '22
foreach (int i in Enumerable.Range(int.MinValue, int.MaxValue - int.MinValue))
    Console.WriteLine($"case {i}: return {i == 1};");

Edit: nope. Trying again.

int i = int.MinValue;
do
{
    Console.WriteLine($"case {i}: return {(i == 1).ToString().ToLower()};");
    i++;
} while (i != int.MinValue);

2

u/ExceedingChunk Feb 19 '22

You can optimize it by creating a hashmap of all int values as keys and all the boolean outputs as value!

56

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Feb 02 '22

Image Transcription: Code


public static bool ToBool(int input)
{
    //woooo switch case
    switch (input)
    {
        case 0:
            return false;
        case 1:
            return true;
        default:
            return false;
    }
}

I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!

13

u/NTheDev Feb 02 '22

Why are you like this?

9

u/Roxfall Feb 02 '22

A better question is who hurt them.

10

u/[deleted] Feb 02 '22

The comment had me rolling

8

u/tony7797176 Feb 02 '22

I guess its not a big deal; “ if (1) return true; return false; “ maybe just be a little simpler.

46

u/hareppas Feb 02 '22

or just return input == 1;

11

u/tony7797176 Feb 02 '22

Good point lol. I just wrote bad code lol

9

u/SoulArthurZ Feb 02 '22

you didn't write bad code, it could just be simplified to return input == 1

→ More replies (1)

6

u/ArfieCat Feb 03 '22

awesome, this is just what i needed!

public static bool IsEven(int input)
{
    if (!ToBool(input % 2))
    {
        return false;
    } 
    return true;
}

4

u/Alhoshka Feb 03 '22 edited Feb 03 '22

"woooo switch case"

I take it you were just beginning your learning journey?

That's not bad code, that's adorable.

.

it's still bad code though, don't do that

3

u/TheJP_ Feb 03 '22

on the more c# side of this, i'm annoyed this isn't an extension method

3

u/[deleted] Feb 03 '22

woooo switch case

3

u/poor_leno Feb 03 '22

woooo switch case

1

u/[deleted] Feb 03 '22

Did you try:

if(input == 0) {
return false;
} else if(input == 1) {
return true;
} else {
return false;
}

^ that seems better efficient :P

6

u/[deleted] Feb 03 '22
return input == 1;

3

u/[deleted] Feb 03 '22

Hey hey.. we are suppose to have a bad code!

1

u/blankman0230 Feb 03 '22

Big brain does, what big brain think is cool to do

1

u/[deleted] Feb 03 '22

I love how Visual Studio even highlighted that the switch case is absolutely unnecessary lol

1

u/MeatyLabia Feb 03 '22

It is suggesting to convert to a switch expression. Not unnecessary at all.

1

u/[deleted] Feb 04 '22

I think you misread my comment. I meant that the switch case is unnecessary, not the highlighting.

1

u/MeatyLabia Feb 04 '22

You said vs highlighted that the switch case is unnecessary, which it didnt highlight it for.

1

u/MeatyLabia Feb 03 '22

OP just did his own implementation of Convert.ToBoolean.

1

u/[deleted] Feb 03 '22

hmmm painfully wrong

1

u/salmjak Feb 03 '22

Return input as Bool

1

u/Ambitious_Medium_625 Nov 09 '22

Why do an if statement when you can use a switch statement and overcomplicate your code? That's a big play right there.

0

u/zsharp68 Feb 03 '22

return (input == 1) ? true : false;

6

u/Deadly_chef Feb 03 '22

Why the ternary when the result is a boolean...

3

u/zsharp68 Feb 03 '22

because i did a dumb

1

u/Deadly_chef Feb 03 '22

It happens

4

u/[deleted] Feb 03 '22

return input == 1;

1

u/MeatyLabia Feb 03 '22

return Convert.ToBoolean(input);