[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

nil being empty

Ohad Lutzky

10/5/2006 6:21:00 PM

Show of hands - who thinks this is bad form?

class NilClass
def empty?; true; end
end

--
Posted via http://www.ruby-....

48 Answers

Ara.T.Howard

10/5/2006 6:28:00 PM

0

Ohad Lutzky

10/5/2006 6:37:00 PM

0

Ooh, actually it is bad form. Seeing as rails already defines .blank? to
be aliased to empty? for strings and arrays, and true for nils.

--
Posted via http://www.ruby-....

Martin Coxall

10/6/2006 8:42:00 AM

0

> Show of hands - who thinks this is bad form?
>
> class NilClass
> def empty?; true; end
> end

Can I wave a different body part at you to register my disgust?

Martin

David Chelimsky

10/6/2006 11:29:00 AM

0

On 10/5/06, Ohad Lutzky <lutzky@gmail.com> wrote:
> Show of hands - who thinks this is bad form?
>
> class NilClass
> def empty?; true; end
> end

Doesn't it depend on context? The other responders seem to suggest
that is objectively bad form, with which I can agree (generally), but
in a case like this:

collection.select do |item|
item.respond_to?(:empty?) and (!item.empty?)
end

I might prefer

collection.select do |item|
true unless item.empty?
end

or this (which, though more terse, I personally find less readable)

collection.select do |item|
!item.empty?
end

One of the beauties of the language (for me) is that language
invariants become somewhat less "in" and more "variant".

dblack

10/6/2006 11:38:00 AM

0

dblack

10/6/2006 11:43:00 AM

0

Rimantas Liubertas

10/6/2006 11:43:00 AM

0

> Show of hands - who thinks this is bad form?
>
> class NilClass
> def empty?; true; end
> end

My hand is up.
It is strange to ask "is your spoon empty?" when there is no spoon...


Regards,
Rimantas
--
http://rim...

Jim Crossley

10/6/2006 11:58:00 AM

0

Ohad Lutzky <lutzky@gmail.com> writes:

> Show of hands - who thinks this is bad form?
>
> class NilClass
> def empty?; true; end
> end

FWIW, neither Ola Bini nor Martin Fowler consider it necessarily bad
form, depending on your context, i.e. whether it adds to or detracts
from the maintainability of your code.

See #7 here:
http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techn...

Or the pattern here:
http://www.refactoring.com/catalog/introduceNullO...

Odds are it is probably bad form and it's possible to encapsulate the
reason *why* you're testing for empty into a higher-level method of a
domain class upon which you truly could apply the pattern. Better
that domain class than the object, nil, I think.

Jim

Farrel Lifson

10/6/2006 12:30:00 PM

0

On 06/10/06, David Chelimsky <dchelimsky@gmail.com> wrote:
> Doesn't it depend on context? The other responders seem to suggest
> that is objectively bad form, with which I can agree (generally), but
> in a case like this:
>
> collection.select do |item|
> item.respond_to?(:empty?) and (!item.empty?)
> end
>
> I might prefer
>
> collection.select do |item|
> true unless item.empty?
> end
>
> or this (which, though more terse, I personally find less readable)
>
> collection.select do |item|
> !item.empty?
> end
>
> One of the beauties of the language (for me) is that language
> invariants become somewhat less "in" and more "variant".
>
>

You could make it slightly more elegant with an extra function call
collection.select.do |item|
!Array(item).empty? #Could also use !String(item).empty depending
on what item is
end

Farrel

Trans

10/6/2006 1:51:00 PM

0


Ohad Lutzky wrote:
> Show of hands - who thinks this is bad form?
>
> class NilClass
> def empty?; true; end
> end

Put your hands down. Notice that everyone that says it's bad form is
doing so on purely ideological basis. I challenge them to show one good
_practical_ case where it's truly "bad". And no, purposefully expecting
a NoMethod error doesn't count --that IS bad form.

T.