Best practice for set-and-test idiom?

K

Kevin Nolan

I've been using Ruby/Rails for about about four months and wonder what
the the Ruby alternative to the popular C/C++ idiom of setting and
testing a variable in the condition of an 'if' statement. If C/C++ the
usage is typically:

if ((ptr = fcn_returning_ptr(...)) == NULL) {
val = ptr->ref
} else {
val = DEFAULT
}

or the compact version:

val = (ptr = fcn_returning_ptr(...)) ? ptr->ref : DEFAULT

Which, when I translate to Ruby yields (ActiveRecord example):

unless (ar = Model.find(...)).nil?
val = ar.attribute
else
val = DEFAULT

or the equivalent expression.

It seems to me that this is very unRuby-like and I don't see any similar
usages in any of the code-bases in which I have looked.

Will someone please provide a counter-example?

Thanks,

kevin nolan
 
T

Tim Hunter

Kevin Nolan wrote:
(snip)
or the compact version:

val = (ptr = fcn_returning_ptr(...)) ? ptr->ref : DEFAULT

Which, when I translate to Ruby yields (ActiveRecord example):

unless (ar = Model.find(...)).nil?
val = ar.attribute
else
val = DEFAULT

or the equivalent expression.

It seems to me that this is very unRuby-like and I don't see any similar
usages in any of the code-bases in which I have looked.

Will someone please provide a counter-example?

Ruby has the ternary ?: operator, too:

val = (ar = Model.find(...)) ? ar.attribute : DEFAULT
 
K

Kevin Nolan

Tim said:
Kevin Nolan wrote:
(snip)

Ruby has the ternary ?: operator, too:

val = (ar = Model.find(...)) ? ar.attribute : DEFAULT

Tim,

I know -- I just omitted it for the sake of brevity. Do you have any
thoughts about a better, or my ruby-like technique?
 
T

Tim Hunter

Kevin Nolan wrote:
(snip)
I know -- I just omitted it for the sake of brevity. Do you have any
thoughts about a better, or my ruby-like technique?

Actually I suck at these kinds of stylistic questions. If I want
somebody else (where "somebody else" includes "me, after a few months
when I've forgotten what I was doing") to be able to easily understand
my code I use the "long" version with no short-cuts. If it's just for me
I stick with the first thing that works.
 
G

Gavin Sinclair

     unless (ar = Model.find(...)).nil?
        val = ar.attribute
     else
        val = DEFAULT

   or the equivalent expression.

It seems to me that this is very unRuby-like and I don't see any similar
usages in any of the code-bases in which I have looked.

Will someone please provide a counter-example?

Does the Model.find(...) need to be buried in a test condition? If
it's retrieving worthwhile data, it's worth a line of its own.

model = Model.find(...)
val = (model && model.attribute) || DEFAULT

The second line could be called idiomatic Ruby. Whether it appeals to
you or not, I can't predict :)

For situations where more complex processing is required before
setting the variable, I am a _big_ fan of these:

val =
if condition then
processing
processing
value1
else
processing
value2
end

val =
case x
when 1: ...
when 2: ...
...
end

Cheers,
Gavin
 
A

ara.t.howard

[Note: parts of this message were removed to make it a legal post.]


Does the Model.find(...) need to be buried in a test condition? If
it's retrieving worthwhile data, it's worth a line of its own.

spoken like someone who has debugged code from logs more than once ;-)
model = Model.find(...)
val = (model && model.attribute) || DEFAULT

The second line could be called idiomatic Ruby. Whether it appeals to
you or not, I can't predict :)

For situations where more complex processing is required before
setting the variable, I am a _big_ fan of these:

val =
if condition then
processing
processing
value1
else
processing
value2
end

val =
case x
when 1: ...
when 2: ...
...
end


me too - the indent is a great indicator. sometimes i even do

val = (
if condition then
processing
processing
value1
else
processing
value2
end
)

for more visual distinction

cheers.

a @ http://codeforpeople.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top