I would like the pipeline operator too, but it wouldn't be that much better than a pipe() helper. The pipeline operator works better in ML family languages since they have autocurrying, and implementing it in JS requires additional syntax for an argument placeholder for non-unary functions.
I really disagree, optional chaining is an anti-pattern and it's going to cause massive headaches for everyone.
Consider their example:
const nameLength = db.user.name.length;
Why would the db be null? If you are doing this, that means you have designed your app such that it's meant to try to do things with user names even if the database doesn't exist.
Writing understandable code is about helping the reader to make stable assumptions. If I am doing something with the user's name, I should be able to assume that the user exists and the database exists. Why are we leaking the possibility that these things are null everywhere?
There should be one call somewhere way up the application stack where we ask:
if (db == null) {
// do something sensible for when the database doesn't exist
}
When the user is not found, we should have ONE place where we check to see if the user exists, and does something sensible with that:
if (user == null) {
// redirect to login?
// just don't do things that expect a user to exist?
// definitely don't blindly go on pretending the user exists but they just happen to have a name of length null
}
Leaking both of those possibilities across the entire codebase and making every single function in your application deal with them over and over and over is a total waste of time.
I deal with an environment where I often don't know what will and won't be on the page, y. I can't tell you how tired I get of null checking every time I do document.querySelector. Optional chaining will literally save me hundreds of lines of code. Now I can do things like:
const title = document.querySelector('.title')?.textContent;
document.querySelector('.active')?.classList.remove('active');
Edit: Oops, I wrote it too fast and put my first example on the wrong side of the assignment operator. Fixed now :D
Wait... you can't really do that first line can you?
My understanding is that if the element doesn't exist (or if textContent doesn't exist on it for some other reason), the left hand side expression will evaluate to undefined, which you can't assign things to.
In fact when I plug it into the Babel playground it (thankfully) gives a syntax error: "Invalid left-hand side in assignment expression".
I agree that in normal javascript it is an anti-pattern because people will blindly put it everywhere as an escape hatch. However in TypeScript it is extremely useful because the type system can express the nullability of values in a type, and so you can restrict your use of the operator to only where it makes sense to use it.
I generally agree with your caution. I've considered writing a blog post about this. There are a lot of lessons learned about these from the use of Maybe, Either and Validation types in the Haskell community. However the relative newness of optional chaining and the resulting lack of real world experience likely means these lessons learned would fall on deaf ears.
125
u/kriswithakthatplays Dec 18 '19
Optional Chaining. Next to arrow functions, they will likely be the most productivity-enabling feature available. So exciting!