By convention in Ruby on Rails applications, methods ending with a ?
character return a boolean, while methods ending with a !
character may modify some state or raise an exception. These are referred to as boolean methods and bang methods:
However, what if we wanted to write a method that modified some state and returned a boolean? For example, the SETNX method in Redis will attempt to set a key, but will only succeed if the key is not currently set. This method returns true
or false
based on if it succeeded. If we were to write a wrapper around the SETNX method, we might try to name it something like modify_some_state!?
to communicate that it may have modified some state and returned a boolean.
Does Ruby on Rails allow us to write a method name using !?
as its ending? For example, what happens if we try to run this code?
If we try to load Foo
in the Rails console, we see the following error:
$ rails c
Running via Spring preloader in process 15120
Loading development environment (Rails 6.1.3)
2.5.1 :001 > Foo.new
Traceback (most recent call last):
1: from (irb):1
SyntaxError (/Users/eric/some_app/app/models/foo.rb:2: syntax error, unexpected '?', expecting ';' or '\n')
def modify_some_state!?
Unfortunately, this results in a syntax error in our application, since we can only use a !
or ?
character at the end of a method name.
However, Ruby allows us to use non-ASCII characters in our method names! The interrobang (represented as ‽
) is a punctuation mark that combines the exclamation and question marks. If we really wanted to, we could use the ‽
Unicode character in a Ruby method name. The following Foo
definition is valid Ruby code:
However, this code has the potential to be extremely confusing and unmaintainable. The ‽
character looks similar to a question mark, and doesn’t appear on a standard QWERTY keyboard. So, even if the above naming convention is possible, I would not recommend actually using it when writing Ruby code 🙂
Here’s my recommendation on naming a method that returns a boolean and modifies state: if you’re ending your method with ?
, use the rest of the method name to make it clear that you’re modifying some state. If you’re ending your method with !
, use the rest of the method name to make it clear that you’re returning a boolean.
Returning to the SETNX wrapper example, two potential names we could use for that method are:
successfully_set_key?
: the?
communicates that we’re returning a boolean, but theset
verb also shows that we’re modifying some state.set_key_if_empty!
: the!
communicates that we’re modifying some state, but the conditionalif
implies some boolean value associated with the result of the method.
Selecting a ?
or !
suffix is also dependent on how the method will be used within the program. A method used as a guard clause, for example, should end with a ?
. A method whose result won’t affect the control flow, on the other hand, may be better suited to ending with a !
.
Start your journey towards writing better software, and watch this space for new content.