Java 中的 Null 安全取消引用,如 ?.在 Groovy 中使用 Maybe monad
我正在研究从 Objective C 移植到 Java 的代码库。没有 nullchecks 的方法链接有多种用法
dog.collar().tag().name()
我一直在寻找类似于 安全解引用运算符?在 Groovy 中而不是进行 nullchecks
dog.collar?.tag?.name
这导致 也许 monad 具有 Nothing 而不是 Null 的概念。但是,当访问值时,我遇到的所有 Nothing 实现都会抛出异常,这仍然无法解决链接问题。我让 Nothing 返回模拟,其行为类似于 NullObject 模式。但它解决了链接问题。
Nothing的这种实现有什么问题吗?
public class Nothing<T> implements Maybe<T> {
private Class<T> klass;
public Nothing(Class<T> klass) {
this.klass = klass;
}
@Override
public T value() {
return mock(klass, Mockito.RETURNS_DEEP_STUBS);
}
}
据我所知,
- 在代码中使用模拟库感觉很奇怪
- 它不会在第一个 null 处停止。
- 我如何区分由于空引用或名称实际上为空而导致的空结果?在Groovy代码中如何区分?
I'm working on a codebase ported from Objective C to Java. There are several usages of method chaining without nullchecks
dog.collar().tag().name()
I was looking for something similar to safe-dereferencing operator ?. in Groovy instead of having nullchecks
dog.collar?.tag?.name
This led to Maybe monad to have the notion of Nothing instead of Null. But all the implementations of Nothing i came across throw exception when value is accessed which still doesn't solve the chaining problem. I made Nothing return a mock, which behaves like NullObject pattern. But it solves the chaining problem.
Is there anything wrong with this implementation of Nothing?
public class Nothing<T> implements Maybe<T> {
private Class<T> klass;
public Nothing(Class<T> klass) {
this.klass = klass;
}
@Override
public T value() {
return mock(klass, Mockito.RETURNS_DEEP_STUBS);
}
}
As far as I can see
- It feels odd to use mocking library in code
- It doesn't stop at the first null.
- How do i distinguish between null result because of null reference or name actually being null? How is it distinguished in Groovy code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我真的不建议只检查空值。真的,我建议甚至不要返回 null,而是抛出异常。
无论哪种情况,您都不会通过执行此空对象方法来使代码更快,而且我认为您最终只会因为想要复制不同编程语言的功能而使某人感到困惑。我认为你应该适应你正在使用的语言。
I wouldn't really recommend just checking for the nulls. Really, I recommend not even ever returning null, but instead throwing exceptions.
In either case, you are not going to make the code any faster by doing this null object method, and I think you'll just end up confusing someone just because you want to replicate the feature of a different programming language. I think you should just adapt to the language you're using.
我也喜欢这个运算符,但它在 Java 上不可用——而且它是针对草率代码的权宜之计:如果dog.collar没有标签,它可能应该有不同的分支,如果标签没有没有名字——这可能是一个错误,应该抛出异常。
当您继承不是您编写的代码时,它并不能解决任何说“编写更好的代码”的问题。
我建议首先重构你的代码——如果这样的代码到处都在发生,也许无论如何它都需要转移到一个集中的函数中。如果dog.getName()委托dog.collar()来获取它的标签,最后获取标签的名称,那么你只需在一处修复这个错误(狗必须有一个名字,即使它不在项圈上,因此,dog.getName 应该能够遍历任何路径来解决您的问题 - 现在,
每当您看到自己在两个位置修补相同的代码时,请再次执行此操作(如果仅此而已)。每组属性都会发生一次(如果它已经完全分解并且面向对象),那么它不会是一个严重的问题,只是几个补丁而已,
哎呀,这样的代码是否违反了一些熟悉的规则?
I love this operator as well, but it's not available on Java--plus it's a stop-gap patch for sloppy code: If a dog.collar doesn't have a tag, it should probably branch differently, and if a tag doesn't have a name--it's probably an error and should throw an exception.
Not that it solves anything to say "code better" when you are inheriting code you didn't write.
I suggest that first you refactor your code--if code like that is happening all over the place, perhaps it needs to be moved into a centralized function anyway. If dog.getName() delegates to dog.collar() to get it's tag and finally the tag's name, then you only have to fix this bug in one place (The dog has to have a name even if it's not on the collar, so dog.getName should be able to traverse any path to solve your problem--now do it again for the NEXT spot of crappy code.
Whenever you see yourself patching the same code in 2 places, there is a factoring issue. If this only happens once for each group of attributes (if it's already fully factored and OO) then it can't be a serious problem, just a couple patches.
Heck, doesn't code like this even violate a few rules of familiarity?
您可以使用闭包来显式地实现它,就像 monad 一样。
我在遇到你的问题的同时也遇到了这个问题。
http://groovyconsole.appspot.com/script/205001
?.安全解引用显然在 groovy 中对于 null 检查在语法上更清晰,您可以用 ?: 结束链以返回 Nothing 值而不是 null,例如在此上下文中的“”。
如果你想理解 monad,我建议学习 Haskell,然后你自然会开始了解这些概念如何在其他语言中使用。允许可变性的语言,即使是功能性的(例如 Scala)也经常展示“不纯粹”的解决方案,而不是纯粹的功能性解决方案。
You could use closures to explicitly implement this as maybe monad.
I ran across this the same time as I ran across your question.
http://groovyconsole.appspot.com/script/205001
The ?. safe dereferencing is obviously syntatically cleaner in groovy for null checking, you can end the chain with ?: to return a Nothing value instead of null, such as "" in this context.
If you want to understand monads I would recommend learning Haskell, and then you naturally start to see how these concepts can and are used in other languages. Languages that allow mutability, even if functionalish (such as Scala) often showcase "impure" solutions rather than purely functional ones.
Java 中没有“安全取消引用”运算符。
我认为这是一件好事,因为“安全解除引用”通常是一个错误的伪装。基本上有两种可能性:
就我个人而言,我通常将长方法链视为将代码重构为更小、命名清晰的方法的提示(如果需要,可以在内部包装适当的空处理)。在大多数情况下,这可能比引入 monad 更明智。
There is no "safe-dereferencing" operator in Java.
I think this is a good thing, as "safe-dereferencing" is often a bug is disguise. You have basically two possibilities:
Personally, I usually see long method chains as a hint to refactor your code into smaller, clearly named methods (which can wrap up proper null handling inside if needed). This is probably more sensible than introducing monads in most circumstances.