返回MatchCollection或什么都没有的方法? [英] method that returns either MatchCollection or nothing?

查看:92
本文介绍了返回MatchCollection或什么都没有的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建一个返回匹配集合的方法是否安全?或者

最好只返回一个匹配集合并让代码在

方法之外验证匹配集合是否为空?

Is it safe to make a method that returns a match collection or nothing? or
is it better to just return a match collection and have the code outside the
method validate that match collection is empty or not?

推荐答案

2008-10-15,Andy B< a _ ***** @ sbcglobal.netwrote:
On 2008-10-15, Andy B <a_*****@sbcglobal.netwrote:

制作一个返回匹配集合的方法是否安全?或者

最好只返回一个匹配集合并让代码超出

方法验证匹配集合是否为空?
Is it safe to make a method that returns a match collection or nothing? or
is it better to just return a match collection and have the code outside the
method validate that match collection is empty or not?



就个人而言,我优先考虑一个方法返回一个空集合,然后如果它返回

什么都不返回。返回一个null集合(VB中没有),通常

使客户端代码复杂化 - 因为现在你需要做检查以避免

NullReferenceException。例如(警告Air Code!):


''方法返回null(无)集合

Dim theCollection As List< SomeEntity = GetCollection()

如果不是theCollection什么都没有那么

每个e作为SomeEntity在收藏中

''做的东西

下一个

结束如果


''方法返回一个空集合

For Each e As SomeEntity In GetCollection()

''做东西

下一页


无论如何,你有它:)


-

Tom Shelton

Personally, I prefere when a method returns an empty collection then if it
returns nothing. Returning a null collection (Nothing in VB), usually
complicates the client code - because now you need to do checks to avoid a
NullReferenceException. For example (Warning Air Code!):

'' method returns a null (nothing) collection
Dim theCollection As List<SomeEntity= GetCollection()
If Not theCollection Is Nothing Then
For Each e As SomeEntity In theCollection
'' do stuff
Next
End If

'' method returns an empty collection
For Each e As SomeEntity In GetCollection()
'' do stuff
Next

Anyway, there you have it :)

--
Tom Shelton


所以我不应该这样做:


dim match作为MatchCollection = ReturnMatches(东西)


如果比赛什么都没有

什么也不返回

其他

返回比赛

相反我应该:


返回(匹配为MatchCollection = ReturnMatches(东西))

Tom Shelton" < to ********* @ comcastXXXXXXX.netwrote in message

新闻:QJ ********************* *********@comcast.com。 ..
So I shouldn''t be doing something like:

dim matches as MatchCollection = ReturnMatches(stuff)

if matches is nothing
return nothing
else
return matches
Instead I should:

return (matches as MatchCollection = ReturnMatches(stuff))
"Tom Shelton" <to*********@comcastXXXXXXX.netwrote in message
news:QJ******************************@comcast.com. ..

2008-10-15,Andy B< a _ ***** @ sbcglobal.netwrote:
On 2008-10-15, Andy B <a_*****@sbcglobal.netwrote:

>制作一个返回匹配集合的方法是否安全?

最好只返回一个匹配集合并将代码放在外面

方法验证匹配集合是否为空?
>Is it safe to make a method that returns a match collection or nothing?
or
is it better to just return a match collection and have the code outside
the
method validate that match collection is empty or not?



就个人而言,我喜欢当一个方法返回一个空集合时,如果它返回

什么都不返回。返回一个null集合(VB中没有),通常

使客户端代码复杂化 - 因为现在你需要做检查以避免

NullReferenceException。例如(警告Air Code!):


''方法返回null(无)集合

Dim theCollection As List< SomeEntity = GetCollection()

如果不是theCollection什么都没有那么

每个e作为SomeEntity在收藏中

''做的东西

下一个

结束如果


''方法返回一个空集合

For Each e As SomeEntity In GetCollection()

''做东西

下一页


无论如何,你有它:)


-

Tom Shelton


Personally, I prefere when a method returns an empty collection then if it
returns nothing. Returning a null collection (Nothing in VB), usually
complicates the client code - because now you need to do checks to avoid a
NullReferenceException. For example (Warning Air Code!):

'' method returns a null (nothing) collection
Dim theCollection As List<SomeEntity= GetCollection()
If Not theCollection Is Nothing Then
For Each e As SomeEntity In theCollection
'' do stuff
Next
End If

'' method returns an empty collection
For Each e As SomeEntity In GetCollection()
'' do stuff
Next

Anyway, there you have it :)

--
Tom Shelton



2008年10月15日星期三14:11:17 -0400,Andy B" < a _ ***** @ sbcglobal.net>

写道:
On Wed, 15 Oct 2008 14:11:17 -0400, "Andy B" <a_*****@sbcglobal.net>
wrote:

>制作返回的方法是否安全比赛集合还是什么?或者
最好只返回一个匹配集合并让
方法之外的代码验证匹配集合是否为空?
>Is it safe to make a method that returns a match collection or nothing? or
is it better to just return a match collection and have the code outside the
method validate that match collection is empty or not?



总是返回一个集合(如果没有找到,则为空)可能会使得调用代码写错的可能性降低。如果你

返回Nothing并且调用者没有检查没有例外

将会发生。


如果你总是返回一个收集,然后取决于方法

如何工作,返回一个空集合可能意味着创建一个不必是
如果没有返回,则创建$>



因此可能需要在更安全的代码和创建更多

对象之间进行权衡。 />

如果是我,我可能会返回Nothing,并将方法

定义为返回Nothing或非空集合,以便调用者

不必进行两次测试来检查是否有任何退回。

Always returning a collection (empty if nothing found) may make it
less likely that calling code will be written incorrectly. If you
return Nothing and a caller fails to check for Nothing an exception
will occur.

If you always return a collection, then depending on how the method
works, returning an empty collection might mean that it will be
necessary to create an empty collection that would not have to be
created if Nothing was returned.

So there might be a trade-off between safer code and creating more
objects.

If it were me I would probably return Nothing, and define the method
as returning either Nothing or a non-empty collection so that callers
don''t have to make two tests to check if anything was returned.


这篇关于返回MatchCollection或什么都没有的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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