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

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

问题描述

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

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

我能想到的一些方法:

  • 将其写在规范/文档中(有人会阅读吗?)
  • 使其成为方法名称的一部分(正如我所建议的 此处)
  • 假设每个抛出异常的方法不会返回null,并且每个不"抛出的方法可能返回null.

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

为什么没有这样的东西:

public notnull Object methodWhichCannotReturnNull(int i) throws Exception{返回空;//这会导致编译器错误!}

总结与结论

契约的表达方式有很多种:

  • 如果您的 IDE 支持它(如 IntelliJ),最好使用类似 @NotNull 的注释,因为它对程序员是可见的,并且可用于自动编译时检查.有一个 Eclipse 插件来添加对这些的支持,但它没有为我工作.
  • 如果这些都不是一个选项,请使用自定义类型,例如 OptionNotNull,这会增加清晰度并至少增加运行时检查.
  • 无论如何,在 JavaDoc 中记录合同永远不会受到伤害,有时甚至会有所帮助.
  • 使用方法名称来记录返回值的可空性不是由我以外的任何人提出的,虽然它可能非常冗长且并不总是有用,但我仍然相信有时它有其优点,

解决方案

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

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

您的示例可能看起来像这样,并被 IDE、编译器或其他代码分析工具拒绝.

@NotNull公共对象方法WhichCannotReturnNull(int i) 抛出异常{返回空;//这会导致编译器错误!}

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 .

I'm writing a method and I already know if I want to return null or throw an exception, what is the best way to express my decision, in other words, to document my contract?

Some ways I can think of:

  • 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.

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:

  • 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.

解决方案

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.)

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.

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天全站免登陆