Communicating Intent: The Perpetually Misunderstood Ruby Bang(!)
Tags: ruby
Published: 2016-02-11 18:44:47

<p>As software projects grow, being able to demonstrate your intention to other developers through clear and reasonable code is paramount. In this series I cover short tips to do just that. The first article in the series can be found on my blog: <a href="http://tombroomfield.com/communicating-intent-1-tap/" target="_blank">Ruby's Tap</a>.</p>
<p>Today we cover the bang(!) symbol. In my opinion is one of the most common, yet misunderstood tactics for communicating intent to other developers. While subtle, it can actually be pretty useful when used consistently.</p>
<p>The first thing to remember is that the <code>!</code> in ruby, at the end of a method name, such as <code>save!</code> does nothing special on it's own. It forms part of the method name and it's only purpose is to communicate something else about what that method might do.</p>
<p>A quick google search will lead to a lot of incorrect information. For instance, <a href="http://stackoverflow.com/a/612196" target="_blank">here</a>, <a href="http://stackoverflow.com/a/18801459" target="_blank">here</a>, <a href="http://stackoverflow.com/a/31598104" target="_blank">here</a>, <a href="http://stackoverflow.com/a/16301829" target="_blank">here</a>, <a href="http://stackoverflow.com/a/16301928" target="_blank">here</a>, <a href="http://stackoverflow.com/a/6208446" target="_blank">here</a>, <a href="http://stackoverflow.com/a/6208856" target="_blank">here</a>, <a href="http://stackoverflow.com/a/6208417" target="_blank">here</a>, <a href="http://stackoverflow.com/a/6260608" target="_blank">here</a>, <a href="http://stackoverflow.com/a/33923572" target="_blank">here</a>, <a href="http://qr.ae/RO9KE9" target="_blank">here</a> and <a href="http://www.wellho.net/mouth/2893_Exclamation-marks-and-question-marks-on-ruby-method-names.html" target="_blank">here</a>.</p>
<p>These all tell you that the bang symbol simply modifies the object in place. It certainly <em>does</em> do this in some cases, but it is definitely <em>not</em> what bang means. An example of this is <code>downcase</code> vs <code>downcase!</code>. The <code>downcase!</code> will modify the existing string, while <code>downcase</code> will return a new string.</p>
<p>This is useful, however, the logic falls down very quickly. For example, the <code>pop</code> method:</p>
<pre class="language-ruby"><code>a = [1,2,3]
a.pop
a #=&gt; [1,2]</code></pre>
<p><code>pop</code> modified the array, but has no <code>!</code>.</p>
<p>Another example is the <code>save/save!</code> methods in Rails. <code>save</code> returns <code>nil</code> on a validation error, while <code>save!</code> throws an error.</p>
<p>In the <a href="https://github.com/geekq/workflow" target="_blank">Workflow</a> gem, you use <code>state!</code> to move to a particular state.</p>
<p>None of these methods have anything to do with modifying the object, so why are they using bang?</p>
<p>Luckily Matz - the creator of Ruby - has something to say on the issue:</p>
<blockquote>
<p>The bang (!) does not mean "destructive" nor lack of it mean non destructive either. The bang sign means "the bang version is more dangerous than its non bang counterpart; handle with care". Since Ruby has a lot of "destructive" methods, if bang signs follow your opinion, every Ruby program would be full of bangs, thus ugly.</p>
<p>&nbsp;</p>
<p>~ Yukihiro Matsumoto</p>
<br />
<p><a href="https://www.ruby-forum.com/topic/176830#773946" target="_blank">Source</a></p>
</blockquote>
<p>So there we go. Use bang when you have a method that has the potential to be more dangerous. I find it nice to leave a comment on any bang method, explaining the danger.</p>
<p>Some good use cases for the bang:</p>
<ul>
<li>Throwing an error on failure instead of returning nil.</li>
<li>Forcing a fetch to bypass cache and recalculate.</li>
<li>A method that hides a lot of complexity, or has performance implications.</li>
<li>Any other method that has may cause side effects that may not be expected.</li>
</ul>
<p>Bang can be a fantastic tool for letting other developers know to proceed with caution, so use it when it makes sense. When exploring code, take special care to look out for these methods and ensure they are being used correctly. They can often reveal bugs or performance issues if the danger has not been respected.</p>