Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Don’t worry too much. It’s a trap that many of us walk into.
That’s the reason why some of the good languages got rid of implicit conversions to bool. -
@happygimp0 in JavaScript, 0 evaluates to false. I was going to use that number to retrieve a value from the array, which would have been a bug because it's a valid index number but falsy value.
-
C0D4681383y@happygimp0
bar is set to 0 due to the findIndex() returning 0 as the index.
If(bar)
is checking 0 against true.
In Javascript 0 is false. -
Grumm18123y@C0D4 but why would you check against a 0 ?
You know you are working with javascript so you should know that 0 = false but 0 is a valid index.
Javascript is know for this type of behavior.
Never assume implicit conversion is doing what you think it is doing. -
Also in C/C++, 0 is both a valid array index and a boolean false. Completely normal.
-
what does it return when it's not present? isn't -1 the universally accepted standard for fallback index?
-
@Fast-Nop Maybe not that normal since I can only think of JS and C/C++.
Are there any other examples? Php maybe (not sure). -
@theabbie traditionally, -1, yes. But the modern way would be to return null or something similar.
-
@Lensflare Not sure either. Even in C, I compare to 0, '\0', or NULL to clarify which kind of 0-ish comparison is going on: value range check, string terminator, or pointer check.
The only exception are variables intended for purely boolean usage anyway. -
@Grumm you are missing the point, they forgor to check if it's not -1.
Also hecc u i will rely on implict conversions even more -
There is no excuse for not doing an explicit identity comparison when your variable isn't actually a boolean.
Don't shoot yourself, always use explicit comparisons and conversions. -
@saintograph Yes, false is a fancy way of saying 0, no question there.
I don't understand this part: foo.findIndex(x => x === 'a')
And why do you check if bar is not 0? -
@C0D4 I don't see the problem. And i still don't understand this part: foo.findIndex(x => x === 'a')
I mean, yes he gets some kind of index and does something with it when the index is not 0, but i don't understand why. What is special about index 0? -
@happygimp0 I reckon I wrote that due to force of habit. Writing JavaScript means having to constantly do type checking, something which switching to Typescript mitigates to a certain degree.
-
@saintograph Yes. I am sorry, but i still don't understand what your mistake was. You say it has something to do with types, but i only see 2 types: The array of chars/strings (don't know enough about JavaScript to know if there is a difference) and a integer which can be 0 (of false, if you want to call it that) or non-zero.
-
@happygimp0 I do know that '\0', NULL and 0 are the same in terms of code, but conveying which kind of 0-comparison is going on makes the code easier to understand for the next dev or even myself after a few months. It's about maintainability.
Of course there is "false" in C, it's in <stdbool.h>. -
athlon173223yI would respond with “what idiot calls their variables ‘foo’ and ‘bar’ - he should not be allowed to even look at the repo”.
-
@saintograph Ok, now i understand. You didn't want to check for the index but something else, correct?
Sorry, but your rant is a bit unclear in this regard. When i see that code i think the purpose is to check for index==0 vs something else. I think it is really bad decision to set invalid index to false. False and 0 is exactly the same in many (most?) languages. -
When an index can't be found, it's assumed -1, he skipped a mind-step and left it as just the result.
-
Grumm18123yYou could do something like this is :
foo.includes => returns a valid bool true or false
Then if true, findIndex.
But again, learning that any array has always a zero-based index is a must.
bar > -1 will be quicker
I almost got caught by this during an interview:
const foo = ['a', 'b'];
const bar = foo.findIndex(x => x === 'a'); // 0
if (bar) { // I'm an idiot
console.log('Do something');
}
🤦♂️
rant