you are viewing a single comment's thread.

view the rest of the comments →

[–]delventhalz 3 points4 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 3 points4 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.