r/node • • Nov 10 '20

Conditionally call a function with optional chaining.

Post image
345 Upvotes

126 comments sorted by

View all comments

Show parent comments

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.

-5

u/OmgImAlexis Nov 10 '20

No.

1

u/recycled_ideas Nov 10 '20

Optional chaining does exactly the same thing as the if, but in significantly fewer characters.

The only reason for it to be less clear is because you're unfamiliar with it. Because, again, the functionality is identical.

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.

1

u/RufusThreepwood Nov 10 '20

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

That's not an apt description of either. I'm not debating function call vs property access. The Elvis operator (?:) and optional chaining are different things.

1

u/recycled_ideas Nov 10 '20

First off, my apologies, I'd learned ?. as the Elvis operator, I come from a C# background where ?: doesn't exist.

That said, my description of optional chaining is accurate.

The reason optional chaining works on functions in JavaScript (it doesn't in most languages) is because functions in JavaScript are objects.

From the runtimes point of view accessing a property and executing involve evaluating the right hand side in terms of the left.

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