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 comments

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]

-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.

1

u/[deleted] Feb 04 '22

Obviously it did. Why should such casts be allowed?

1

u/Knuffya Feb 04 '22

Why shouldnt they?!

0

u/[deleted] Feb 04 '22

Because booleans and integers have nothing to do in common.

1

u/Knuffya Feb 04 '22

That doesn't fucking matter if i want to dump the bits of cEmployee into an array of doubles, i'll better be able to do so. That should be explicit but definitely allowed!

1

u/[deleted] Feb 04 '22

Woah man, language.

You can easily just use an equality operator, as I already pointed out, such as this:

cEmployee.SomeProperty == 1 // 1 for true

Your bad implementation doesn't mean the language is poorly designed because it doesn't suit your needs. Booleans are not numbers. Same way strings aren't numbers and you can't cast them to ints.

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.

2

u/[deleted] Feb 03 '22

It's really annoying to see code like this (obviously not within the context of r/badcode when the author realises it's bad, but like actual production code). The System namespace has like a million general-purpose exception classes that can be applied to almost every scenario. Why aren't people using them?!?!