[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Parentheses

Nikolai Weibull

10/15/2003 4:04:00 PM

3 Answers

matz

10/15/2003 4:18:00 PM

0

Hi,

In message "Parentheses"
on 03/10/16, Nikolai Weibull <ruby-talk@pcppopper.org> writes:

|OK, is there a general concensus on when it is OK to omit parentheses
|and when it is not? I prefer skipping parentheses as often as possible,
|but I'm not quite sure when I may run into trouble and when it is OK.

foo 1,2,3 # this is OK (top level)
a = foo 3 # this is not recommended (part of expression)

matz.

Nikolai Weibull

10/15/2003 4:29:00 PM

0

* Yukihiro Matsumoto <matz@ruby-lang.org> [Oct, 15 2003 18:20]:
> |OK, is there a general concensus on when it is OK to omit parentheses
> |and when it is not? I prefer skipping parentheses as often as possible,
> |but I'm not quite sure when I may run into trouble and when it is OK.
>
> foo 1,2,3 # this is OK (top level)
> a = foo 3 # this is not recommended (part of expression)
OK, thanks. Did this change in 1.8.0 in any way?
nikolai

--
::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

Josef 'Jupp' Schugt

10/15/2003 11:51:00 PM

0

Hi!

* Nikolai Weibull; 2003-10-15, 21:32 UTC:
> OK, is there a general concensus on when it is OK to omit
> parentheses and when it is not?

The answer depends on what 'OK' means. The obvious answer is: "You
can drop any parentheses unless doing so breakes the program." That
doesn't help you a lot, does it?

A better approach seems to be "Remove or add non-essential
parentheses unless the code 'feels good'". What 'feels good'? This
depends on the problem you face: The following can 'feel good' if it
means 'compute a sum, divide it by a value then divide it by a
quotient':

(1.1 + 2.2) / 3.3 / ((2.2 + 3.3) / (1.0 + 0.1))

but if the same computation is done meaning 'compute a fraction and
then divide it by another fraction' one should rather write

((1.1 + 2.2) / 3.3) / ((2.2 + 3.3) / (1.0 + 0.1))

This is the techical side of 'feels good'. The other side is the
programmer. Many programmers are 'parameter-centered' while many
others are 'option-centered'.

One can see h.delete in two ways: As a function that takes a
parameter or as a command that understands a numerical option.
Depending on how your interpretation of h.delete is, you either
prefer using parentheses or not.

irb(main):001:0> a = [3, 1, 4, 1, 5]; b = [3, 1, 4, 1, 5]
=> [3, 1, 4, 1, 5]
irb(main):002:0> a.delete(1)
=> 1
irb(main):003:0> b.delete 1
=> 1
irb(main):004:0> a
=> [3, 4, 5]
irb(main):005:0> b
=> [3, 4, 5]

> I prefer skipping parentheses as often as possible, but I'm not
> quite sure when I may run into trouble and when it is OK.

Corollary of "Remove or add non-essential parentheses unless code
'feels good'": "Don't remove parentheses that feel like trouble, add
parentheses where white space feels like trouble".

Please take notice of signature! / Bitte Signature beachten!

Josef 'Jupp' Schugt
--
db Wenn sie mir ohne meine Einwilligung geschickt wurde, db
dpqb wird eine E-Mail > 100 kB ungelesen entsorgt dpqb
dp::qb If you send me an e-mail > 100 kB without my dp::qb
dp::::qb consent it will be silently discarded dp::::qb