coffeescript icon indicating copy to clipboard operation
coffeescript copied to clipboard

'in' operator doesn't guard against non arrays

Open avalanche1 opened this issue 10 years ago • 9 comments

123 in undefined throws Array.prototype.indexOf called on null or undefined whereas it should return undefined

avalanche1 avatar Apr 05 '16 16:04 avalanche1

No, it shouldn't. in operator in coffeescript is equal to indexOf in javascript. And in javascript you will get same error. I think you can close it.

ghost avatar Apr 15 '16 18:04 ghost

Agreed. We don't do "defensive" programming in any construct that I'm aware of.

vendethiel avatar Apr 15 '16 18:04 vendethiel

Any value in a defensive version of in? Something like 123 in? undefined.

fortybillion avatar Apr 19 '16 15:04 fortybillion

Something like 123 in? undefined

Yeah, that would be nice

avalanche1 avatar Apr 21 '16 11:04 avalanche1

Yeah, that would be nice

As i know ? operator works only with variables. And you can get same result by 123 in ar if ar?.

ghost avatar Apr 21 '16 11:04 ghost

And you can get same result by 123 in ar if ar?.

that wouldn't check for an Array.

vendethiel avatar Apr 21 '16 11:04 vendethiel

We don't do "defensive" programming

That's a real shame. Why not?

avalanche1 avatar Apr 21 '16 15:04 avalanche1

This is issue has kind of been filed a few times before - once by me: #3650

I still think it's frustrating when in as a keyword could instead invoke a helper that checks with .indexOf() for an Array or walks through all the key-value pairs for an Object. in should be more general - not Array-specific. I doubt it would even matter performance-wise.

blitmap avatar Apr 23 '16 04:04 blitmap

You can use this very simple pattern:

propertyName in (array ? [])

You should always know if a value you are working with is potentially null/undefined.

michaelficarra avatar Apr 24 '16 01:04 michaelficarra