Josef 'Jupp' Schugt
10/15/2003 11:51:00 PM
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