[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Difference between nil? vs. (variable != nil)...

Ceol

12/6/2006 8:31:00 PM

Is there a particular reason you're comparing to nil? Normally I'd just
write something like

(@product_coverages && @product_coverages.length > 0)

- James Moore


4 Answers

Jason Vogel

12/7/2006 5:41:00 AM

0

I found 'return "" if product_coverage.external_description.nil?' as a
source sample somewhere, and I liked the readability factor. It seemed
very "Ruby-ish" to a PowerBuilder/Java/C# developer :-).

In the end, I'm trying to figure out my "best practice" for iterating
over an array of objects. I'll happily take any suggestions.

Thanks,
Jason

On Dec 6, 2:31 pm, "James Moore" <bans...@banshee.com> wrote:
> Is there a particular reason you're comparing to nil? Normally I'd just
> write something like
>
> (@product_coverages && @product_coverages.length > 0)
>
> - James Moore

David Vallner

12/7/2006 6:55:00 AM

0

James Moore wrote:
> Is there a particular reason you're comparing to nil? Normally I'd just
> write something like
>
> (@product_coverages && @product_coverages.length > 0)
>

This is Perlish, and there's people (myself included) that don't like
the style - it hides the nil-check, which is useful to be more visible
if you're doing it as a special case filter, like snippet B in the
original post does. I usually prefer getting rid of special cases like
missing arguments etc. with a series of checks and returns / exceptions
in the worse cases at a the beginning of a method to keep precondition
verification in one place instead of scattered through the code with
failure consequences interspersed with regular processing.

David Vallner

Jason Vogel

12/10/2006 6:33:00 AM

0

I appreciate the advice. I've just being trying to be careful to not
be too C#/Java-ish. I've never done any Perl.

Thanks,
Jason

On Dec 7, 12:55 am, David Vallner <d...@vallner.net> wrote:
> James Moore wrote:
> > Is there a particular reason you're comparing to nil? Normally I'd just
> > write something like
>
> > (@product_coverages && @product_coverages.length > 0)This is Perlish, and there's people (myself included) that don't like
> the style - it hides the nil-check, which is useful to be more visible
> if you're doing it as a special case filter, like snippet B in the
> original post does. I usually prefer getting rid of special cases like
> missing arguments etc. with a series of checks and returns / exceptions
> in the worse cases at a the beginning of a method to keep precondition
> verification in one place instead of scattered through the code with
> failure consequences interspersed with regular processing.
>
> David Vallner
>
> signature.asc
> 1KDownload

David Vallner

12/10/2006 3:50:00 PM

0

Jason Vogel wrote:
> I appreciate the advice. I've just being trying to be careful to not
> be too C#/Java-ish. I've never done any Perl.
>

Huh? Why not? You should write that a) you can read easily, b) other
people working on the codebase can read easily.

The "Approved By The Grand Commitee Of Java Bashers (r)" stamp comes, in
my opinion, very, very far behind. Consider the practical criteria first.

David Vallner