What is the dominant style for parenthesization of Ruby function calls?

放肆的年华 提交于 2019-12-23 11:15:12

问题


Say I have func_a and func_b which both take one argument, and I want to pass the result of func_b to func_a.

What is the most common way to parenthesize this?

  1. func_a func_b input
  2. func_a func_b(input)
  3. func_a(func_b input)
  4. func_a(func_b(input))

回答1:


You'd have to scan source to find the "most common".

I try to write what makes sense under the circumstances, but would almost always use either:

func_a func_b(arg)
func_a(func_b(arg))

If the functions are named things that "sound like" a sentence or phrase, then I'll drop as many parens as I can.

func_a func_b arg

In other words, if it sounds like something I'd say out loud, I'll write it like I'd say it--a sentence or phrase.

If it doesn't sound like something I'd say in real life, needs parens to enhance clarity, etc. then I'll write it like I'm writing code, because it sounds/looks like code.




回答2:


I can't give you the the most common way, but my personal opinion.

I would reject version one func_a func_b input. It's too confusing, you don't see if input is the parameter of func_b, or if it is the 2nd parameter of func_a.

I prefer version four, it shows explicit, what's the parameter for what (and you see, what is a methodname and what's a variable). But I would add spaces before and after the parenthesis:

func_a( func_b( input ))

or

func_a( func_b(input) )


来源:https://stackoverflow.com/questions/7457347/what-is-the-dominant-style-for-parenthesization-of-ruby-function-calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!