What do the vast majority of Rails shops do? Since Ruby allows for either I’m sure there is a set standard I should probably fixate on making a habit since I’m just starting out with Rails.
According to Pragmatic Agile Web Development With Rails (4th Edition):
In Rails applications, you’ll find that most method calls involved in
larger expressions will have parenthe- ses, while those that look more
like commands or declarations tend not to have them.
So which is it? Or is it the wild west out there?
The Rails contributing style guide seems to favor
over
But most of the code I’ve seen seems to buck that trend. I would definitely say that you shouldn’t use open parentheses for a method call without parameters. The one guiding principle I’ve noticed is to emphasize code human readability. That leads me to the following conclusions:
Don’t use the parentheses if
Your method call has no arguments
Your method call is the only thing on the line
Do use the parentheses if
Your method call is on the same line as a number of method calls
However, there are different conventions for different situations that you’ll probably only see by looking at a lot of code. For example, methods that execute within a class definition mostly don’t use parentheses, and have each argument on a different, indented line: