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.
I'd disagree... If it never will receive anything other than 0 and 1, that would anyway be all the more reason to raise an Exception in that case.
Who knows, maybe later down the line the same function will be reused with a different data source, where "True" is only guaranteed to be non-zero, and the function gets reused by a collaborator.
I've seen the "ignore unexpected inputs silently" anti-pattern in our own production code. E.g. (made up example)
SELECT CASE (type)
CASE(TYPE_A, TYPE_C, TYPE_D)
CALL handle_typeclass_1()
CASE(TYPE_B, TYPE_E)
CALL handle_typeclass_2()
DEFAULT ! TYPE_F, TYPE_G
CALL handle_typeclass_3()
END SELECT
Guess what will happen when TYPE_H is introduced and should be handled by handle_typeclass_2? Better would be to have
The approach I've found in the code shifts detection of the missing case to detailed evaluation of the results during testing, with no indicator of the source of the issue; The "error message" variant makes it much more obvious, that something is wrong, and where.
Not sure with C#, but as far as I know Java is the only language that does that. And, though it sounds nice on paper to declare the possible exceptions, the decision was ultimately frowned upon due to the real-world outcome being a lot of
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.
The reason I did not make a unity app was because A) I wanted a standalone app B) I wanted a easy to code GUI (designer) C) when I created the project, I did not even think about a unity app
What am I making? I save data modifier for a unity game, with poor coding.
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.
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.
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.
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?!?!
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.
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)…
254
u/Ok-Mulberry-4600 Feb 02 '22
Seems like someone was keen to use a switch statement
39
u/OriAfias Feb 02 '22
so 2 is false?
140
u/catithebathtub Feb 02 '22
th- this isn't just bad, it's wrong
132
u/joske79 Feb 02 '22
You should add cases for all possible int values.
56
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Feb 02 '22
Image Transcription: Code
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?
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.
6
u/ArfieCat Feb 03 '22
awesome, this is just what i needed!
4
u/Alhoshka Feb 03 '22 edited Feb 03 '22
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:
^ that seems
betterefficient :P1
u/ososalsosal Feb 03 '22
Wat
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
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;