[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Parentheses

khaines

10/15/2003 6:00:00 PM

Bob Gustafson said:
> I prefer the parentheses.
>
> Parens are an 'ambiguity reducing notation' - without the parens, you have
> to ask (from time to time) - "What is the precedence of these operators in
> this language".
>
> Less ambiguity means clearer (human) code.

I think that it depends.

#1
-----
dbh.select_all sql do |row|
media_list.push QMedia.new *row
end
-----

#2
-----
dbh.select_all(sql) do |row|
media_list.push(QMedia.new(*row))
end
-----

I think that #1 is easier to read than #2. However, consider:

#3
-----
dbh.select_all sql,@client.idx do |row|
media_list.push QMedia.new me.idx,row[0],row[1]
end
-----

#4
-----
dbh.select_all(sql,@client.idx) do |row|
media_list.push QMedia.new(me.idx,row[0],row[1])
end
-----

If there are multiple arguments, I prefer to use parens to group them and
help make it clearer to my eyes where the groupings are.


Kirk Haines


1 Answer

Robert Klemme

10/15/2003 7:01:00 PM

0


"Kirk Haines" <khaines@enigo.com> schrieb im Newsbeitrag
news:60989.148.78.249.33.1066240635.squirrel@enigo.com...
> Bob Gustafson said:
> > I prefer the parentheses.
> >
> > Parens are an 'ambiguity reducing notation' - without the parens, you
have
> > to ask (from time to time) - "What is the precedence of these operators
in
> > this language".
> >
> > Less ambiguity means clearer (human) code.
>
> I think that it depends.
>
> #1
> -----
> dbh.select_all sql do |row|
> media_list.push QMedia.new *row
> end
> -----
>
> #2
> -----
> dbh.select_all(sql) do |row|
> media_list.push(QMedia.new(*row))
> end
> -----
>
> I think that #1 is easier to read than #2. However, consider:

Hm, I found #2 easier to read, since it's immediately obvious what belongs
to the argument list. But maybe that's just while reading email: my editor
would have colored the "do" differently...

> #3
> -----
> dbh.select_all sql,@client.idx do |row|
> media_list.push QMedia.new me.idx,row[0],row[1]
> end
> -----
>
> #4
> -----
> dbh.select_all(sql,@client.idx) do |row|
> media_list.push QMedia.new(me.idx,row[0],row[1])
> end
> -----
>
> If there are multiple arguments, I prefer to use parens to group them and
> help make it clearer to my eyes where the groupings are.

I'd even spent some spaces, at least after the ",":

dbh.select_all(sql, @client.idx) do |row|
media_list.push QMedia.new(me.idx, row[0], row[1])
end

Often I do:

dbh.select_all( sql, @client.idx ) do |row|
media_list.push QMedia.new( me.idx, row[0], row[1] )
end

My internalized rule of thumb on the parentheses seems to be, that I use
them whenever there are more than two method invocations in a line. I find

if foo( bar ) && baz( foz )

more readable than

if foo bar && baz foz


Regards

robert