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

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?!?!