-
Notifications
You must be signed in to change notification settings - Fork 75
Using the name "safe-navigation" operator instead of "optional-chaining" #68
Comments
Objects are not paths to navigate, and there’s nothing “safe” about it - you can still get exceptions if getters throw. I don’t think it’s an appropriate name. |
”Optional”, not ”conditional” (and no, it isn’t meant to resemble to any sort of hack). Among the various names of the feature in other languages, I picked the one that describes better the feature for me. Although we can take time to bikeshed the name, I don’t think it is worth, unless one name is markedly better than the other. |
I see your point about it not being safe, but it definitely is safe-er. Also, objects are typically arranged in hierarchies which can be seen as paths, just like web addresses. It's a valid analogy and I think that's why the creators of Groovy chose this name. Why is it currently called Optional-chaining? Is it 'optional' because the result may or may not produce a null? Conditional seems to be more appropriate in this context (which is probably why I made my mistake in the original posting). Optional implies agency, as in someone having an option to alter the result.
Shouldn't the default position be that of using the existing name of the operator? You wouldn't rename the '+' operator anything other than the 'addition' operator. |
I believe this is primarily a Swift-ism, but they call Option/Maybe types "Optionals" ( |
Optional chaining is not about safety, but about brevity. IOW, I don’t expect |
Yes, although I’ve (re)interpreted ”optional” as an adjective. If you want to avoid ”optional”, alternatives are ”nullish-conditional” or ”nullish-guarded”. But not ”safe”. |
Thanks for that information @rkirsling. I don't have a background in Swift and never came across the 'Optional' terminology which threw me off.
I think 'nullish-guarded' is really good. What about 'nullish-guarded-accessor'? This would be much more descriptive than even 'safe-navigation'. |
I fear that "X-guarded" more readily sounds like "guarded by X" than "guarded against X" (cf. "semaphore-guarded critical section"), but I agree that guarded accessor / guarded access operator sounds quite nice. I suppose the only concern there is whether Edit: FWIW, Dart calls this "conditional member access", so even if we don't like "conditional", that's a point for "access". |
I find ”access” inaccurate, given that the short-circuited part may contain method calls, as in: |
This doesn't seem to be a counterexample—there's only one If the concern were about
All sorts of things can be chained, so I absolutely don't think it's wrong, just a little vague. But then I suppose people will probably call it "question-dot" regardless of the official name. 😄 |
I think 'chaining' is good at describing the overall syntax when the operator is used multiple times, like Method chaining. However, method chaining itself doesn't name it's operator(s) with the word 'chaining'. It's a combination of method calls and member access / structure reference operators. That being said, I'd be ok with the word 'chaining' with the above considered. Here are the names which I think are good so far:
|
Personally Id pick speculative access operator. I agree with @claudepache: at this point it's pure bikeshedding. |
I vote for null-safe-accessor. Who cares if it's not 100% accurate. It's the actual use case. It's something 100% of people can understand. Remember JS is a language for regular people, not uber-technical programmers who get hung up on techie words. |
@vonwao As someone who has been programming with javascript for only ~2 years, I find null-safe-accessor to be confusing. Intuitively, I would expect it to only work on |
“Safe” is something I didn’t understood until I saw an explanation, and when I saw it, I was hoping it wasn’t the actual use case... I don’t know whether it’s because I have a different philosophy of programming than some people (that is, I try to avoid errors rather than recover from them), or because it was an intentional abuse of language I couldn’t get used of. The expected use case of optional chaining is to take different actions when a value is null (or undefined) and when it is not. This has nothing to do with safety, this is control flow like an For me, it is not “protect against the TypeError that would occur when trying to access a property of null” as I have heard, because I wouldn’t ever try to access a property of null: If I expect that the value is sometimes null, I test that condition before attempting to access the property. If I don’t expect the value to be null but it occurs nevertheless, I try to correct the root of that bug rather than its symptom.
Indeed. Über-technical programmers can understand the feature whatever it is named. But regular people are helped if the feature has not a misleading name: it’s not about making their code safer. |
FWIW, "nullish-aware" is another option which has precedent but hasn't been raised here. |
An incomplete name list:
|
Name change didn’t happen before stage 4. |
As far as I can tell, the inventor of the operator was Groovy, using the name "Safe-navigation operator". This name is more intuitive and concise to the purpose and result of the operation, as the user is attempting to safely obtain a member along an object path.
ConditionalOptional-chaining more closely resembles one of the hacks people used in Javascript to obtain the same effect:((object || {}).property1 || {}).property2 || null
where chaining could either refer to the chain of conditions which must be met, or the chain in the sense of the object path.Would it not be better to pay homage and respect the first creator of the operator?
The text was updated successfully, but these errors were encountered: