Simple litle style question

K

Kirk Haines

This is just a simple little stylistic query that I became curious about
after one of Ara Howard's questions the other day.

In general, do people prefer:

def simpleExample(t)
x = block_meth do
r = nil
if t == 'forty-two'
r = 42
else
r = 0
end
r
end

x
end


or:

def simpleExample(t)
x = block_meth do
if t == 'forty-two'
r = 42
else
r = 0
end
end
end


both assuming this method is available:

def block_meth
yield
end


i.e. do you explicity declare a return variable and put that variable
right before the 'end' or do you rely on your audience being smart enough
to perceive what the return value is going to be?

On a really short piece of code like this, I appreciate the elegant
simplicity of not being explicit, though my person trend is to be explicit
anyway. On a longer piece of code, though, I can certainly see where it
might not be as clear to a reader of the code just what is being returned,
especially from a casual scan, if it is not done explicitly.

Is this clarity helpful, or is it a minor enough thing that the ugliness
that it imparts to the code isn't worth it? What do you all think?


Kirk Haines
 
G

Grzegorz Chrupała

Kirk said:
This is just a simple little stylistic query that I became curious about
after one of Ara Howard's questions the other day.

In general, do people prefer:

def simpleExample(t)
x = block_meth do
r = nil
if t == 'forty-two'
r = 42
else
r = 0
end
r
end

x
end


or:

def simpleExample(t)
x = block_meth do
if t == 'forty-two'
r = 42
else
r = 0
end
end
end

I would do the following, which is more in the spirit of an expression-based
language:

def simpleExample(t)
block_meth do
if t == 'forty-two' then 42 else 0 end
end
end

Personally, I wouldn't assign to variables that I'm not going to use.
 
J

Jim Weirich

David A. Black said:
I prefer "simple_example" :)
Amen!

And, just for fun, there's also:

def simple_example(t)
block_meth { if t == 'forty-two' then 42 else 0 end }
end

def simple_example(t)
block_meth { t == 'forty-two' ? 42 : 0 }
end

I prefer either of these one-liners over the longer examples. They
succinctly express the heart of the calculation without any extra noise.
(I won't bother with the "t == 'forty-two' && 42 || 0" one :)

Good Heavens! Thank you. Where the other oneliners express the logic
directly, this one tends to cloud it (IMHO).

On the short examples given, I would let Ruby be Ruby and just have the
last (and only) expression be the implicit result of the function.

If the function is longer (more than one expression), then I might use a
return variable, especially if the expression I wish to return is the last
expression only by coincidence. When I use a return variable, I _always_
spell it "result", and only use the name "result" for return variables.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top