r/node • • Nov 10 '20

Conditionally call a function with optional chaining.

Post image
345 Upvotes

126 comments sorted by

View all comments

58

u/dudeitsmason Nov 10 '20

Just because you can, doesn't mean you should

0

u/[deleted] Nov 10 '20

[deleted]

14

u/dudeitsmason Nov 10 '20

I agree, optional chaining rocks, but there's something unsettling about using it between a function variable and it's invocation I guess?

1

u/KyleG Nov 10 '20

learn about functional optics and start using those instead! Just say no to null.

0

u/Not_Sugden Nov 10 '20 edited Nov 10 '20

it really depends on the use case for me, but i rarely run into situations where an the chaining would look more logical than chaining x && x.y

like uh

const bool = x && x.y ? x.y : false;

const bool = x && x.y ? x.y === 'smth' : false;

rather than

const bool = x?.y || false;

const bool = x?.y === 'smth'

the 2nd example here is a better one, what use is it comparing undefined to a string, using a tenary w/ && would be more logical there no?

edit: this has no relevance but i just wanted to say

you could also do

const bool = Boolean(x) && (x.y === 'smth');

5

u/robotslacker Nov 10 '20

The optional chaining way looks much more readable.

7

u/nickbreaton Nov 10 '20

In think case, why not do this if you’re in an environment that supports it? If this was in the language all along, I imagine it would be the established way for doing this.

4

u/conventionalWisdumb Nov 10 '20

Agreed. Ruby has a similar operator and the issues others have raised here never come up.

14

u/wobsoriano Nov 10 '20

Of course 👍 This is just another way of doing it.

-20

u/OmgImAlexis Nov 10 '20

This is a worse way of doing it.

11

u/threeys Nov 10 '20

Why is it worse?

1

u/FerLuisxd Nov 10 '20

Is not worse

-6

u/OmgImAlexis Nov 10 '20 edited Nov 10 '20

It’s harder to read the intent and doesn’t add anything helpful for the few chars it saves.

4

u/burnblue Nov 10 '20

Is it still a few characters when the function is deep?.inside?.an?.object?.()

2

u/ikeif Nov 10 '20

This is when I pulled it into a project.

Large JSON objects, so it was either constantly making shorthand assignments or being able to concisely do it this way.

People get so hung up on an option in a toolset. It’s not for every project, but some projects, it’s a good tool.

6

u/threeys Nov 10 '20

I think one could make the case it adds value by reducing the need to repeat the same name twice, thus reducing the potential for errors.

I'm not sure it's worth it given how nontraditional it is, and I think JS devs typically do not think of functions as objects as it is being used here, but still it's an interesting approach

-4

u/OmgImAlexis Nov 10 '20

What errors? If you’re using a good editor like VS code you shouldn’t have any issues guessing here.

if(typeof myFunction === ‘function’)

This reads better than what the OP shows. Without needing to know anything else I can read that it’s checking it’s a function. The ?. doesn’t.

The only time I can see it being a benefit is when you’re using a ternary operator to set something inline.

5

u/threeys Nov 10 '20

this example is a bit contrived:

const funcA = () => true;
const funcB = () => false;
const value = funcA && funcA();

I could accidentally change it to

const value = funcA && funcB();

and intellisense would be none the wiser.

This specific scenario isn't all that convincing, but IMO in general it's safer to be more expressive with less repetition than not, because it reduces the opportunity for subtle errors in the future.

6

u/recycled_ideas Nov 10 '20

The Elvis operator on a function says "if this exists, call it", just like when you use it to get a property of an object that might be null or undefined.

Just because the operator is unfamiliar to you, doesn't mean it's bad. It just means that it's unfamiliar.

if(typeof myFunction === ‘function’)

Is significantly longer and says exactly the same thing.

Again, unfamiliar doesn't mean bad.

-7

u/OmgImAlexis Nov 10 '20

No. I’m not “unfamiliar” with it. It’s been in typescript for quite some time now. I actively disallow it.

Just use an if and test the access properly.

8

u/recycled_ideas Nov 10 '20

Of course you're unfamiliar with it, because when you look at it, it's not clear.

That's on you, because the meaning is explicit.

→ More replies (0)

6

u/dertrommler06 Nov 10 '20

Why fight language improvements? Do you disallow spread because you’re used to Object.assign? Or mutate vars inside for loops instead of Array.map? I assure you if you use optional chaining you’ll find it reads far easier especially when accessing deeply nested properties on objects. 4 props or more deep is pretty hideous with &&

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