如何显示方法是否可以返回null [英] How to show if a method may return null

查看:166
本文介绍了如何显示方法是否可以返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发布这个问题和阅读那个我意识到知道一个方法是否应该返回null是非常重要的,或者如果这被认为是错误条件并且应该抛出异常。还有一个很好的讨论,何时返回'null'或抛出异常

After posting this question and reading that one I realized that it is very important to know if a method is supposed to return null, or if this is considered an error condition and an exceptions should be thrown. There also is a nice discussion when to return ‘null’ or throw exception .

我正在编写一个方法,我已经知道是否要返回null或抛出异常,表达我的决定的最佳方式是什么,换句话说,记录我的合同?

我能想到的一些方法:


  • 在规格/文档中写下来(有人会阅读吗?)

  • 将它作为方法名称的一部分(正如我建议的那样这里

  • 假设抛出异常的每个方法都返回null,而每个不抛出的方法可能返回null。 / li>
  • Write it down in the specs / the documentation (will anyone read it?)
  • Make it part of the method name (as I suggested here)
  • assume that every method that throws an exception will not return null, and every one that does 'not' throw might return null.

我主要谈论java,但它可能适用于其他la nguages:为什么有一种正式的方式来表达是否会抛出异常(抛出关键字)但是没有正式的方式来表达是否可能返回null?

I'm mainly talking about java, but it might apply to other languages, too: Why is there a formal way to express if exceptions will be thrown (the throws keywords) but no formal way to express if null might be returned?

为什么没有这样的东西:

Why isn't there something like that:

public notnull Object methodWhichCannotReturnNull(int i) throws Exception
{
    return null; // this would lead to a compiler error!
}



摘要和结论



有很多方法来表达合同:

Summary and Conclusion

There are many ways to express the contract:


  • 如果您的IDE支持它(作为IntelliJ),最好使用类似的注释 @NotNull 因为它对程序员可见,可用于自动编译时检查。有一个 Eclipse插件来添加对这些的支持,但它没有为我工作。

  • 如果这些不是一个选项,请使用自定义类型,如选项< T> NotNull< T> ,这增加了清晰度,至少增加了运行时检查。

  • 无论如何,在JavaDoc中记录合同永远不会伤害,有时甚至会有帮助。

  • 使用方法名称来记录返回值的 nullability 除了我之外没有人提出过,虽然它可能非常详细而且并不总是有用,但我仍然相信它有时也有它的优点。

  • If your IDE supports it (as IntelliJ), it's best to use an annotation like @NotNull because it is visible to the programmer and can be used for automated compile time checking. There's a plugin for Eclipse to add support for these, but it didn't work for me.
  • If these are not an option, use custom Types like Option<T> or NotNull<T>, which add clarity and at least runtime checking.
  • In any way, documenting the contract in the JavaDoc never hurts and sometimes even helps.
  • Using method names to document the nullability of the return value was not proposed by anyone but me, and though it might be very verbose und not always useful, I still believe sometimes it has its advantages, too.

推荐答案

一个非常好的跟进问题。我认为 null 是一个真正特殊的值,如果一个方法可能返回 null 它必须在Javadoc中清楚地记录它的时候是( @return some value ...,如果...... ,则为null)。在编码时我是防御性的,并假设一个方法可能会返回 null ,除非我确信它不能(例如,因为Javadoc这样说。)

A very good follow up question. I consider null a truly special value, and if a method may return null it must clearly document in the Javadoc when it does (@return some value ..., or null if ...). When coding I'm defensive, and assume a method may return null unless I'm convinced it can't (e.g., because the Javadoc said so.)

人们意识到这是一个问题,建议的解决方案是使用注释以一种可以自动检查的方式陈述意图。请参阅 JSR 305:软件缺陷检测注释 JSR 308:Java类型的注释 JetBrain的Nullable方法

People realized that this is an issue, and a proposed solution is to use annotations to state the intention in a way it can be checked automatically. See JSR 305: Annotations for Software Defect Detection, JSR 308: Annotations on Java Types and JetBrain's Nullable How-To.

您的示例可能如下所示,并被IDE拒绝,编译器或其他代码分析工具。

Your example might look like this, and refused by the IDE, the compiler or other code analysis tools.

@NotNull
public Object methodWhichCannotReturnNull(int i) throws Exception
{
    return null; // this would lead to a compiler error!
}

这篇关于如何显示方法是否可以返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆