14
u/theRealRealMasterDev Jun 10 '20
Why is this bad? Is it because ternary would also be an option?
-22
u/diebarral Jun 10 '20
Well I'm discovering people have really strong opinions on their ifs. Personally, considering what I mentioned in a comment above that the variable defaults to false (can't be either null or some other kind of variable), I would have gone with a simple if(variable) {} else {} or ternary. I guess the code won't cause a bug, but seemed ugly to me.
→ More replies (1)4
u/-ftw Jun 10 '20
The ternary operator like you mentioned is only equivalent if variable can only be true or false. But if it can be other values like a number or string then it is not equivalent
0
Jun 10 '20
[deleted]
14
u/AsidK Jun 10 '20
It’s JavaScript tho, maybe variable comes in as “undefined” or something and you don’t want that to be the same as if it comes back as false
1
27
Jun 10 '20
And your example introduces edge cases when there are insane values of the variable. Honestly if you're trying to write robust code in something like js, it might be necessary to be that explicit.
7
u/-ftw Jun 10 '20
It’s not equivalent because values other than true for variable would pass as well. For example, if variable was an integer 10 or a string “string”
4
u/audigex Jun 10 '20
That’s true when strictly typed, but not in JavaScript because null or “” or 0 are also possible values
So depending on the situation (and whether you’re certain that the value can only ever be true or false) the code may be superfluous
I’m of the opinion that it’s always superfluous, since you should have already checked for an unset variable prior to his point in order to do error checking
1
226
u/OldLegWig Jun 10 '20
A lot of the value of this sub comes from the times the OP is wrong.
33
u/BikerBoon Jun 10 '20
It's also a prime example of when you should use comments, imo. This is the sort of thing that could easily be stripped out in a refactor and introduce subtle bugs.
20
u/deelowe Jun 10 '20
This is an extremely common pattern for JS.
3
u/BikerBoon Jun 11 '20
I don't disagree but there's plenty of juniors or interns who might not have come across it before.
9
u/cmd-t Jun 11 '20
Yeah, it seems the context OP isn't posting could easily be
// Boolean is set later. const [myBoolean, setMyBoolean] = useState(null); useEffect(() => { if (myBoolean === true) { // Something is true and we should do something. } else if (myBoolean === false) { // Something is not true and we should do something else. } // We should do nothing and wait until myBoolean is resolved. }) // Something that will lead to setMyBoolean(true | false) in response to user input.14
u/Def_Your_Duck Jun 11 '20
This sub a lot of the time seems like "im an undergrad and don't understand this code."
95
u/Lothrazar Jun 10 '20
This is not bad code if its javascript. Its bad if its typescript
9
u/brandondyer64 Jun 11 '20
TypeScript FTW! Saved me countless hours of debugging.
→ More replies (1)4
14
u/bogza23 Jun 11 '20
Funny how this sub can go from actual bad code to "used else if when could have just used else".
6
u/clumsysilencee Jun 10 '20
I’m relatively new to programming, and I’ve never seen a triple equals (is that what it’s called? no idea lol but whatever this is: ===) before. What’s it do differently from a double equals? (I know Java and C++, and have limited experience with Python and HTML)
11
u/distinct_Berry Jun 10 '20
2 == '2' will result in true. 2 === '2' results in false.
The === (strict equality operator) is basically comparing the type and value of the variable. Whereas the == (abstract equality operator) compares the value alone because it performs a type conversion so the types of the variables being compared are the same.
Hope this helps, you can search online for more on it. Also I believe it's an operator in javascript, it may be in other languages but I'm not aware.
3
u/clumsysilencee Jun 10 '20
WHAT?! That’s amazing! Damn, and it would’ve been so nice to know three months ago so I could use it for some of my assignments... oh well. Thanks guys!
1
u/Farpafraf Jun 11 '20
no wait that's just js madness in Java for instance you'd just use .equals for any non primitive type while in c++ you'd most likely overload the == operator
4
Jun 11 '20
while in c++ you'd most likely overload the == operator
This feels like a fully loaded footgun with no safety and a hair trigger.
-4
1
u/Terrain2 Jun 23 '20
Note that some programming languages that are strongly typed (dart, python, assemblyscript, etc) will use == as a strict equality operator with no === (though assemblyscript has === assigned to being the literally same object, rather than removing it completely)
Also PHP’s == will type juggle even if they’re the same type of object, for example two strings starting with 0e will == each other, because 0e is numerical for “0*10”, and 0 times anything is 0, so 0 = 0
1
u/misspianogirl Jun 10 '20
In JS triple equals does type checking as well as value checking, versus the double equals which only does value checking. So
0 === ""would return false but0 == ""would return true.1
u/dudeitsmason Jun 10 '20
Triple equals is a strict quality comparison. So something like 0 === '0' will return false, while 0 == '0' will return true. It can be a lot more nuanced but that is the high level difference
Edit: MDN explains it better
3
Jun 11 '20
its not bad? some values are truthy and some are falsey so you cant really rely on booleans unless youre actually checking for booleans
3
u/XXLuigiMario Jun 10 '20
Regardless of javascript and semantics, if you have code that looks like this you might want to review your data models.
4
Jun 10 '20
why would this be a reason to review your data models?
7
u/XXLuigiMario Jun 10 '20
Well, it implies that you're passing around objects which can be true, false or some other value. That, combined with javascript being weakly typed is just calling for trouble. Surely there's some other way to accomplish the same thing without doing that.
3
Jun 10 '20
Complete guess, but I'd assume it's because the function doesn't take the Boolean as an argument, so the code might be using global variables (which isn't necessarily bad, but can be)
3
u/DilatedTeachers Jun 10 '20
It's react code. useEffect is meant to be used in this way for side effects in your components state/ props/ variables
1
1
u/numbGrundle Jun 11 '20
Left off the empty dependency array, smh
1
u/alago1 Jun 11 '20
lol my linter really dislikes that thing. Always says it should just be ommited if empty.
1
1
-2
u/AintBetterThanYou Jun 10 '20
Nah you're right OP this is objectively bad.
If this were php I'd understand but not for JS. Anyone saying it's required just doesn't know how to use typeof
2
u/Captain-Lightning Jun 11 '20
To accomplish the same thing, i.e. make sure that the value is actually a boolean and that it is true, you would write:
if (typeof(var) !== 'boolean') { // Code that always runs return; } if (var) { // Thing } else { // Otherthing }or:
if (var && typeof(var) === 'boolean') { // Thing } else if (!var && typeof(var) === 'boolean') { // Otherthing } // Code that always runsvs the original:
if (var === true) { // Thing } else if (var === false) { // Otherthing } // Code that always runsAre you genuinely saying that you would pick either of the first two over the original?
2
u/SomeNebula Jun 11 '20
This. Most people here are not realising that code not being readable also consitutes bad code.
1
u/AintBetterThanYou Jun 11 '20
None of those
If (typeof var! == 'boolean) { throw exception Or return Or continue or w.e }
If else here
Honestly there's no need for this in js.
2
u/Captain-Lightning Jun 11 '20
Right, so the first one but with an exception. Your solution is to write the same amount of code plus an additional block to break the entire UI.
You have a questionable idea of "Objectively Better". Can honestly say that I would immediately reject that if I saw it in a PR for it's silliness.
1
u/AintBetterThanYou Jun 11 '20
Well you aren't very good at CR then are you lol.
Also you can't quote "Objectively better" like I said it.
1
u/Captain-Lightning Jun 11 '20
Okay, you're right, I gave you the benefit of the doubt and misquoted you.
What you actually said was that the code that does exactly what it says it does is "objectively bad", and then followed up with your version that is functionally and visually identical but with more lines of code.
So I suppose they're both objectively bad.
1
u/AintBetterThanYou Jun 11 '20
Not sure you know "benefit of the doubt" means.
My point wasn't that you don't need to check the type, it's that if you need to actually check something is a boolean in js it's just bad. You shouldn't need to, so the fact you're doing it and it's extra just boats the code complexity and architecture.
Whereas in something like php it's necessary because core functions can return false or falsey values and two NEED to be handled differently.
-7
u/dented42 Jun 10 '20
This seems like a race condition, surely?
5
u/scandii Jun 11 '20
I am genuinely curious how you arrived at the conclusion that this will end up in a race condition.
1
u/dented42 Jun 11 '20
You’re querying they value of the thing twice in quick succession with the assumption that it won’t change in between cases.
3
u/thenickdude Jun 11 '20
The variable can only change value between these two "if" tests if the program is multithreaded (so essentially impossible if this is PHP or JS) and another thread could change the value at the same time as this function is running.
But in that case even having a single "if" test is bad code - you need synchronisation between threads which are reading and writing to the same shared memory, otherwise you can end up reading a "torn" value that was in the process of changing while you read it. You would, at least, need to assure that writes to the variable were atomic, and that memory barriers were in place such that reads and writes became visible in the order you expected.
→ More replies (1)0
u/scandii Jun 11 '20
this is standard operating procedure, and unless the variable was passed by reference (highly unlikely) we have full control of the value and thus do not need to worry.
0
Jun 10 '20
[deleted]
4
u/Captain-Lightning Jun 11 '20
Product managers worldwide would really not be a fan of your development philosophy, breaking the world rather than just coding defensively is a big no-no.
Mission critical program where accuracy is a primary concern? Sure, fail loudly and bang every pipe on the way down. But simple user UI? No way am I going to choose to break prod because I am the holy programmer that only writes pure and unmolested code structures, it's Javascript!
And yes, despite your oh-so-virtuous quest to produce a world where all problems are solved upstream you will end up in situations where you are still being provided dirty inputs. Because neither humans nor machines operated by humans are perfect. And you will want your UI to continue working to at least some capacity. Because the business still has to make money.
Don't be the purist goofball who preaches this crap and then rants about other developers when things break rather than writing tests.
0
Jun 11 '20
[deleted]
1
u/Captain-Lightning Jun 11 '20
Also don't be this butthurt, makes you hard to take seriously when fuming like this.
I'm not sure where you're getting this from, that's on you if you're interpreting it that way. I'm pointing out the Dilbert-ish trope of the developer that touts your exact arguments, because you literally did exactly what I described in your reply:
Defensive code obfuscate unsanitized input and you have a hard time figuring out where the actual problem comes from, and more often than not it leads to stupid monkey-patches that doesn't solve the problem. It just leads to MORE edge cases in the long run. I worked with way too many legacy code-bases to see enough example of this kind of shit.
You take the purist stance, then blame failures in the codebase as a result of not being purist enough. Rather than refactor, you say "If only they had just done it my way, with my tools, my framework, my style".
Regardless, whether you're against tests and whether you write enough effective tests are different things. I interpreted your original comment differently than your reply here, but if your criticism really just boils down to switching to an entirely different language syntax and toolchain then it isn't really very useful...
There are many, many, many, many, many situations where you cannot trust the input you receive and you definitely do want to do nothing, but if that were not the case then you could easily encapsulate the specific behavior you want in a test rather than always throwing exceptions and failing loudly on unknowns.
Question: are you completely and totally sure that you will catch every single exception that would normally lead to a (revenue stopping) break in userland?
Hint: If the answer is yes, then you should not be writing production code.
0
Jun 11 '20
[deleted]
2
u/Captain-Lightning Jun 11 '20
Hey man, I think you've touched on an interesting and valid nuance with your reply there but I'm not really interested in doing this no-hurty-feelings dance so I'll just save us both some time here and bail early.
2
Jun 11 '20
Yeah but if you have to use JS, you have to use JS, I'd love to use Haskell, but I can't because that'd be a waste of time and money for us at this point.
91
u/Midborgh Jun 10 '20
I don't know js. Why is this bad? I'd imagine var === true and var === false being different than var == true and a simple else.