Why I don't use round brackets

ruby / programming

This is a blog post for once NOT ABOUT PICKY! :D So enjoy the tentacle-free space.

Let me be blunt: I really don’t like reading Ruby code that uses a lot of round brackets.

No, let me be blunter: I hate reading code that uses a lot of round brackets.

Actually, it’s like this: Round brackets are the training wheels of a Ruby coder. They might be useful in the beginning, but at some point they should come off!

But let me be less contrarian and just show you why I don’t use them anymore…

Weaning yourself off the training wheels

There’s a few good reasons why I don’t use round brackets anymore.

Less noise

Brackets introduce visual noise. Compare and contrast these two method signatures:

def extract_from(text)

with

def extract_from text

What do you gain by introducing brackets? Would you gain something by introducing them into text?

My name is(Florian Hanke)

If you think this text example has nothing to do with code then we have different views on code readability. It’s more legible to me.

Law of Demeter

You’ve probably heard of the Law of Demeter? If you haven’t, please read about it :)

Not wanting to use round brackets introduces a strain every time I am about to break the Law of Demeter.

Consider this code:

text = extract other_text

Now, if I wanted to call another method on the result, I’d have to write this:

text = extract(other_text).process

Spotting violations is easy for me. I just look for the brackets. If I see brackets in my code, I instantly know that they are there for a good reason and that I actually had a reason to break the Law of Demeter.

Code like

a.b(c).d(e).f

is simply impossible for me, and that’s a good thing!

Typing

This is not about typing speed. It is simply about comfort. The comfort of not having to do bracket acrobatics™.

Not using brackets lets you type as if the code was free text.

As opposed to e.g. JavaScript, Ruby actually lets you do this, so take advantage.

Being explicit about no parameters

Two small counterpoints.

I use Rspec. Chances are, you use it as well.

There’s an expression that goes like this:

thing.should_receive(:some_method).once.with

It’s a fluid interface, so using parentheses is ok for me. One of the exceptions. However, I even add them explicitly to tell the future me that I really don’t expect any parameters:

thing.should_receive(:some_method).once.with()

Equals “with nothing”.

Another exception is the “gobbler” * argument to a method, where Ruby needs brackets to know what it is looking at.

def try(*) end

But I’m used to it!

Yes, and you’re also trained on QWERTY. Doesn’t mean it was a good idea.

But, but, I need to help Ruby with reading my code!

Please. You’re probably the first to cheer when the robot overlords arrive.

Conclusion

It’s a good idea to be sceptical.

I simply asked myself: Why am I actually using brackets when they are not needed?

I couldn’t think of good reasons, while I was able to find some reasons against using brackets.

Hence, no brackets.

WDYT?

Next Unthinking Autoloader

Share


Previous

Comments?