我应该从返回null的函数返回null或将“null object”模式应用于函数吗?

时间:2022-09-10 23:52:01

Let's say you have a function that returns a date:

假设您有一个返回日期的函数:

Date myFunc(paramA, paramB){
   //conditionally return a date?
}

Is it appropriate to return null from this function? This seems ugly because it forces clients to check for null.

从这个函数返回null是否合适?这看起来很难看,因为它迫使客户检查null。

The "null object" pattern is an implementation pattern that addresses this concern.
I'm not a huge fan of the null object pattern, but yes, it makes sense to always return a list, even if is empty, rather than to return null.
However, say in Java, a null date would be one that is cleared and has the year 1970.

“空对象”模式是解决此问题的实现模式。我不是null对象模式的忠实粉丝,但是,总是返回一个列表是有意义的,即使它是空的,而不是返回null。但是,在Java中,一个空日期将被清除并具有1970年。

What is the best implementation pattern here?

这里最好的实施模式是什么?

8 个解决方案

#1


7  

The null object pattern is not for what you are trying to do. That pattern is about creating an object with no functionality in it's implementation that you can pass to a given function that requires an object not being null. An example is NullProgressMonitor in Eclipse, which is an empty implementation of IProgressMonitor.

空对象模式不适用于您要执行的操作。该模式是关于创建一个在其实现中没有功能的对象,您可以将其传递给需要对象不为null的给定函数。一个例子是Eclipse中的NullProgressMonitor,它是IProgressMonitor的空实现。

If you return a "null" date, like 1970, your clients will still need to check if it's "null" by seeing if it's 1970. And if they don't, misbehaviour will happen. However, if you return null, their code will fail fast, and they'll know they should check for null. Also, 1970 could be a valid date.

如果你返回一个像“1970”这样的“空”日期,你的客户仍然需要通过查看是否为1970来检查它是否为“空”。如果它们没有,则会发生错误行为。但是,如果返回null,它们的代码将快速失败,并且它们将知道它们应该检查null。此外,1970年可能是一个有效的日期。

You should document that your method may return null, and that's it.

你应该记录你的方法可能返回null,就是这样。

#2


5  

null is quite acceptable. However if you want to return null on an error, consider throwing an exception instead.

null是完全可以接受的。但是,如果要在错误上返回null,请考虑抛出异常。

#3


2  

If it is possible a date won't be found then the null makes sense. Otherwise you end up returning some magical date (like the 1970 epoch) that will frustrate people hooking into the function far more than just getting a null returned.

Document that it could return null, however...

如果有可能找不到日期,那么null就有意义了。否则你最终会返回一些神奇的日期(比如1970年代),这会让人们更加难以接触到函数,而不仅仅是返回null。记录它可以返回null,但是......

#4


1  

I'm not a fan of the null object pattern.

我不是空对象模式的粉丝。

If null is a valid and intended return value, then return it. If it's caused by an error condition, an exception would make more sense.

如果null是有效且预期的返回值,则返回它。如果它是由错误条件引起的,则异常会更有意义。

Sometimes, the real problem is the method should be returning a more complex type that does represent more information. In those cases it's easy to fall into a trap and return some basic type, plus some special magic values to represent other states.

有时候,真正的问题是该方法应该返回一个更复杂的类型来代表更多的信息。在这些情况下,很容易陷入陷阱并返回一些基本类型,加上一些特殊的魔术值来表示其他状态。

#5


1  

It seems like the expected results from this method is a Date, or none found. The none found case is typically represented by returning null. Although some would use an exception to represent this case, I would not (as it is a expected result and I have never been a fan of processing by exception).

看起来这个方法的预期结果是Date,或者没有找到。找不到案例通常由返回null表示。虽然有些人会使用异​​常来表示这种情况,但我不会(因为它是预期的结果而且我从未成为异常处理的粉丝)。

The Null object pattern is not appropriate for this case, as has been stated. In fact, from my own experience, it is not appropriate for many cases. Of course, I have some bias due to some experience with it being badly misused ;-)

如上所述,Null对象模式不适用于这种情况。事实上,根据我自己的经验,它不适合许多情况。当然,我有一些偏见,因为它被严重误用了一些经验;-)

#6


0  

If its not a performance hit I like to have an explicit query method and then use exceptions:

如果它不是性能命中我喜欢有一个显式查询方法然后使用异常:

if(employee.hasCustomPayday()) {
    //throws a runtime exception if no payday
    Date d = emp.customPayday();
}

#7


0  

Use exceptions if this is not a scenario that should usually happen.

如果这不是通常应该发生的情况,请使用例外。

Otherwise, (if this is for example an end-date for an event), just return null.

否则,(如果这是例如事件的结束日期),则返回null。

Please avoid magic values in any case ;)

在任何情况下都要避免魔法值;)

#8


-1  

You could try using an output parameter

您可以尝试使用输出参数

boolean MyFunction( a,b,Date c)
{
  if (good) 
     c.SetDate(....);
  return good;

}

Then you can call it

然后你可以打电话给它

Date theDate = new Date();
if(MyFunction(a, b ,theDate ) 
{
   do stuff with C
}

It still requires you to check something, but there isn't a way of avoiding some checking in this scenario.

它仍然需要您检查一些内容,但是在这种情况下无法避免某些检查。

Although SetDate is deprecated, and the Calendar implementation is just ugly.

虽然不推荐使用SetDate,但Calendar实现只是丑陋。

Stupidest API change Sun ever did.

Sun发生过愚蠢的API改变。

#1


7  

The null object pattern is not for what you are trying to do. That pattern is about creating an object with no functionality in it's implementation that you can pass to a given function that requires an object not being null. An example is NullProgressMonitor in Eclipse, which is an empty implementation of IProgressMonitor.

空对象模式不适用于您要执行的操作。该模式是关于创建一个在其实现中没有功能的对象,您可以将其传递给需要对象不为null的给定函数。一个例子是Eclipse中的NullProgressMonitor,它是IProgressMonitor的空实现。

If you return a "null" date, like 1970, your clients will still need to check if it's "null" by seeing if it's 1970. And if they don't, misbehaviour will happen. However, if you return null, their code will fail fast, and they'll know they should check for null. Also, 1970 could be a valid date.

如果你返回一个像“1970”这样的“空”日期,你的客户仍然需要通过查看是否为1970来检查它是否为“空”。如果它们没有,则会发生错误行为。但是,如果返回null,它们的代码将快速失败,并且它们将知道它们应该检查null。此外,1970年可能是一个有效的日期。

You should document that your method may return null, and that's it.

你应该记录你的方法可能返回null,就是这样。

#2


5  

null is quite acceptable. However if you want to return null on an error, consider throwing an exception instead.

null是完全可以接受的。但是,如果要在错误上返回null,请考虑抛出异常。

#3


2  

If it is possible a date won't be found then the null makes sense. Otherwise you end up returning some magical date (like the 1970 epoch) that will frustrate people hooking into the function far more than just getting a null returned.

Document that it could return null, however...

如果有可能找不到日期,那么null就有意义了。否则你最终会返回一些神奇的日期(比如1970年代),这会让人们更加难以接触到函数,而不仅仅是返回null。记录它可以返回null,但是......

#4


1  

I'm not a fan of the null object pattern.

我不是空对象模式的粉丝。

If null is a valid and intended return value, then return it. If it's caused by an error condition, an exception would make more sense.

如果null是有效且预期的返回值,则返回它。如果它是由错误条件引起的,则异常会更有意义。

Sometimes, the real problem is the method should be returning a more complex type that does represent more information. In those cases it's easy to fall into a trap and return some basic type, plus some special magic values to represent other states.

有时候,真正的问题是该方法应该返回一个更复杂的类型来代表更多的信息。在这些情况下,很容易陷入陷阱并返回一些基本类型,加上一些特殊的魔术值来表示其他状态。

#5


1  

It seems like the expected results from this method is a Date, or none found. The none found case is typically represented by returning null. Although some would use an exception to represent this case, I would not (as it is a expected result and I have never been a fan of processing by exception).

看起来这个方法的预期结果是Date,或者没有找到。找不到案例通常由返回null表示。虽然有些人会使用异​​常来表示这种情况,但我不会(因为它是预期的结果而且我从未成为异常处理的粉丝)。

The Null object pattern is not appropriate for this case, as has been stated. In fact, from my own experience, it is not appropriate for many cases. Of course, I have some bias due to some experience with it being badly misused ;-)

如上所述,Null对象模式不适用于这种情况。事实上,根据我自己的经验,它不适合许多情况。当然,我有一些偏见,因为它被严重误用了一些经验;-)

#6


0  

If its not a performance hit I like to have an explicit query method and then use exceptions:

如果它不是性能命中我喜欢有一个显式查询方法然后使用异常:

if(employee.hasCustomPayday()) {
    //throws a runtime exception if no payday
    Date d = emp.customPayday();
}

#7


0  

Use exceptions if this is not a scenario that should usually happen.

如果这不是通常应该发生的情况,请使用例外。

Otherwise, (if this is for example an end-date for an event), just return null.

否则,(如果这是例如事件的结束日期),则返回null。

Please avoid magic values in any case ;)

在任何情况下都要避免魔法值;)

#8


-1  

You could try using an output parameter

您可以尝试使用输出参数

boolean MyFunction( a,b,Date c)
{
  if (good) 
     c.SetDate(....);
  return good;

}

Then you can call it

然后你可以打电话给它

Date theDate = new Date();
if(MyFunction(a, b ,theDate ) 
{
   do stuff with C
}

It still requires you to check something, but there isn't a way of avoiding some checking in this scenario.

它仍然需要您检查一些内容,但是在这种情况下无法避免某些检查。

Although SetDate is deprecated, and the Calendar implementation is just ugly.

虽然不推荐使用SetDate,但Calendar实现只是丑陋。

Stupidest API change Sun ever did.

Sun发生过愚蠢的API改变。