|
|
|
SearchCategories
Books by the AuthorOther Ruby Projects |
Do I Need (These Parentheses()?)
Posted over 2 years ago
in Early Steps and Ruby Tutorials.
If you came to Ruby via the Learn to Program book or just don't yet have a consistent set of rules for when you do and don't need parentheses in Ruby code, this post is for you. I have nothing against Learn to Program, just to be clear. A member of my family is learning Ruby from it and it's going pretty well. I recommend it. However, Chris is a little inconsistent with his use of parentheses in the code samples, and worse, he doesn't really give you a good set of rules to decide when to make the choice. No problem. Let me give you the rules. I'm a chess player. In learning chess, you really go through two phases. First, you learn the rules of strategy. These will make you good because the rules are designed to help you avoid common mistakes. Now, to get great, you go through the second phase: learning when to break the strategy rules. Ruby is exactly the same. Here's the only rule of strategy you need to learn to get good: methods need parentheses around their arguments. So you want to write:
and:
I'm serious. That's it. You're good now. Congratulations! Wasn't that easy? If you've had enough learning for one post, call it an early day and be secure in the fast that you are now knowledgeable in the Way of the Parentheses. If you have some energy left, read on and I'll make you great... First, let's talk about what's not a method call. Mainly
and:
OK, here's another easy one. I said the rule was, "...need parentheses around their arguments." If we take that literal, we can figure out the next exception: no arguments means parentheses aren't needed. So it's:
and:
Now, because you're probably using them often, we will relax the rules on puts() and p(). You can leave them off those calls, as long as you are following the rest of these rules. That let's us write:
or even:
One last exception. The question methods read very prettily without parentheses and beautiful code is always a good goal, so drop them in simple conditionals with just one question and argument. For example:
Beware of that last one though. If the condition is getting complicated, add the parentheses:
Finally, beware of the gotchas. If you have a bunch of
|
|
|
|
Here is a rule I follow:
When you are calling a method without an explicit receiver (like a method in your class) and it takes no parameters, use parentheses or add an explicit receiver (self). It is very confusing sometimes if you do not.
Although I agree with thesee rules in general, it's interesting to point out that Rails (specifically the Rails documentation) seems adamantly against parantheses, (and braces on hash tables...)
Although it makes their code almost read like English, it can be hard to understand what exactly is going on if you're new to Ruby (it seems that quite a bit of Rails programmers jump right in knowing no Ruby beforehand)
Any insight?
Yeah, Rails has a lot to answer for in this regard, IMHO, although at the same time I've written DSL type stuff myself that's just looked better without parens. Maybe it's just because like null said, a lot of Rails programmers see Rails before Ruby. Ho hum...
These rules are my opinion of what's correct. Obviously, others, including the Rails core team, may not agree with me 100%. I try to worry more about whether I am doing things right.
Also, I'm pretty sure Ruby herself agrees with me:
My only rule of thumb is whenever ruby or I have problems parsing a method call, I add parantheses. It works pretty well.
Given that I've been doing a lot of Rails lately and that I rather like dropping the parenthesis when possible, the rules I tend to follow are much in line with your own, however, I also tend to think of whether or not my method call is a statement, or a request for a resource. For instance:
I always use parenthesis in documentation, without exception.
Last example could mention that
and
and
work out OK
but
is not working (by default).
Considering puts' speciality I'm not even sure which one from the first three is the best.
Your rules seem to say
is preferred to I don't think I agree.Jim: Very good point there. I treat equal method accessors as operators and I only use parenthesis for those as needed. Good catch.
Similar to Jim's comment, if you're going to update your suggestions, you might want to include the #[] and #[]= methods as no-paren aspects, also.
Again that falls into the operators category with me, so yes we don't need parenthesis with those.