r/javascript • • Feb 20 '18

Optional Chaining may be coming to JavaScript

https://dev.to/sammyisa/optional-chaining-may-be-coming-to-javascript--4ff0
321 Upvotes

100 comments sorted by

View all comments

19

u/phpdevster Feb 20 '18 edited Feb 21 '18

I don't mind this coming to JS since different tools for different jobs and all that, but it's been my experience that if you have to do this extensively, you have some design problems with your code whereby objects may or may not be in a reliably complete state at a given time. It means you either have the wrong abstractions, the wrong data models, are grotesquely violating the Law of Demeter, or are assembling data models at the wrong points in your codebase.

Your goal should be to design your domain and application such that you are always working with proper, well formed objects that do in fact have the data and properties you expect them to have. If you only need partial data from a model and don't want to have to populate the whole thing, then your data model is too tightly coupled to too many responsibilities and you should break it apart. If a function is asking such a deep chain of data, then stop passing in the root and pass in only the leaf that you need. If you are doing logic based on the existence or non-existence of some data in an object, stop and more carefully think about the responsibilities of the functions you're passing the potentially incomplete data into.

As convenient as foo?.bar?.baz?.() is, it's really hiding a code smell. At least before when you did all that explicit null checking, you had to stop and think if that was really the right design for the code, and that maybe something was amiss.

8

u/1-800-BICYCLE Feb 20 '18

I totally agree and was hoping to see more productive discussion about this.

On one hand, JS is a scripting language at heart, so it makes sense to have first-class support for dealing with maybe-null values. Furthermore, any JS dev has had to deal with deep nullable path traversal at some point.

On the other hand, I've code reviewed way too many cases where 1) design-by-contract or early checks (at parse time, for example) should be used instead (e.g. your consumers make the assumption that the value being passed to them exists and your producers make damned well sure that they do).

There's also another problem that tends to crop up: hyper-defensive coding that adds unnecessary noise and makes debugging and refactoring harder. For example, defining a nested object literal on line 1 and then using the ?. operator to access it on line 2. If for some reason that object you defined on line 1 doesn't exist (misspelling, comic ray flipping bits, etc) then you better damned well find out about it sooner rather than later.

3

u/mrarnoldpalmer Feb 20 '18

Definitely agree with this. After writing a large project that heavily uses propOr and pathOr from ramda we have unknowingly hidden a lot of issues with the data contract. We used it as a crutch instead of doing proper up front data normalization and now we are kicking ourselves and refactoring a big part of the app to help fix these issues. I'm in favor of this feature, its just easy to abuse.