Parentheses

N

Nikolai Weibull

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.
nikolai
 
Y

Yukihiro Matsumoto

Hi,

In message "Parentheses"

|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.
 
N

Nikolai Weibull

* Yukihiro Matsumoto said:
|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
 
J

Josef 'Jupp' SCHUGT

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top