空检查错误消息为“is null”或“为空”
在 Java 代码中进行 null 检查时,如果您为 null 值抛出 IllegalArgumentExceptions,您使用哪种消息模板?
我们倾向于使用这样的东西
public User getUser(String username){
if (username == null){
throw new IllegalArgumentException("username is null");
}
// ...
}
:“is null”或“was null”哪个更好,为什么?
对我来说“为空”感觉更自然。
When doing null checks in Java code, and you throw IllegalArgumentExceptions for null values, what kind of message template do you use?
We tend to use something like this
public User getUser(String username){
if (username == null){
throw new IllegalArgumentException("username is null");
}
// ...
}
What is better : "is null" or "was null", and why?
For me "is null" feels more natural.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于
Exception
是由于前置条件检查失败而引发的,我认为您应该声明所违反的要求,而不是简单地陈述事实。也就是说,不要说“用户名为空”,而是说“用户名不应该为空”。
关于使用库进行前置条件检查
作为提示,您可以使用旨在促进前置条件检查的众多库之一。 Guava 中的许多代码都使用 <代码>com.google.common.base.Preconditions
结构,这里更直接相关的是它具有
/base/Preconditions.html#checkNotNull(T)" rel="nofollow noreferrer">
checkNotNull
,它允许您简单地 消息明确指出所违反的要求。陈述事实的替代方案更加尴尬:
此外,这也可能不太有用,因为客户可能已经意识到这一事实,并且异常并不能帮助他们弄清楚实际的需求。是。
On
IllegalArgumentException
与NullPointerException
虽然原始代码在
null
参数上抛出IllegalArgumentException
,但 Guava 的Preconditions.checkNotNull< /code> 会抛出
NullPointerException
。这符合 API 设定的指南:
此外,这里引用了《Effective Java 第二版:第 60 条:支持使用标准异常》:
Since the
Exception
is thrown due to a failed precondition check, I think rather than simply stating a fact, you should state the requirement that was violated.That is, instead of saying
"username is null"
, say"username should not be null"
.On using libraries for precondition checks
As a tip, you can use one of the many libraries designed to facilitate precondition checks. Many code in Guava uses
com.google.common.base.Preconditions
More directly relevant here is that it has
checkNotNull
, which allows you to simply write:Note how naturally the above code reads, with the detailed message explicitly stating the requirement that was violated.
The alternative of stating facts is more awkward:
Moreover, this is also potentially less useful, since the client may already be aware of the fact, and the exception doesn't help them figure out what the actual requirements are.
On
IllegalArgumentException
vsNullPointerException
While your original code throws
IllegalArgumentException
onnull
arguments, Guava'sPreconditions.checkNotNull
throwsNullPointerException
instead.This is in accordance with the guideline set by the API:
Additionally, here's a quote from Effective Java 2nd Edition: Item 60: Favor the use of standard exceptions:
为 null,因为参数仍然为 null。
但是,为什么不简单地抛出一个 NullPointerException 而没有消息呢?
is null, since the argument is still null..
However, why not simply throw a NullPointerException without a message?
我建议说,
因为这太致命了,程序员无论如何都必须看看它。在异常消息中引用有问题的代码片段是我能想象的最简洁的事情。
I would suggest saying
as this is so fatal that a programmer must look at it anyway. Referring the offending code snippet in the exception message is the concisest thing I can imagine.
我更愿意这样写:
一石二鸟。首先,它检测用户名是空字符串的情况,(为了论证)我假设这是一个错误。其次,如果参数为
null
,则尝试分派length
调用将给出NullPointerException
。根据记录,意外
null
引发的预期异常是NullPointerException
。如果您不使用它的主要原因是 NPE 通常没有消息,请按如下方式编码:为什么在这里使用 NPE?因为 NPE 几乎总是表明与其他类型的参数验证错误不同类型的问题;例如,尚未初始化的字段或数组单元格或未正确处理的“可选”值。
最后回答问题:
这是一个见仁见智的问题,但我会写
“is null”
。I would be inclined to write this:
This kills two birds with one stone. First, it detects the case where user name is an empty string, which (for the sake of argument) I'm assuming is an error. Second, if the parameter is
null
attempting to dispatch thelength
call will give aNullPointerException
.For the record, the expected exception to throw for an unexpected
null
isNullPointerException
. If your main reason for not using it is that NPE's typically don't have a message, code it like this:Why use NPE's here? Because NPE's almost always indicate a different kind of problem to a other kinds of argument validation error; e.g. a field or array cell that has not been initialized or an "optional" value that is not being handled properly.
Finally to the question:
This is a matter of opinion, but I would write
"is null"
.