r/node • • Nov 10 '20

Conditionally call a function with optional chaining.

Post image
345 Upvotes

126 comments sorted by

View all comments

84

u/anon774 Nov 10 '20

People are hating on this approach, but IMO the only reason people think this is bad or shows "unclear intent" is because it's new and they're unfamiliar with it.

It's extremely clear what this means, I don't know how you could get confused by this if you saw it in the code. Nobody has provided an example showing how this could be unclear.

8

u/[deleted] Nov 10 '20

Its weird what the use case is for this. I am having a hard time trying to think when i needed to know if a method exists. I might just be too used to working in typed languages now.

However, language features aren't usually inherently bad. Just don't use if you don't like it, duh.

17

u/[deleted] Nov 10 '20

[removed] — view removed comment

3

u/VolperCoding Nov 10 '20

Languages like C++ don't have optionals because they're low level-ish. Typescript is a superset of Javascript so if course it has to feature hull and undefined

6

u/notAnotherJSDev Nov 10 '20

Rust is also in that C++ class of languages and it also has optionals?

-6

u/VolperCoding Nov 10 '20

I never programmed in Rust so idk

5

u/notAnotherJSDev Nov 10 '20

I have, it does.

3

u/[deleted] Nov 10 '20

Scala and every other FP language does too. Optionals are just an abstraction on top of null pointers, you can reason about them and handle them in a more elegant way. There's nothing 'basic' about this. Java has them in the more recent versions too.

Rather than null-pointer checks everywhere, you have some really nice syntax to deal with it, and it's a lot more transparent in the code.

1

u/[deleted] Nov 10 '20

You probably (?) wouldn't model it as Maybe f though. Instead you'd just require a f and at the call site either provide a proper function or a noop.

1

u/[deleted] Nov 10 '20

Yeah, tbh I find the use case in the OP a bit odd, was more talking about options in a general sense.

1

u/Qildain Nov 10 '20

The billion dollar mistake... as a (primarily) JVM language dev, I abhor null checks. I'm glad they pulled it into vanilla.

1

u/johannes1234 Nov 10 '20

C++ ist lowlevel-ish. Template meta programming is more high-level than what is being done with JavaScript. It however targets to be fun at the "machine level".

Such features as mentioned here are in the pipeline for C++, C++ however is slow to take something in the core language and it's expected that a library level solution will come first. (And in fact there are already different library implementations allowing such chaining, bringing it in standard makes it interoperable)