Agreed! Kent points this out in his article too that his downfall was using contacts.length in his code rather than contacts.length > 0 or !!contacts.length or Boolean(contacts.length), not necessarily the usage of the && operator.
agree, but I think it is not worth to have think every time how and why you use one pattern or the other. I prefer to reduce the amount of things I need to keep in mind (given they are already countless) and stick with one pattern that works for all
Yeah but all the gotchas the article is speaking about are misuses of the operator &&. If you really know the operator and just don't throw it everywhere without thinking one second (which you should always do, like in any language, especially in JavaScript), then you'll never have any problem.
Plus this shortcut is really readable. Most of the time, you don't ask yourself if it works.
For the length thing, I think you should basically never use the shortcut where you simply test its truthiness to know if the array is empty or not.
And for the error about returning undefined with { error } as a function argument, you just have to know that object destructuring can return undefined.
Anyway, do what you want, but I always use it without any issue.
If you really know the operator and just don't throw it everywhere without thinking one second
React's entire development philosophy (at least how it appears to me) is to craft an API that prevents developers from shooting themselves in the foot. Sidebar: Google could learn a thing or two from them.
Best practices are best practices for all levels of developers, not just the experts.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Agreed! Kent points this out in his article too that his downfall was using
contacts.length
in his code rather thancontacts.length > 0
or!!contacts.length
orBoolean(contacts.length)
, not necessarily the usage of the&&
operator.agree, but I think it is not worth to have think every time how and why you use one pattern or the other. I prefer to reduce the amount of things I need to keep in mind (given they are already countless) and stick with one pattern that works for all
Yeah but all the gotchas the article is speaking about are misuses of the operator &&. If you really know the operator and just don't throw it everywhere without thinking one second (which you should always do, like in any language, especially in JavaScript), then you'll never have any problem.
Plus this shortcut is really readable. Most of the time, you don't ask yourself if it works.
For the
length
thing, I think you should basically never use the shortcut where you simply test its truthiness to know if the array is empty or not.And for the error about returning undefined with
{ error }
as a function argument, you just have to know that object destructuring can return undefined.Anyway, do what you want, but I always use it without any issue.
React's entire development philosophy (at least how it appears to me) is to craft an API that prevents developers from shooting themselves in the foot. Sidebar: Google could learn a thing or two from them.
Best practices are best practices for all levels of developers, not just the experts.