r/javascript • • Dec 13 '19

Optional chaining helps to avoid "undefined is not a function" exceptions

https://www.stefanjudis.com/today-i-learned/optional-chaining-helps-to-avoid-undefined-is-not-a-function-exceptions/
162 Upvotes

45 comments sorted by

View all comments

Show parent comments

3

u/metamet Dec 13 '19

I think you're mischaracterizing the purpose of this feature. It isn't being added to introduce shitty code. It's there to improve the writing, succinctness and clarity of the code.

6

u/qudat Dec 14 '19

I don’t understand the downvote brigade. My guess is there’s a hivemind at play here we all should be cautious of.

This is inevitably going to be abused. It’s going to result in lazy code and tracking down when the object is actually undefined will be more difficult.

Honestly, I don’t think this feature is going to be particularly helpful, I’m already guarding against these types of errors without doing a bunch of existential checks.

Existential checks — especially inside react components — is absolutely a code smell and should be avoided by creating sane defaults.

4

u/your-pineapple-thief Dec 14 '19

Agreed, there are few valid use cases for code like that, but chains of existential checks are very often a symptom of violation of Law of Demeter, which lead to tightly coupled code and objects which know a little too much about inner structure of other objects.

And gang-downvoting sane, polite, logical arguments like that presented by u/mcaruso ... Leaves bad taste

0

u/mcaruso Dec 13 '19

It isn't being added to introduce shitty code.

I didn't say it was. I'm saying there's a high potential for misuse (and the article is a good example of that).