简体   繁体   English

如果String在代码中传递,我应该使用null或空字符串吗? (Java最佳实践)

[英]Should I use null or empty string if the String is passed around in the code? (Java best practice)

I have a String which is optional in the API. 我有一个在API中是可选的String。 If I don't get that parameter I am unsure if I should use an empty String or a null value. 如果我没有得到该参数,我不确定是否应该使用空Stringnull值。 The value is being passed around in a lot of functions and being used in some of them. 该值在许多函数中传递,并在其中一些函数中使用。

I have been thinking about this and this is what I came up with. 我一直在考虑这个,这就是我提出的。

  • Using null could be better because it would throw NPE if a developer tries to use it without checking if it exists. 使用null可能会更好,因为如果开发人员尝试使用它而不检查它是否存在,它会抛出NPE。 "" would not do that so the developer may think he is using the string when it does not exist. ""不会这样做,所以开发人员可能会认为他在不存在时使用字符串。
  • Using "" could be better because if a developer wants to change something he/she won't need to check for null pointer. 使用""可能会更好,因为如果开发人员想要更改某些内容,他/她将不需要检查空指针。 This could be beneficial when the developer does not need to know if the value exist like changing the encoding of the string. 当开发人员不需要知道值是否存在就像改变字符串的编码一样时,这可能是有益的。

What is the best practice in such a situation? 在这种情况下,最佳做法是什么?

EDIT: I am looking for reasons why an approach is better than the other. 编辑:我正在寻找为什么一种方法比另一种更好的原因。 And Optional sounds interesting. Optional听起来很有趣。

Empty string definitely is better than null. 空字符串绝对优于null。 If you want to indicate that the setting is optional , and provide a different behavior, use Optional<String> . 如果要指示该设置是可选的 ,并提供不同的行为,请使用Optional<String>

In my opinion, if "" is not a valid value for that parameter, use "" . 在我看来,如果""不是该参数的有效值,请使用"" Otherwise use null . 否则使用null

Regarding your first point, you'll get an IndexOutOfRangeException if you try to substring an empty string or call charAt or anything like that. 关于你的第一点,如果你试图对一个空字符串进行子串或调用charAt或类似的东西,你将得到一个IndexOutOfRangeException So I don't think it's gonna encourage using it without checking for empty string. 所以我认为不鼓励使用它而不检查空字符串。 And there are lots of ways you get prevent an NPE. 有很多方法可以防止NPE。

An alternative is Optional<String> , but I think currently it's syntax looks too ugly. 另一种选择是Optional<String> ,但我认为目前它的语法看起来太难看了。 I would still use "" if it is not considered a valid value. 如果它不被认为是有效值,我仍然会使用""

If you're returning a null String in your API, it is entirely possible that the developer consuming it will forget to check that its value is null . 如果您在API中返回null String,那么使用它的开发人员完全有可能忘记检查其值是否为null As Joop has already mentioned in his answer, use Optional<String> . 正如Joop在他的回答中已经提到的那样,使用Optional<String>

This forces the consumer of the API to check for its existence (or at the very least, think about how to handle its absence), eg 这迫使API的消费者检查它的存在(或者至少,考虑如何处理它的缺席),例如

myAPI.ifPresent(/* do something with String */);
myAPI.map(String::toLowerCase).orElse("");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM