C# is getting “?.”, sometimes called the Safe Navigation Operator
27 points by mendicant 11 years ago | 10 comments- mcv 11 years agoGreat news. The ?. operator in Groovy greatly reduces boilerplate code and improves readability and maintainability. No layers of null pointer checks that are all basically the same and getting in the way of what you actually want to do.
And in those cases where you still need specific behaviour based on which object is null, you can still use the . operator with traditional null pointer checks.
- mendicant 11 years agoThe question I have about this: Does making it easy to do parent?.child?.grandchild?.property help or hinder the software development process in the long term?
- _random_ 11 years agoThis will help getting rid of the Maybe monad pattern in our code (at least partially) - all patterns are patches for missing language features. I cannot see any drawbacks personally. Would much rather prefer the 'object! paramName' not-null enforcement operator though, but it is tricky to make it backwards-compatible.
- vorg 11 years agoThe ?. has only really worked in scripting, i.e. writing quick scripts to manipulate code written in a proper type-checked programming language. Groovy has it but Java doesn't. In Groovy's heyday if some code spewed out NullPointerException, the first thing people did was replace all the . with ?. and run it again, because the code was throwable quality anyway. I can't imagine anything good will come of putting ?. into C# (or Java) which is meant for building more lasting systems.
- gizmo686 11 years agoUsing ?. lets you do code like:
instead of putting a null check between every call. In many cases null is used to indicate that a computation failed. If you are doing a chain of computations, there is often not a reason you need or want to do a check between every computation, instead of having a single handler at the end for if any of them fail.var = foo()?.bar?.bing() if(foo==null){...}
This pattern comes up fairly often in Haskell, which accomplishes it with the Maybe monad.
- mendicant 11 years agoThere are certain languages and situations where the pattern does make sense. But as a heavy C# coder, I find that more often than not you're dealing with a class heavy codebase where it's more important that you _don'_ do foo()?.bar?.bing(). Specifically as things happen such as refactoring you often need to ask the question should something 'care' that a foo has a bar which has a bing. Perhaps if you want to know about a bing that is down the chain from Foo, perhaps Foo should provide you with the Bing somehow (which really helps with refactoring as codebases grow to years and potentially decades old).
It's not always so cut and dry, and there are situations where this is a very valuable tool. I just feel that down the road if you feel like there is a valid reason for refactoring object hierarchy that you'll potentially be doing yourself (or those that follow) a disservice in the long run.
- mendicant 11 years ago
- mcv 11 years agoGroovy is used for very serious websites, just like Java. And in Java, maintainability, and therefore readability, of code is just as important as in Groovy. I really like the ?. operator, and I miss it in Java. It makes code cleaner, more robust, and easier to check. Less boilerplate, more readability.
- gizmo686 11 years ago
- mendicant 11 years agoI can understand the reason for it being there, but I also tend to fall towards following the Law of Demeter. I tend to find that most (not all) of the use cases of this are a symptom of overly intimate knowledge between objects that really shouldn't know about one another.
- bpicolo 11 years agoIt currently exists in Coffeescript (and I'm sure other languages). Always been handy.
- _random_ 11 years ago