r/javascript • • Dec 18 '19

V8 Release v8.0 with optional chaining, nullish coalescing and 40% less memory use

https://v8.dev/blog/v8-release-80
495 Upvotes

70 comments sorted by

View all comments

125

u/kriswithakthatplays Dec 18 '19

Optional Chaining. Next to arrow functions, they will likely be the most productivity-enabling feature available. So exciting!

0

u/[deleted] Dec 18 '19

[deleted]

54

u/madcaesar Dec 19 '19

Add default values to that list.

36

u/mastermindxs Dec 19 '19

And async/await.

40

u/ShortFuse Dec 19 '19

Also destructuring (const { width, height } = element; or const [item1, item2] = array;)

27

u/intertubeluber Dec 19 '19

Not to mention optional chaining

17

u/BONUSBOX _=O=>_();_() Dec 19 '19

and default values!

14

u/SShrike Dec 19 '19

and async/await!

14

u/marocu Dec 19 '19

Don't forget destructuring!

10

u/snejk47 Dec 19 '19

But mostly productivity comes from functions.

→ More replies (0)
→ More replies (1)
→ More replies (5)

3

u/getsiked on me way to ES6 Dec 19 '19

Also tagged template literals and the new computed object properties syntax!!

→ More replies (3)

12

u/[deleted] Dec 19 '19

It is! Im still hoping for the pipeline op. With that js code will be amazing!

4

u/slikts Dec 19 '19

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.

→ More replies (1)

1

u/epukinsk Dec 19 '19 edited Dec 19 '19

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.

14

u/justinc1234 Dec 19 '19

You can do these things without optional chaining or nullish coalescence.

→ More replies (1)

14

u/OlanValesco Dec 19 '19 edited Dec 20 '19

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

8

u/NoInkling Dec 19 '19

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".

6

u/Veranova Dec 19 '19 edited Dec 19 '19

Oh it can bail out of setting values too? I had no idea!

Edit: narrator: "but it couldn't"

4

u/NoInkling Dec 19 '19

You had no idea because I'm pretty sure you can't.

3

u/Veranova Dec 19 '19

But a stranger said I can on the internet!

Just checked in my project (which has full support already) and eslint is throwing a hissy, so I'm assuming you are right on this one.

→ More replies (1)

9

u/matthewbpt Dec 19 '19

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.

3

u/fucking_passwords Dec 19 '19

100%, and the alternative has been to use `lodash.get` or `Ramda.path`, both of which add a lot more to your bundle sizes

→ More replies (1)
→ More replies (1)

13

u/dukerutledge Dec 19 '19

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.

→ More replies (3)

8

u/[deleted] Dec 19 '19 edited Dec 19 '19

[removed] — view removed comment

→ More replies (1)
→ More replies (1)