NotNot Alternative

There’s a common ruby convention out there, I call it NotNot. You’ll find it often in predicate methods, and it looks like it sounds, ‘!!’. I suppose you could call it “Bang Bang”, or perhaps it has a real name I’m not aware of.

I don’t like NotNot, primarily I think because in my head it seems like the dreaded English grammar double negative. In other words, it’s just weird.

The point of the NotNot is for your classes to return true/false, rather than just truthy/falsy. Let’s start with a simple example predicate:

What? That looks kind of strange, however, it will pass conditional tests, since ruby treats the values nil and false both as falsy values, while other values as truthy. But again, the returns look weird. Enter the NotNot:

Hey, those returns looks better, I see we’re getting back real true/false which is really what we want. But that NotNot looks weird. Think about it in your head, what’s the value of not not false…uh.

Say hello to the TrueClass#& method. The TrueClass RDoc states that the & method “Returns false if obj is nil or false, true otherwise.”

You’ll note we’re still getting back true/false. The method is less terse than the NotNot, but I think slightly a better match intuitively.

In the end, it really boils down to style and readability. If you code Ruby, while it’s important to know the NotNot, I’d advise something your brain doesn’t have to double unroll.

Hat tip to my friend Bill for taking me straight from example 1 to 3 when I started in Ruby.

—Jul 27, 2011