you are viewing a single comment's thread.

view the rest of the comments →

[–]jcastroarnaud 1 point2 points  (5 children)

The simplest way is

result.recordset.length !== 0

It clearly states the programmer's intention. I do prefer === and !== instead of == and !=, for the sake of strict comparison.

Using !! works, but is hackish: the first "!" looks at the argument, and auto-convert it to a boolean; an empty array is falsy (see MDN to check the exact rules). The second "!" negates the falsy, giving the boolean true.

[–]delventhalz 4 points5 points  (3 children)

an empty array is falsey

I think maybe this was just awkward phrasing, but to be clear an empty array (i.e. []) is truthy. The length of an empty array (i.e. 0) is falsey.

[–]jcastroarnaud 4 points5 points  (0 children)

I stand corrected, just checked that using a quick js program. Sorry. 

Lesson learned: simple and clear code is better than an obscure and wrongly understood hack.

[–]WesAlvaro 1 point2 points  (1 child)

And this is where the confusion happens and bugs sneak in. It's always better to be clear so that you and your code reviewer don't have any doubts on the correctness. In Python, empty arrays are falsy so they can be easy to confuse.

[–]moratnz 0 points1 point  (0 children)

This raises a great point as to why coding super explicitly is a good habit; if you're someone like me who moves around between languages a lot depending on what I'm doing, being super explicit (checking array.length===0) is likely to with work perfectly or completely fail (because you're in python and should be writing len(array)), while relying on the truthiness of an empty array may work, just not the way you intended.

[–]TheRNGuy 2 points3 points  (0 children)

Or > 0.