I have a coworker who is actively trying to convince me that I should not use do..end and instead use curly braces for defining multiline blocks in Ruby.
I'm firmly in the camp of only using curly braces for short one-liners and do..end for everything else. But I thought I would reach out to the greater community to get some resolution.
So which is it, and why? (Example of some shoulda code)
context do
setup { do_some_setup() }
should "do somthing" do
# some more code...
end
end
or
context {
setup { do_some_setup() }
should("do somthing") {
# some more code...
}
}
Personally, just looking at the above answers the question for me, but I wanted to open this up to the greater community.
Comes down to personal bias, I prefer curly braces over a do/end block as its more understandable to a higher number of developers due to a majority of background languages use them over the do/end convention. With that being said the real key is to come to an agreement within your shop, if do/end is used by 6/10 developers than EVERYONE should be using them, if 6/10 use curly braces, then stick to that paradigm.
Its all about making a pattern so that the team as a whole can identify the code structures quicker.
I'm voting for do / end
The convention is
do .. end
for multiline and{ ... }
for one-liners.But I like
do .. end
better, so when I have a one liner, I usedo .. end
anyway but format it as usual for do/end in three lines. This makes everyone happy.One problem with
{ }
is that it is poetry-mode-hostile (because they bind tightly to the last parameter and not the entire method call, so you must include method parens) and they just, to my mind, don't look as nice. They are not statement-groups and they clash with hash constants for readability.Plus, I've seen enough of
{ }
in C programs. Ruby's way, as usual, is better. There is exactly one type ofif
block, and you never have to go back and convert a statement into a compound-statement.A couple influential rubyists suggest to use braces when you use the return value, and do/end when you don't.
http://talklikeaduck.denhaven2.com/2007/10/02/ruby-blocks-do-or-brace (on archive.org)
http://onestepback.org/index.cgi/Tech/Ruby/BraceVsDoEnd.rdoc (on archive.org)
This seems like a good practice in general.
I'd modify this principle a bit to say that you should avoid using do/end on a single line because it's harder to read.
You do have to be more careful using braces because it'll bind to a method's final param instead of the whole method call. Just add parentheses to avoid that.
My personal style is to emphasize readability over rigid rules of
{
...}
vsdo
...end
choice, when such choice is possible. My idea of readability is as follows:In more complex syntax, such as multiline nested blocks, I try to intersperse
{
...}
anddo
...end
delimiters for most natural result, eg.Although the lack of rigid rules might produce sligtly different choices for different programmers, I believe that case-by-case optimization for readability, although subjective, is a net gain over adherence to rigid rules.
From Programming Ruby:
So the code
Binds the block to the
param
variable while the codeBinds the block to the function
f
.However this is a non-issue if you enclose function arguments in parenthesis.
The most common rule I've seen (most recently in Eloquent Ruby) is: