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

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 &&

1

u/RufusThreepwood Nov 10 '20

Optional chaining is not quite the same as the Elvis operator.

1

u/recycled_ideas Nov 10 '20

It checks if the left hand side is null or undefined and the runs the right hand side.

There's no difference between a function call and a property access.

→ More replies (0)

1

u/inabahare Nov 10 '20

Its not as nontraditional when you consider its literally just the elvis operator but applied to function calls. Like it doesnt really so anything surprising