Do you leave parentheses in or out in Ruby? [close

2019-01-13 05:19发布

When possible.. do you leave parentheses in or out in Ruby?

9条回答
家丑人穷心不美
2楼-- · 2019-01-13 05:30

I leave them out when I'm doing DSL-ish stuff, like t.column or has_many in rails. The rest of the time, it generally comes down to clarity, and it's probably an even split.

查看更多
太酷不给撩
3楼-- · 2019-01-13 05:33

I tend to leave them out when doing assertions such as assert_equal. Maybe it's to make it domain specific language-like.

查看更多
Rolldiameter
4楼-- · 2019-01-13 05:37

I use parens as comments to help the future me... who is likely to have fewer brain cells than the current me :-)

Nothing worse than looking at some code you wrote 2 years ago and misunderstanding it, so that you break something while modifying it.

If parens will save the future me a few minutes (or hours) in the future, I'll put in as many as needed to make the statement crystal clear.

-- John

查看更多
淡お忘
5楼-- · 2019-01-13 05:37

I try to leave them out, if at all possible. I think it makes code easier to read (generally speaking).

查看更多
地球回转人心会变
6楼-- · 2019-01-13 05:40

If you mean in function calls, I always put parenthesis because it's always easier to read. If you mean in conditions (if, while) I only put parenthesis when they're necessary.

查看更多
淡お忘
7楼-- · 2019-01-13 05:43

I guess I do both, but I definitely keep them in if it adds to readability and avoids statements that look ambiguous.

查看更多
登录 后发表回答