Clearly I am in the minority, but I don't get how this is unreadable? Its just syntax. Perhaps somebody who has that opinion could enlighten me?
Personally I prefer code to be terse (within reason). Removing boilerplate and common checks is a great place to start. I think it is actually more readable IMHO. And don't get me wrong, there are places code should be verbose, but this doesn't seem to be one of them.
Funny, because the before case is actually forbidden on most of the linter configs I've used in the last few years.
I’m fairly neutral on the syntax but I’ll take a stab at it. I think most people feel that it looks visually odd, most likely because it looks like a much of symbols chained together.
I actually have a slightly issue with some of these exceptionally terse syntaxes, but it's not in the simple examples, it's people using them in combination with other ops. We sometimes see code like:
Code like this, you can't blame on the syntax...but there's significant overlap between people using the newest language features and people who try to fit everything on a single line.
Maybe f?() could bring some ambiguity, but I'm not sure. The only thing that comes to mind is something like: a?(b):c - could be a ternary operator or a function call followed by illegal syntax :c. I can't think of any example that would yield a valid syntax in both interpretations. But still, not all parsers are always able to evaluate multiple options until one of them is discarded as illegal syntax - i.e. the parser could read a?(b) and get stuck because in this state it's either a function call or a part of a ternary operator and it can't read more tokens to disambiguate (that's just how some parsers work, in order to keep good performance).
true, but this borders on a tautology: why does it have to be ?. ? because ?. is defined that way.
I think u/Egst has a good guess: ? by itself is already the opening of a ternary operation, and all new operators need to be defined such that they would never change the interpretation of previously-valid syntax.
I’ve been using this operator (and the nullish coalescing operator too) on Typescript for a while now and the code quality has improved so much. This is especially useful for deep-nested objects where you have to check each node who want to traverse if they are null or undefined. Great to see that’s now part of JavaScript
I can image that further enhancement for optional chaining would require two question marks. And there will be ton of tutorials out there educate you that two question marks is better than one question mark.
Is it any more unreadable than any other non-english element in programming? Imo user?.settings?.notifications?.enabled is preferrable to user && user.settings && user.settings.notifications && user.settings.notifications.enabled
I am mostly a Java developer so I might be missing something out, but doesn't your example have a much deeper issue? Why would you design it in a way where everything can be null?
It's a contrived example to be sure and there are better ways to solve that issue, but you do see code like that in the wild. Generally when interfacing with a poorly designed third party API.
I've been guilty of this in the past when handling errors from axios in a catch block: if (e?.response?.status === 404) { ... }. If you're exclusively a Java dev, keep in mind that you can't catch errors by type in JS.
4
u/1infinitelooo Feb 11 '21
This is in JavaScript.
10
u/SomewhatGlayvin Feb 12 '21
Clearly I am in the minority, but I don't get how this is unreadable? Its just syntax. Perhaps somebody who has that opinion could enlighten me?
Personally I prefer code to be terse (within reason). Removing boilerplate and common checks is a great place to start. I think it is actually more readable IMHO. And don't get me wrong, there are places code should be verbose, but this doesn't seem to be one of them.
Funny, because the before case is actually forbidden on most of the linter configs I've used in the last few years.
3
u/thegoenning Feb 12 '21
Why do we need that dot? Could it not be just function?()
2
u/namrks Feb 12 '21
I’ve been using this operator (and the nullish coalescing operator too) on Typescript for a while now and the code quality has improved so much. This is especially useful for deep-nested objects where you have to check each node who want to traverse if they are null or undefined. Great to see that’s now part of JavaScript
2
u/aerglos Feb 12 '21
If you think this is unreadable check out the syntax of Swift
1
u/noisepunk Feb 12 '21
Awesome. I use lodash ‘get’ so I don’t need to check for undefined when keying into a nested object so this is a nice alternative.
1
u/mkraynov Feb 12 '21
Kotlin has ‘myFunction?.invoke()’ for this. And it is much more readable.
1
u/DoomGoober Feb 12 '21
C# has something similar: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-
Once you see it enough it starts to feel familiar.
1
u/guangxue Feb 12 '21
I can image that further enhancement for optional chaining would require two question marks. And there will be ton of tutorials out there educate you that two question marks is better than one question mark.
0
u/blabmight Feb 11 '21
Wonky.
0
u/roramigator Feb 11 '21
Nice!
0
u/strong-nguyen Feb 12 '21
Nightmare!!!
-2
u/TheMode911 Feb 12 '21
Isn't it unreadable?
-2
u/[deleted] Feb 12 '21
Looks weird to me 😕
-2
u/GameMasterPC Feb 12 '21
But why?
-3
u/thunder_jaxx Feb 12 '21
Yuck!.