返回null和具有零元素的数组之间的区别 [英] Difference between returning null and array with zero elements

查看:114
本文介绍了返回null和具有零元素的数组之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回ArrayList对象的属性。


在失败的情况下,返回null或返回一个ArrayList

对象是否正确元素?

解决方案

Sek写道:

我有一个返回ArrayList对象的属性。在失败的情况下,返回null或返回一个零元素的ArrayList
对象是正确的吗?




嗯,你在写吗?这个API的客户?如果是这样,那对你来说更有用的是什么?你真的想掩盖失败吗?

会有例外更合适吗?


Jon




" Jon Skeet [C#MVP]"写了...

Sek写道:


我有一个返回ArrayList对象的属性。

在失败的情况下,返回null或返回一个零元素的ArrayList
对象是正确的吗?



嗯,你在为客户编写客户端吗?这个API?如果是这样,那么对你更有用的是什么?你真的想掩盖失败 -
会不会更合适?




我会说它取决于什么是合同?该属性应该满足,但在大多数情况下,我认为零长度的ArrayList比
null更受欢迎。返回null在更多情况下会导致更多问题。


带上这个emplemple:


类人物

{

///< summary>

///兄弟姐妹列表。

///< / summary>

ArrayList兄弟姐妹

{

get

{

//说你读过这个人的兄弟姐妹<来自数据库的客户应该期望

//一个ArrayList,即使它是空的,也不是空的。

}

}

}


但Jon的问题非常有效; 失败是什么意思?如果它是严重的b $ b,那么最好的解决方案可能根本不是掩盖它,而是为了让它传播给调用者。


< br b> B B B B B B B B B B B B B B B B B B B B B B B B < SK *** @ pobox.com>写道:

Sek写道:

我有一个返回ArrayList对象的属性。

在失败情况下,是不是返回null或返回一个零元素的ArrayList
对象?



那么,你在为这个API编写客户端吗?如果是这样,那么对你更有用的是什么?你真的想掩盖失败 -
会不会更合适?

Jon




但有时它的并非ArrayList为空的例外。只有当ArrayList永远不允许为空时,我才会使用

异常。否则在尝试使用之前检查

它是否为零。我宁愿不检查

一个ArrayList为null。如果它是空的那么我可以添加它,如果我需要。


我做了很多数据清理和后端工作,我使用ArrayLists,List<>并且

Hashtable很多,当他们没有数据时我将它们返回给零

元素。如果他们的空是不好,那么我可以从调用者的
抛出异常,或者根本不执行使用它们的代码。


祝你好运项目,

Otis Mukinfus
http:// www.arltex.com
http://www.tomchilders.com


I have a property that returns a ArrayList object.

On failure condition, is it right to return null or return an ArrayList
object with zero elements?

解决方案

Sek wrote:

I have a property that returns a ArrayList object.

On failure condition, is it right to return null or return an ArrayList
object with zero elements?



Well, are you writing the clients for this API? If so, what would be
more useful to you? Do you actually want to mask the failure at all -
would an exception be more appropriate?

Jon



"Jon Skeet [C# MVP]" wrote...

Sek wrote:


I have a property that returns a ArrayList object.

On failure condition, is it right to return null or return an ArrayList
object with zero elements?



Well, are you writing the clients for this API? If so, what would be
more useful to you? Do you actually want to mask the failure at all -
would an exception be more appropriate?



I would say that it depends on what "contract" the property should meet, but
in most cases I think that a zero-length ArrayList would be preferred over a
null. Returning a "null" causes more problems in more cases.

Take this eaxmple:

class Person
{
///<summary>
///A list of siblings.
///</summary>
ArrayList Siblings
{
get
{
// Say that you read the siblings of the person
// from a database, the client should probably expect
// an ArrayList, even if it''s empty, not a null.
}
}
}

But Jon''s question is very valid; what do you mean by "failure"? If it''s
severe, the best solution would probably not be to mask it at all, but to
let it propagate to the caller.

// Bjorn A


On 11 Apr 2006 03:51:51 -0700, "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:

Sek wrote:

I have a property that returns a ArrayList object.

On failure condition, is it right to return null or return an ArrayList
object with zero elements?



Well, are you writing the clients for this API? If so, what would be
more useful to you? Do you actually want to mask the failure at all -
would an exception be more appropriate?

Jon



But sometimes its not an exception that an ArrayList is empty. I would use an
exception only when an ArrayList is never allowed to be empty. Otherwise check
it for a count of zero before attempting to use it. I would prefer not to check
an ArrayList for null. If it''s empty then I can add to it if I need to.

I do a lot of data scrubbing and backend work and I use ArrayLists, List<> and
Hashtable a lot and when they don''t have data in them I return them with zero
elements. If their being empty is not good then I can throw an exception from
the caller or simply not execute the code that uses them.

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com


这篇关于返回null和具有零元素的数组之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆