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
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