简体   繁体   English

为空对象提供方法返回值是否有用?

[英]Would it be useful to have method return values for null objects?

Would it be useful to be able to provide method return value for null objects? 能够为空对象提供方法返回值是否有用?

For a List the null return values might be: 对于列表,空返回值可能是:

get(int) : null
size() : 0
iterator() : empty iterator

That would allow the following code that has less null checks. 这将允许下面的代码具有较少的空检查。

List items = null;

if(something) {
    items = ...
}

for(int index = 0; index < items.size(); index++) {
    Object obj = items.get(index);
}

This would only be used if the class or interface defined it and a null check would still work. 仅当类或接口定义了它并且空检查仍然有效时,才使用此方法。 Sometimes you don't want to do null checks so it seems like it could be beneficial to have this as an option. 有时您不想进行空检查,因此将其作为选项似乎很有益。

From: http://jamesjava.blogspot.com/2007/05/method-return-values-for-null-objects.html 来自: http : //jamesjava.blogspot.com/2007/05/method-return-values-for-null-objects.html

It is nice to not have to check for NULL, and some languages make it easier -- eg C#'s non-NULLable types, or Haskell which doesn't have NULLs but can express a missing value with the Maybe type constructor. 不必检查NULL很好,并且某些语言使它更容易使用-例如C#的非NULLable类型,或不具有NULL但可以使用Maybe类型构造函数表达缺失值的Haskell。

A NULL is distinct from an empty list. 空值不同于空列表。 You can take the point-of-view that someone passing in a NULL where you need a list is making a programming error, and that the right thing to do is throw a NullPointerException. 您可以得出这样的观点:有人在需要列表的NULL中传递消息会导致编程错误,而正确的做法是抛出NullPointerException。

The typical excuse for accepting NULLs is that often there's a case where you don't need the list, and you shouldn't have to create a new List that's empty, especially when there's some concern about efficiency. 接受NULL的典型借口是,在某些情况下,您不需要列表,也不必创建一个空的新列表,特别是当您担心效率问题时。 You can have many of the benefits without changing the language, but by instead having a static EmptyList that people can pass in, that never needs to be reinitialized. 您可以在不更改语言的情况下获得很多好处,但是可以通过一个静态的EmptyList来让人们传入,而无需重新初始化。

It's a pattern called Null Object 这是一个称为空对象的模式

http://en.wikipedia.org/wiki/Null_Object_pattern http://en.wikipedia.org/wiki/Null_Object_pattern

This is a good idea. 这是一个好主意。

Smalltalk does this. Smalltalk做到了。

There is a NULL object. 有一个NULL对象。 It doesn't descend from Object. 不是来自对象的。 (Smalltalk is a singly-rooted class hierarchy like Java) (Smalltalk是像Java这样的单根类层次结构)

For the advanced student, you can sub-class it for making proxies! 对于高级学生,您可以将其细分为代理!

Ruby does this (as well as others). Ruby可以做到这一点(以及其他)。 It has nil instead of null and it's an object. 它具有nil而不是null,并且是一个对象。

I dp hate when functions that are meant to return lists can return null. 当要返回列表的函数可以返回null时,我dp讨厌。 It's better to return an empty list and let the user decide if they want to check for null (empty) or not. 最好返回一个空列表,让用户决定是否要检查null(空)。

In C# (among other languages), this is normally not allowed. 在C#(以及其他语言)中,通常不允许这样做。 Without an instance of Foo, .net doesn't know to call Foo's method or Foo's child's method. 没有Foo的实例,.net不知道调用Foo的方法还是Foo的子方法。

However, an C# 3.0 extension method applied to the Foo type would allow this: 但是,将C#3.0扩展方法应用于Foo类型将允许以下操作:

Foo x = null;
if (x.Bar() == 0)
{
  Console.WriteLine("I win");
}

Bar could be constructed like so: 酒吧可以这样构造:

public static int Bar (this Foo theFoo)
{
  return theFoo == null ? 0 : 1;
}

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

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