Ruby method names ending with ?

Warning: I can’t think up something practical about what I prefer and explain below! Don’t say I didn’t warn you!

Last week I found myself writing a method ending with a question mark, but it wasn’t returning a Boolean. Just as most of the other methods ending with a question mark I wrote the last couple of years it returns either a NilClass instance, which evaluates to false, or some other object instance, which evaluates to true.

The question I want to answer is if such a method should return true or false or is returning nil or some object good enough?

Personally I think it should return a boolean value. I have more arguments opposed to returning a bool than not, but it just feels right, seems more logical. But! if there are humongous performance implications, I’m also happy enough with the way I did it before.

To check just this I wrote a simple class and some benchmarking code to see how big the implications are.  I have two methods which mimics the normal situation and a method which returns a boolean value. I’m using the ‘!!’ trick to accomplish the typecasting. [you can see all the code in my ruby study git repos]

Boolean Typecasting Test results

The test results show, I’m running the code 1 million times, that the difference is marginal. So for me the only logical conclusion is that returning a boolean, which feels more logical to me, is perfectly acceptable and will be my weapon of choice starting as of right now. If you have a practical use to actually justify this, please let me know!

This entry was posted in Ruby and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.


  1. Posted November 3, 2010 at 10:33 | Permalink

    I’m a Schemer, so I can provide you with some perspective from that community. In Scheme you can use any character in a procedure name, including a question mark (I think Ruby took its inspiration of question marks and bangs at the end of method names from Scheme’s conventions), and there are some procedures like “member” which are logically often used to check whether an element is a member of the list (as a boolean) but occasionally it is used to obtain the list tail which has the requested element at its head, and that’s why it’s not called “member?”.

    There are also more bizarre questions like “should the string equality procedure be called string= or string=?” If it’s a predicate it should be string=? (and it is). But then, why isn’t the numeric comparison operator called “=?”? Of course because no sane person would write “=?”, but still, it is an inconsistency :)

    As for a justification: it’s simple. You know what kind of output to expect from a question mark-method, and it would be a violation of the reader’s expectations if you suddenly took the output of such a method and started using it as the key for a hash table! And you know people are going to do it (even if you warn them not to…), so you’d better remove the question mark from the method if it doesn’t return a strict boolean.

  2. Jeroen van Doorn
    Posted November 3, 2010 at 12:17 | Permalink

    Thank you for the ‘Scheme’ insights and some more justification! I, of course, agree with you :)

  3. Posted November 3, 2010 at 12:20 | Permalink

    By the way, I’m at T-DOSE in Eindhoven this weekend at a booth with the Chicken Scheme crew. And on Sunday, Eric and Ferry will hold a presentation about the Dacolt (formerly Renuda) Rails app you might remember we worked on together back in the day. It would be fun if you could also make it to the conference!

  4. Jeroen van Doorn
    Posted November 3, 2010 at 12:24 | Permalink

    Thanks for the invite, but there is a ‘Family Efteling’ weekend scheduled, so I’m afraid I’m not going to make it.

    Please say hi to Eric and Ferry for me!

  5. Posted November 3, 2010 at 12:31 | Permalink

    Too bad. Have fun at the Efteling! I’ll tell them you said hi :)

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>