Too Cool for Internet Explorer

Saturday, March 13, 2010

Ruby TransParenTheses

First thing first, the definition:

Parentheses (singular, Parenthesis)—sometimes called Round Brackets, Curved Brackets, Oval Brackets, or just Brackets, or, colloquially, Parens.

Contain material that could be omitted without destroying or altering the meaning of a sentence.

I think the above definition, must be considered when discussing about the non mandatory Parentheses use in Ruby.

On many discussion groups, people are questioning Matz’s option to not make the use of parentheses mandatory in Ruby Language, mostly around Ruby methods parameters.

Apparently this decision is becoming stronger.

If you run the following piece of code in Ruby 1.8.6, you will get a warning:

p Array.new 31

ptest01.rb:1: warning: parenthesize argument(s) for future version

The same occurs with this one:

p Array.new (3+1), 1

ptest02.rb:1: warning: don't put space before argument parentheses

But if you run them in Ruby 1.9.1 they will pass with no warnings.

Do you really think it would be better this way?

p(Array.new((3+1), 1))

When developing an application I like to put myself in the user’s shoes.

As a user of a reading text (program), I would like to read it easily.

Some times the absence of a rule is not that good.

But the strict use of a rule could be bad as well.

I have this little example:

def isThatSoGood what # I can consider defining the method using parenthesis
puts what == 'GOOD'
end
puts '=========='
# As a text reader point of view, I think this reads better
isThatSoGood 'bad'
isThatSoGood 'GOOD'
puts '=========='
# If you consider a program another text you are reading, I think this is ugly
isThatSoGood('bad')
isThatSoGood('GOOD')
puts '=========='
# Do you need a RULE of EXCEPTIONS
mykhol = []
# If you need to use parenthesis in Ruby methods...
puts mykhol.empty?()
# It is really ugly don't you think?

I can say Matz is right to keep parentheses optional in Ruby, leaving to users the final decision on its usage.