i will put parentheses everywhere now.

by janedotx7

I wish Ruby had mandatory parentheses. These are the two reasons why:

1. def waltz(*args)
#stuff
end

def hustle(*args)
#stuff
end

hustle waltz 1, 2, 3, 4

What if I’d intended hustle(waltz(1, 2), 3, 4))? I recently broke
the sign up button on the twitter.com home page (dear TechCrunch, that
broken code never got deployed if you’re curious) because there had
been no parentheses in a piece of code where one helper was calling
the result of another helper. It looked something like: hustle
waltz(long_parameter_name, another_long_parameter_name,
:long_parameter_name => long_value), another_complicated_parameter. I
think that while Ruby often looks better this way, it can encourage
code that’s hard to read for later maintainers. Or maybe I’m just
n00b.

2. Wouldn’t it be nice if I could just do this:
def polka(one, two, three, four)
end

def frack(fn, one, two)
fn(one, two)
end

frack(polka, one, two)

Due to the optional parentheses, Ruby evaluates any mention of a
function as an actual attempt to call it and complains about missing
parameters if you try to refer to a function by just its name. If
parentheses were mandatory, you could pass around functions quite
easily, but instead, we have to jump through these hoops:

def frack(one, two, &fn)
yield(one, two)
end

frack(one, two) { |a, b| polka(a, b) }

I’ve never had to pass around a named function, mostly because I
can just use blocks. Still, this seems like shockingly revolting
syntax for something that really ought to be rather easy.

Comments? I’d be glad to hear other thoughts. Also, if there
really is an easier way to pass around a function than wrapping it in
a Proc, block, or lambda, please embarrass me. I want to know.

Advertisements