On Sunday, October 10, 2010 07:25:15 pm egervari wrote:
I just started playing around with ruby and rails, and one thing I’ve
noticed is that the style of the code is a little odd. People don’t
use parenthesis most or even all of the time.
Right.
I find this to look messy.
I find redundant crap looks messy. Without parentheses, it can often
read like
English, and it’s very easy to build remarkable DSLs.
It’s hard to tell what is the method, what
is the variable, and how the code is formed at a glance.
I don’t agree, but maybe I’ve been looking at it for too long. Maybe it
just
takes practice. However, I think it’s much more important whether I can
tell
what a piece of code is trying to do – it may be slightly more
difficult, but
I can always figure out how it works.
Is there any reason tastes in style have changed? I think it would be
best to stick with the parenthesis just to make everything consistent.
With what?
I am consistent. I don’t use parentheses unless I have to. This is true
even
in languages where parentheses are required for method calls. Consider:
distance = Math.sqrt(xx + yy)
Yes, I had to write parens around the body of the square root to make it
unambiguous. Were this Java, I would also be required to because
Math.sqrt is
a method call. But I didn’t do this, though I could have:
distance = (Math.sqrt((xx) + (yy)))
I don’t know about you, but I find that a lot less readable.
I’m also consistent with the majority of the Ruby community, so about
all
adding parens would help with is being consistent with other programming
languages – but I think we should celebrate Ruby’s strengths, not try
to
bring it down to the lowest common denominator.
I’m also consistent with how you run commands. After all, you don’t do
this:
‘gem’ ‘install’ ‘rails’
…do you? Single-quotes would be unambiguous, and it would make your
commands
more consistent with the times when you actually need them to escape
special
characters, like spaces:
rm ‘My Essay.odt’
But even though it’s only a few extra keystrokes, I think most of us
would
find the extra noise to be less readable, not more. Quoting, in this
case,
is a tool to remove ambiguity, nothing more – and I would argue parens
serve
the same purpose in most languages, and should not be required when the
meaning is unambiguous.
Same with semicolons. You can put them at every line, but Ruby is smart
enough
to figure out what you mean most of the time, so instead, you only need
to use
semicolons (or a backslash at the end of a line) as an exception to the
rule,
as a tool to remove ambiguity.
It should be easier to refactor code and inline things if the
parenthesis were already there after all.
How so?