r/badcode • • Feb 02 '22

c# Oh hey, its more of my code.

Post image
902 Upvotes

95 comments sorted by

View all comments

Show parent comment

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.

1

u/cheeseDickies Feb 21 '22

It just plain makes more sense to have every other value, including negative ones be true besides 0. 0 is the only number where every bit is set to 0, every other value has at least one bit set to 1. This is even reflected in Assembly where you have the jnz (jump if not zero) and the jz (jump if zero) instruction.

0

u/[deleted] Feb 03 '22

[deleted] — view deleted comment

-3

u/[deleted] Feb 03 '22

[removed] — view removed comment

3

u/[deleted] Feb 03 '22

C# is perfectly capable of converting int to bool.

int num = 1;
Convert.ToBoolean(num);
// or
bool result = num == 1;

There's no reason that conversion should be implicit. Bool and Int32 are completely different types with no relation to each other.

-3

u/Knuffya Feb 03 '22

I've quickly tried (bool)34, which failed.

→ More replies (5)

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.

3

u/0Flight64 Feb 03 '22

Agreed. You know the worst part? This link is the first one that comes up of you search "convert int to bool c#". Check the second method. https://www.delftstack.com/howto/csharp/convert-int-to-bool-in-csharp/#:~:text=the%20Convert%20class.-,The%20Convert.,equivalent%20to%20true%20in%20boolean.

5

u/MrBananaStorm depraved Feb 03 '22

Hey, at least OP has a default case

1

u/Knuffya Feb 03 '22

Then it should throw an exception. Then true and false are both wrong results.

→ More replies (1)

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.

1

u/Dumfing Feb 03 '22

And audio developers, and network developers

1

u/[deleted] Feb 03 '22

C++ stores booleans as a byte

6

u/RFC793 Feb 03 '22

Your comment originally said “ints” and you changed it to “booleans” after my reply.

But, yes, bool in C++ is explicitly defined as one byte.

4

u/Biyama Feb 02 '22

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