<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Jake Goulding]]></title>
  <link href="http://jakegoulding.com/atom.xml" rel="self"/>
  <link href="http://jakegoulding.com/"/>
  <updated>2016-11-18T17:45:06-05:00</updated>
  <id>http://jakegoulding.com/</id>
  <author>
    <name><![CDATA[Jake Goulding]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Rust on an Arduino Uno, Part 6]]></title>
    <link href="http://jakegoulding.com/blog/2016/05/27/rust-on-an-arduino-uno-part-6/"/>
    <updated>2016-05-27T11:11:11-04:00</updated>
    <id>http://jakegoulding.com/blog/2016/05/27/rust-on-an-arduino-uno-part-6</id>
    <content type="html"><![CDATA[<p>We can&rsquo;t yet compile the stock version of libcore, so in the meantime
we have our own version with the essentials. Because we&rsquo;ve directly
added this code to our project, each recompile takes a while. It&rsquo;d be
really nice if we could use Cargo like a Real Rust Project would,
allowing us to compile our modified libcore once and reuse it.</p>

<!-- more -->


<p>Create a new library crate (<code>cargo new avr-core</code>) and move all of the
hacked-up core files that we created before into the <code>src</code> directory:</p>

<ul>
<li>clone.rs</li>
<li>cmp.rs</li>
<li>intrinsics.rs</li>
<li>marker.rs</li>
<li>ops.rs</li>
<li>option.rs</li>
</ul>


<p>Additionally, create a <code>lib.rs</code> with the top-level core items:</p>

<ul>
<li>all the <code>feature</code> flags</li>
<li>the <code>prelude</code></li>
<li>module references</li>
<li>the <code>eh_personality</code> and <code>panic</code> handlers.</li>
</ul>


<p>Now we can create a binary crate that will use our AVR-compatible
libcore. After <code>cargo new --bin blink</code>, add a path to the core library:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>[dependencies.avr-core]
</span><span class='line'>path = "../avr-core"</span></code></pre></td></tr></table></div></figure>


<p>We can remove a bunch of junk from our <code>main.rs</code> and just import the
interesting core items:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>extern crate avr_core;
</span><span class='line'>
</span><span class='line'>use avr_core::prelude::*;
</span><span class='line'>use avr_core::intrinsics::{volatile_load, volatile_store};
</span><span class='line'>use avr_core::marker::PhantomData;</span></code></pre></td></tr></table></div></figure>


<p>Now when we compile, we only need to rebuild our own code, not all of
libcore! Much better.</p>

<hr />

<p>Let&rsquo;s continue improving our code. The last thing we did was to hook
up an interrupt handler for the timers, but we had to add a bunch of
assembly to make the handler behave in the proper way. As suggested in
the previous post, there&rsquo;s a much better way to do it.</p>

<p>Rust allows us to declare <code>extern</code> functions with a <em>calling
convention</em>. A calling convention describes where the arguments are
located, where the return value should be placed, and what registers a
function is allowed to change.</p>

<p>There are two special calling conventions for AVR code:
<code>avr-interrupt</code> and <code>avr-non-blocking-interrupt</code>. They are basically
the same, except that the latter immediately re-enables interrupt
handling when it starts. With the former, you don&rsquo;t have to worry
about one interrupt happening while you are handling another.</p>

<p>That means we can rewrite our interrupt handler much easier:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>#[no_mangle]
</span><span class='line'>pub unsafe extern "avr-interrupt" fn _ivr_timer1_compare_a() {
</span><span class='line'>    let prev_value = volatile_load(PORTB);
</span><span class='line'>    volatile_store(PORTB, prev_value ^ PINB5);
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>


<hr />

<p>Now that we are using Cargo, it would be nice if we didn&rsquo;t have to
directly call <code>avr-gcc</code> ourselves. We can accomplish this with a
<em>target file</em>. This is JSON configuration that can enhance the Rust
compiler&rsquo;s knowledge about how to compile a piece of code.</p>

<p>There are many fields that are required (check the <a href="https://github.com/shepmaster/rust-arduino-blink-led-no-core-with-cargo">repository</a>
for the full reference), but the important one is that we can tell the
compiler to use <code>avr-gcc</code> as our linker:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>  "linker": "avr-gcc",
</span><span class='line'>  "pre-link-args": ["-mmcu=atmega328p", "-nostartfiles", "../interrupt_vector.S"],
</span><span class='line'>  "exe-suffix": ".elf",
</span><span class='line'>  "post-link-args": ["-Wl,--no-gc-sections"],</span></code></pre></td></tr></table></div></figure>


<p>And we can use this JSON target file when compiling:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>cargo build --release --target=./arduino.json</span></code></pre></td></tr></table></div></figure>


<p>This will create our ELF file, automatically linking to our interrupt
vector definition, and ready to be processed with <code>avr-objcopy</code> and
uploaded to the board. We are getting closer and closer to an
enjoyable development experience!</p>

<p>As before, the <a href="https://github.com/shepmaster/rust-arduino-blink-led-no-core-with-cargo">complete source</a> is available on Github.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Rust on an Arduino Uno, Part 5]]></title>
    <link href="http://jakegoulding.com/blog/2016/05/19/rust-on-an-arduino-uno-part-5/"/>
    <updated>2016-05-19T13:57:04-04:00</updated>
    <id>http://jakegoulding.com/blog/2016/05/19/rust-on-an-arduino-uno-part-5</id>
    <content type="html"><![CDATA[<p>Previously, we wrote some code that allowed us to
<a href="http://jakegoulding.com/blog/2016/01/24/rust-on-an-arduino-uno-part-3/">sleep by waiting for a number of cycles to pass</a>. However, we
had to peek at the disassembly to know how many cycles we were
spending and adapt our source code to match. While it got us started,
it&rsquo;s not a very elegant solution.</p>

<!-- more -->


<p>The Arduino Uno uses an ATmega328P processor. One of the features of
this processor are 3 built-in timers that can trigger <em>interrupts</em> at
certain periods. Interrupts are special bits of code that take over
control of the processor when something important happens. These are
often time-critical things that need to be handled quickly.</p>

<p>What would be ideal is if we could rely on the timer feature to
implement our <code>sleep</code> method. To get started, we are going to need the
ability to specify the <em>interrupt vector</em>.</p>

<p>The interrupt vector is a table of 26 instructions that must be placed
at a specific section in memory. Each element in the table corresponds
to a specific interrupt, and should consist of one instruction that
jumps to the appropriate interrupt handler.</p>

<p>To do this, we need to write a little bit of assembly:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>ivr:
</span><span class='line'>        jmp _ivr_reset
</span><span class='line'>        jmp _ivr_irq0
</span><span class='line'>        jmp _ivr_irq1
</span><span class='line'>        ;; continues for all the rest</span></code></pre></td></tr></table></div></figure>


<p>In order to use this, we need to include it when linking all of our
code together. We also have to disable the existing interrupt vector
that would be added. This is done via the <code>-nostartfiles</code> flag:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>avr-gcc -mmcu=atmega328p interrupt_vector.S hello.o -nostartfiles -o hello.elf</span></code></pre></td></tr></table></div></figure>


<p>If you compile right now, you will get a whole bunch of errors of the
form:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>temp_file_name.o: In function `ivr':
</span><span class='line'>(.text+0x0): undefined reference to `_ivr_reset'</span></code></pre></td></tr></table></div></figure>


<p>Our interrupt vector is trying to jump to a bunch of symbols that we
haven&rsquo;t yet defined. We could do the simple thing and define a bunch
of <code>_ivr_*</code> methods in Rust (and I did, to start with), but that&rsquo;s
rather annoying. Instead, we can use <em>weak linking</em> to define a kind
of &ldquo;fallback&rdquo; symbol. We will have one simple handler that just
returns from the interrupt, and set each handler to use that unless it
is defined:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>_ivr_undefined:
</span><span class='line'>        reti
</span><span class='line'>
</span><span class='line'>.weak _ivr_irq0
</span><span class='line'>.set  _ivr_irq0, _ivr_undefined
</span><span class='line'>;;; And so on</span></code></pre></td></tr></table></div></figure>


<p>The only outlier is <code>_ivr_reset</code> which we define to point to our
<code>main</code> method, avoiding extraneous indirection. At this point, we
should be compiling again, but not using the interrupts yet. Let&rsquo;s
change that.</p>

<p>Following <a href="http://www.instructables.com/id/Arduino-Timer-Interrupts/?ALLSTEPS">this guide</a>, we can see all the details of
setting up the timer. At a high level it&rsquo;s:</p>

<ol>
<li>Register an interrupt handler.</li>
<li>Disable interrupts.</li>
<li>Set a bunch of values as determined by the datasheet and math.</li>
<li>Enable interrupts.</li>
</ol>


<p>We will copy all of the values and registers from this article to
setup timer 0, but with a 1kHz rate instead of 2kHz. This matches
nicer with our <code>sleep_ms</code> method which waits milliseconds.</p>

<p>Let&rsquo;s use a little bit of nice Rust for a change. When we disable
interrupts, we <em>really</em> want to make sure we enable them again! In a
language like Rust, we can use a (misleadingly labeled) pattern known
as Resource Acquisition Is Initialization (RAII). We will create a
<code>struct</code> that disables interrupts when it is created and enables them
when the struct is dropped. This means we can never forget to
re-enable interrupts as the compiler will ensure things are restored!</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>struct DisableInterrupts(PhantomData&lt;()&gt;);
</span><span class='line'>impl DisableInterrupts {
</span><span class='line'>    fn new() -&gt; DisableInterrupts {
</span><span class='line'>        unsafe { asm!("CLI") }
</span><span class='line'>        DisableInterrupts(PhantomData)
</span><span class='line'>    }
</span><span class='line'>}
</span><span class='line'>
</span><span class='line'>impl Drop for DisableInterrupts {
</span><span class='line'>    fn drop(&mut self) {
</span><span class='line'>        unsafe { asm!("SEI") }
</span><span class='line'>    }
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>


<p>We can bundle this into a nice wrapper:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>fn without_interrupts&lt;F, T&gt;(f: F) -&gt; T
</span><span class='line'>    where F: FnOnce() -&gt; T
</span><span class='line'>{
</span><span class='line'>    let _disabled = DisableInterrupts::new();
</span><span class='line'>    f()
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>


<p>And use it like so:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>without_interrupts(|| {
</span><span class='line'>    volatile_store(TCCR0A, 0);
</span><span class='line'>});</span></code></pre></td></tr></table></div></figure>


<p>To define the interrupt handler, we simply create a method that
matches the expected name from our assembly file. The method simply
increments a global variable each time it is triggered:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>static mut N_MS_ELAPSED: u8 = 0;
</span><span class='line'>
</span><span class='line'>#[no_mangle]
</span><span class='line'>pub unsafe extern fn _ivr_timer0_compare_a() {
</span><span class='line'>    N_MS_ELAPSED += 1;
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>


<p>And re-implement our <code>sleep_ms</code> function to:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>fn sleep_ms(duration_ms: u8) {
</span><span class='line'>    unsafe {
</span><span class='line'>        volatile_store(&mut N_MS_ELAPSED, 0);
</span><span class='line'>        while volatile_load(&mut N_MS_ELAPSED) &lt; duration_ms {
</span><span class='line'>            // spin
</span><span class='line'>        }
</span><span class='line'>    }
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>


<p>Compile this and load it onto the board, and we are greeted with the
sight of <em>nothing blinking</em>. It&rsquo;s time to dig into more
disassembly. Here&rsquo;s what <code>_ivr_timer0_compare_a</code> looks like:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>lds     r24, 0x0000
</span><span class='line'>inc     r24
</span><span class='line'>sts     0x0000, r24
</span><span class='line'>ret</span></code></pre></td></tr></table></div></figure>


<p>Checking the instruction set manual and the datasheet, we will notice a few problems:</p>

<ol>
<li>We use <code>ret</code> (Return from Subroutine) instead of <code>reti</code> (Return from Interrupt).</li>
<li>We do not save and restore the Status register.</li>
<li>We do not save and restore the <code>r24</code> register.</li>
</ol>


<p>Let&rsquo;s modify our handler with a bit more assembly to address all three issues:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>#[no_mangle]
</span><span class='line'>pub unsafe extern fn _ivr_timer0_compare_a() {
</span><span class='line'>    asm!{
</span><span class='line'>        "PUSH R24
</span><span class='line'>         IN R24, 0x3F
</span><span class='line'>         PUSH R24"
</span><span class='line'>    };
</span><span class='line'>
</span><span class='line'>    N_MS_ELAPSED += 1;
</span><span class='line'>
</span><span class='line'>    asm!{
</span><span class='line'>        "POP R24
</span><span class='line'>         OUT 0x3F, R24
</span><span class='line'>         POP R24
</span><span class='line'>         RETI"
</span><span class='line'>    };
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>


<p>That&rsquo;s certainly a bit longer, but it compiles and works again! And it
will continue to work, so long as the compiler always decides to use
<code>r24</code> for the incremented value, something we have no control over. As
you might guess, there&rsquo;s a better solution.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Rust on an Arduino Uno, Part 4]]></title>
    <link href="http://jakegoulding.com/blog/2016/05/12/rust-on-an-arduino-uno-part-4/"/>
    <updated>2016-05-12T13:04:43-04:00</updated>
    <id>http://jakegoulding.com/blog/2016/05/12/rust-on-an-arduino-uno-part-4</id>
    <content type="html"><![CDATA[<p>When we left off, we were <a href="http://jakegoulding.com/blog/2016/01/24/rust-on-an-arduino-uno-part-3/">blinking the LED</a>. Let&rsquo;s take a
brief detour and document how to get a working Rust compiler. This is
mostly a way for me to document what I&rsquo;ve been doing so I can find it
again!</p>

<!-- more -->


<p>We are going to start by getting a local version of LLVM that supports
targeting AVR. After cloning <a href="https://github.com/avr-llvm/llvm">the repository</a>, we will need
to set up for a build. Note that the upstream <code>avr-rust-support</code>
branch sometimes lags compared to <code>avr-support</code>, so you will probably
want to merge the two branches to get any updates.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>cd avr-llvm
</span><span class='line'>git checkout avr-support
</span><span class='line'>git merge origin/avr-rust-support
</span><span class='line'>mkdir -p debug/build
</span><span class='line'>cd debug/build</span></code></pre></td></tr></table></div></figure>


<p>We will then configure LLVM. This <em>particular</em> configuration I have
here is based off the current Rust build and is specific to OS X (see
the <code>C_FLAGS</code> and <code>CXX_FLAGS</code>). If you are using a different platform,
you&rsquo;ll need to poke at the Rust build process to see the appropriate
flags.</p>

<p>Last updated: <strong>2016-11-06</strong></p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>cmake ../.. \
</span><span class='line'>  -DCMAKE_BUILD_TYPE=Debug \
</span><span class='line'>  -DLLVM_TARGETS_TO_BUILD="X86;AVR" \
</span><span class='line'>  -DLLVM_INCLUDE_EXAMPLES=OFF \
</span><span class='line'>  -DLLVM_INCLUDE_TESTS=OFF \
</span><span class='line'>  -DLLVM_INCLUDE_DOCS=OFF \
</span><span class='line'>  -DLLVM_ENABLE_ZLIB=OFF \
</span><span class='line'>  -DWITH_POLLY=OFF \
</span><span class='line'>  -DLLVM_ENABLE_TERMINFO=OFF \
</span><span class='line'>  -DLLVM_INSTALL_UTILS=ON \
</span><span class='line'>  -DCMAKE_C_FLAGS="-ffunction-sections -fdata-sections -m64 -fPIC -stdlib=libc++" \
</span><span class='line'>  -DCMAKE_CXX_FLAGS="-ffunction-sections -fdata-sections -m64 -fPIC -stdlib=libc++" \
</span><span class='line'>  -DCMAKE_INSTALL_PREFIX=..</span></code></pre></td></tr></table></div></figure>


<p>Then it&rsquo;s just a matter of building and installing. Since it created
normal <code>Makefile</code>s for me, I passed an extra make flag to build in
parallel. The LLVM build is pretty fast this way!</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>cmake --build . -- -j7
</span><span class='line'>cmake --build . --target install</span></code></pre></td></tr></table></div></figure>


<p>Then we need to build Rust with this custom LLVM. After cloning
<a href="https://github.com/avr-rust/rust">the repository</a>, set up the structure:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>cd avr-rust
</span><span class='line'>git checkout avr-support
</span><span class='line'>mkdir -p debug
</span><span class='line'>cd debug/</span></code></pre></td></tr></table></div></figure>


<p>AVR-LLVM is based on a very new version of LLVM, so we need to use the
in-progress Rust build system called &ldquo;rustbuild&rdquo;. Using in-development
build systems with in-development compilers, what could go wrong?</p>

<p>Note that it&rsquo;s very important to use an absolute path to your LLVM
directory.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>../configure \
</span><span class='line'>  --enable-rustbuild \
</span><span class='line'>  --enable-debug \
</span><span class='line'>  --disable-docs \
</span><span class='line'>  --enable-debug-assertions \
</span><span class='line'>  --disable-jemalloc \
</span><span class='line'>  --llvm-root=/absolute/path/to/avr-llvm/debug</span></code></pre></td></tr></table></div></figure>


<p>Then we build!</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>make -j7</span></code></pre></td></tr></table></div></figure>


<p><strong>4 or more hours later</strong>, you will have a fully-built
compiler. However, you can usually get up-and-running earlier by using
the stage 1 compiler, located in <code>debug/build/*/stage1</code>. This will be
available pretty quickly, before the entire build is complete.</p>

<p>We then add this build as a <a href="https://rustup.rs/">rustup</a> toolchain and use it as the
override compiler in a directory:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>rustup toolchain link avr /path/to/rust/debug/build/*/stage1
</span><span class='line'>rustup override set avr</span></code></pre></td></tr></table></div></figure>


<p>Note that this will only produce a cross-compiler; none of the
libraries that make things actually work. That&rsquo;s still coming!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Rust on an Arduino Uno, Part 3]]></title>
    <link href="http://jakegoulding.com/blog/2016/01/24/rust-on-an-arduino-uno-part-3/"/>
    <updated>2016-01-24T13:11:12-05:00</updated>
    <id>http://jakegoulding.com/blog/2016/01/24/rust-on-an-arduino-uno-part-3</id>
    <content type="html"><![CDATA[<p>Now that we can <a href="http://jakegoulding.com/blog/2016/01/17/rust-on-an-arduino-uno-part-2/">turn an LED on</a>, let&rsquo;s see if we can do
something more exciting: make the LED blink. Surprisingly, this is
more difficult than you might expect!</p>

<!-- more -->


<p>Blinking boils down to &ldquo;turn the light on, wait a while, turn the
light off, wait a while&rdquo; and repeat forever. We already know how to
turn the light on and off, as well as repeating forever. The trick
lies in &ldquo;wait a while&rdquo;.</p>

<p>In a conventional Rust application, we&rsquo;d probably call something like
<a href="http://doc.rust-lang.org/std/thread/fn.sleep.html"><code>std::thread::sleep</code></a>, but we don&rsquo;t have access to <code>libstd</code> on an
Arduino as that library is too high-level. We will have to implement
it ourselves!</p>

<p>It&rsquo;s easy enough, all we have to do is loop a bunch of times. If the
Arduino processor runs at 16MHz, we can waste 16000 cycles to take one
millisecond. We will execute a <code>nop</code> instruction to waste the time:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>fn sleep_ms(duration_ms: u16) {
</span><span class='line'>    const FREQUENCY_HZ: u32 = 16_000_000;
</span><span class='line'>    const CYCLES_PER_MS: u16 = (FREQUENCY_HZ / 1000) as u16;
</span><span class='line'>
</span><span class='line'>    for _ in 0..duration_ms {
</span><span class='line'>        for _ in 0..CYCLES_PER_MS {
</span><span class='line'>            unsafe { asm!("nop"); }
</span><span class='line'>        }
</span><span class='line'>    }
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>


<p>Just compile this and away we go! Or not&hellip;</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>error: failed to resolve. Maybe a missing `extern crate iter`? [E0433]
</span><span class='line'>     for _ in 0..duration_ms {
</span><span class='line'>     ^~~~
</span><span class='line'>
</span><span class='line'>error: unresolved name `iter::IntoIterator::into_iter` [E0425]</span></code></pre></td></tr></table></div></figure>


<p>Right, we haven&rsquo;t actually defined any of the <code>Iterator</code> logic; that&rsquo;s
in <code>libcore</code> which we don&rsquo;t have yet. Let&rsquo;s skip that and do something
a little more C-like. We can just loop and increment integers and
compare them:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>fn sleep_ms(duration_ms: u16) {
</span><span class='line'>    const FREQUENCY_HZ: u32 = 16_000_000;
</span><span class='line'>    const CYCLES_PER_MS: u16 = 16_000;
</span><span class='line'>
</span><span class='line'>    let mut outer = 0;
</span><span class='line'>    while outer &lt; duration_ms {
</span><span class='line'>        let mut inner = 0;
</span><span class='line'>        while inner &lt; CYCLES_PER_MS {
</span><span class='line'>            unsafe { asm!("nop"); }
</span><span class='line'>            inner += 1;
</span><span class='line'>        }
</span><span class='line'>        outer += 1;
</span><span class='line'>    }
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>


<p>And&hellip; that fails too:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>error: binary operation `/` cannot be applied to type `u32` [E0369]
</span><span class='line'>     const CYCLES_PER_MS: u16 = (FREQUENCY_HZ / 1000) as u16;
</span><span class='line'>                                 ^~~~~~~~~~~~</span></code></pre></td></tr></table></div></figure>


<p>Ok, no division, even if it is just a constant and should be computed
at compile time. Well, we can hard code it for the moment&hellip;</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>error: binary operation `&lt;` cannot be applied to type `_` [E0369]
</span><span class='line'>     while outer &lt; duration_ms {
</span><span class='line'>           ^~~~~
</span><span class='line'>
</span><span class='line'>error: binary assignment operation `+=` cannot be applied to type `u16` [E0368]
</span><span class='line'>         outer += 1;
</span><span class='line'>         ^~~~~</span></code></pre></td></tr></table></div></figure>


<p>OK, wow, no addition or comparison either. There&rsquo;s no way around
this &ndash; we really need <code>libcore</code> or else we are stuck with a pretty
primitive environment. Since we know we have issues compiling all of
libcore, let&rsquo;s try a smaller part, just enough to compile this
example.</p>

<p>Previously, we had copied in some small snippets from libcore, but
let&rsquo;s replace those excerpts with the complete files and drag in a few
more. After some trial-and-error, this small set compiles:</p>

<ul>
<li><code>clone</code></li>
<li><code>cmp</code></li>
<li><code>intrinsics</code></li>
<li><code>marker</code></li>
<li><code>ops</code></li>
<li><code>option</code></li>
</ul>


<p>With it compiling, let&rsquo;s actually call <code>sleep_ms</code> in our <code>main</code> and
load the program onto the board:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>loop {
</span><span class='line'>    sleep_ms(500);
</span><span class='line'>    volatile_store(PORTB, 0xFF); // Everything is on
</span><span class='line'>    sleep_ms(500);
</span><span class='line'>    volatile_store(PORTB, 0x00); // Everything is off
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>


<p><video src="http://jakegoulding.com/images/blog/arduino_led/blink.mp4" controls>
<a href="http://jakegoulding.com/images/blog/arduino_led/blink.mp4">
A video of the blinking LED.
</a>
</video></p>

<p>Look at that nice, steady blinking. Blinking at a rate that is
<em>nothing</em> like 500 milliseconds. Let&rsquo;s take a look at the disassembly
for the inner loop to understand why:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>adiw ;; Add word (16-bit)            ;; 2 cycles
</span><span class='line'>cp   ;; Compare registers            ;; 1 cycle
</span><span class='line'>cpc  ;; Compare registers with carry ;; 1 cycle
</span><span class='line'>brcs ;; Branch if carry set          ;; 1 cycle (false) / 2 cycles (true)</span></code></pre></td></tr></table></div></figure>


<p>We increment our counter and check to see if we&rsquo;ve exceeded our
limit. In all cases except the last iteration we will branch back to
the beginning of the loop, bringing the total cycle count of the loop
to six. Compare that to the naive calculation that the <code>nop</code> would
take one cycle and the rest of the loop would be free. Dividing the
inner loop constant by six gets us much closer to the appropriate
duration.</p>

<p>The outer loop and the function call itself also have some overhead,
but these only add up to a few cycles per inner loop. Since the inner
loop corresponds to many thousands of cycles, a few cycles is a small
error and I think can be safely ignored.</p>

<p>An interesting aside is that I have no idea why the <code>nop</code> does not
occur inside the loop. The compiler has reordered the code such that
the <code>nop</code> occurs in the variable initialization of the function. You
can change the code to just <code>asm!("")</code> and accomplish the same goal of
preventing the loop from being optimized away.</p>

<p>Next time, we will see if we can do something a little more structured
than counting cycles to sleep. As before, check out
<a href="https://github.com/shepmaster/rust-arduino-blink-led-no-core/tree/part3">the repository</a> for the code up to this point.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Rust on an Arduino Uno, Part 2]]></title>
    <link href="http://jakegoulding.com/blog/2016/01/17/rust-on-an-arduino-uno-part-2/"/>
    <updated>2016-01-17T14:34:54-05:00</updated>
    <id>http://jakegoulding.com/blog/2016/01/17/rust-on-an-arduino-uno-part-2</id>
    <content type="html"><![CDATA[<p>After my <a href="http://jakegoulding.com/blog/2016/01/02/rust-on-an-arduino-uno/">previous attempt</a>, I started to think that the
issues were caused by an inability to completely link the program. If
that were the case, could we try to link in a different way?</p>

<p>Through a bit of trial and error, I was able to generate an object
file:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>rustc --target avr-atmel-none hello.rs --emit obj</span></code></pre></td></tr></table></div></figure>




<!-- more -->


<p>Checking the disassembly of this file with <code>objdump -d hello.o</code> showed
promise:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>00000000 &lt;main&gt;:
</span><span class='line'>   0:   0e 94 00 00     call    0     ; 0x0 &lt;main&gt;
</span><span class='line'>   4:   08 95           ret
</span><span class='line'>
</span><span class='line'>Disassembly of section .text._ZN4main10__rust_abiE:
</span><span class='line'>
</span><span class='line'>00000000 &lt;_ZN4main10__rust_abiE&gt;:
</span><span class='line'>   0:   8f ef           ldi r24, 0xFF ; 255
</span><span class='line'>   2:   84 b9           out 0x04, r24 ; 4
</span><span class='line'>   4:   00 c0           rjmp    .+0   ; 0x6 &lt;LBB1_1&gt;
</span><span class='line'>
</span><span class='line'>00000006 &lt;LBB1_1&gt;:
</span><span class='line'>   6:   8f ef           ldi r24, 0xFF ; 255
</span><span class='line'>   8:   85 b9           out 0x05, r24 ; 5
</span><span class='line'>   a:   80 e0           ldi r24, 0x00 ; 0
</span><span class='line'>   c:   85 b9           out 0x05, r24 ; 5
</span><span class='line'>   e:   fb cf           rjmp    .-10  ; 0x6 &lt;LBB1_1&gt;</span></code></pre></td></tr></table></div></figure>


<p>I then used an existing installation of GCC with AVR support to finish
linking the code together:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>avr-gcc -mmcu=atmega328p hello.o -o hello.elf</span></code></pre></td></tr></table></div></figure>


<p>Taking a look at the disassembly of this code shows a lot of things
that were not present in the original object file:</p>

<ol>
<li>The interrupt vector table is established. This occupies about the
first 25 instructions. Each instruction is a jump to the
appropriate interrupt handler. Most importantly, table index 0 is
the reset &ldquo;interrupt&rdquo; which controls where the processor should
jump to when it is initialized.</li>
<li>The EEPROM Control Register and GPIOR0 are initialized and external
interrupts are disabled. Then <code>main</code> is called.</li>
<li>After <code>main</code> returns, interrupts are disabled and the chip goes
into an infinite loop.</li>
</ol>


<h3>Getting code on board</h3>

<p>Now that we have a compiled binary, we need to get it onto the Arduino
proper. <a href="http://www.nongnu.org/avrdude/">avrdude</a> is an in-system programmer that will allow us to
upload the compiled code, but it prefers input in a different format:
Intel HEX. We can convert using <code>avr-objcopy</code>:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>avr-objcopy -O ihex -R .eeprom hello.elf hello.hex</span></code></pre></td></tr></table></div></figure>


<p>Now we can upload to the Arduino:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>avrdude -p atmega328p -c arduino -P /dev/cu.usbmodem1411 -U flash:w:hello.hex:i</span></code></pre></td></tr></table></div></figure>


<p>The Arduino Uno has a second Atmel chip (ATmega16U2) that looks like a
USB-to-serial device to the host computer. On my OS X computer, that
device shows up at <code>/dev/cu.usbmodem1411</code>. Your location will differ.</p>

<h3>It&rsquo;s alive!</h3>

<p>Because I have such a basic level of code, I can&rsquo;t do anything nice
like blink an LED. Instead, I can write a tight loop that just turns
the LED on or off some percentage of the time. This allows it to have
a different relative brightness, which in turn lets me see that the
code changes are actually happening.</p>

<p>Check out the LED marked <code>L</code> in the following pictures.</p>

<h4>LED on 100% of the time</h4>

<p><img src="http://jakegoulding.com/images/blog/arduino_led/100.jpg" alt="LED at 100%" /></p>

<h4>LED on 50% of the time</h4>

<p><img src="http://jakegoulding.com/images/blog/arduino_led/050.jpg" alt="LED at 50%" /></p>

<h4>LED on 1% of the time</h4>

<p><img src="http://jakegoulding.com/images/blog/arduino_led/001.jpg" alt="LED at 1%" /></p>

<h3>What&rsquo;s next?</h3>

<p>This isn&rsquo;t the most <em>elegant</em> of solutions, and there are a lot of
avenues to explore:</p>

<ol>
<li><p><strong>Avoid installing <code>avr-gcc</code> and <code>avr-objcopy</code></strong>. Right now,
<code>avr-gcc</code> is used when compiling Rust itself (for <code>compiler-rt</code>)
and to finish assembly of the executable. It would be ideal if all
of this could be handled within an AVR-enabled Rust or LLVM.</p></li>
<li><p><strong>Set interrupt handlers</strong>. I think the typical solution is
to use a linker script, but that&rsquo;s one more moving piece I&rsquo;d like
to avoid adding.</p></li>
<li><p><strong>Compile <code>libcore</code></strong>! In order to get the most basic of things
to compile, I had to straight-up copy code from <code>libcore</code>. An
impressive amount of things are included there. Things you might
want to use, like <em>addition</em>, not to mention <code>Option</code> or anything
having to do with iterators. <code>libstd</code> is unlikely to ever be
supported as it relies on memory allocation.</p></li>
<li><p><strong>Merge the Rust fork of LLVM with the AVR fork of LLVM</strong>. The more
frequently these are merged, the easier it will be to eventually
include the AVR support in Rust proper. I started to do this, but
had a large number of merge conflicts so I backed off.</p></li>
<li><p><strong>Compile AVR-enabled Rust in non-debug mode</strong>. For some reason,
when I compile Rust without debugging symbols, I get an &ldquo;exciting&rdquo;
assertion failure from deep within LLVM. That is most likely a
symptom of some problem that should be fixed.</p></li>
</ol>


<h3>TL;DR</h3>

<p>Check out <a href="https://github.com/shepmaster/rust-arduino-blink-led-no-core/tree/part2">my repo</a> for an example that worked for me. In short:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class=''><span class='line'># Compile object
</span><span class='line'>rustc --target avr-atmel-none -C target-cpu=atmega328p --emit=obj hello.rs -o hello.o
</span><span class='line'># Link together
</span><span class='line'>avr-gcc -mmcu=atmega328p hello.o -o hello.elf
</span><span class='line'># Reformat for upload
</span><span class='line'>avr-objcopy -O ihex -R .eeprom hello.elf hello.hex
</span><span class='line'># Upload to the board
</span><span class='line'>avrdude -p atmega328p -c arduino -P /dev/cu.usbmodem1411 -U flash:w:hello.hex:i</span></code></pre></td></tr></table></div></figure>


<p>If you are on OS X, you can install the things you need (except an
AVR-enabled Rust build) with <a href="http://brew.sh/">homebrew</a>:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>brew tap osx-cross/avr
</span><span class='line'>brew install avr-libc
</span><span class='line'>brew install avrdude</span></code></pre></td></tr></table></div></figure>


<p>Continue on to <a href="http://jakegoulding.com/blog/2016/01/24/rust-on-an-arduino-uno-part-3/">part 3</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Rust on an Arduino Uno]]></title>
    <link href="http://jakegoulding.com/blog/2016/01/02/rust-on-an-arduino-uno/"/>
    <updated>2016-01-02T15:26:54-05:00</updated>
    <id>http://jakegoulding.com/blog/2016/01/02/rust-on-an-arduino-uno</id>
    <content type="html"><![CDATA[<p>We have an <a href="https://www.arduino.cc/en/Main/ArduinoBoardUno">Arduino Uno</a> that&rsquo;s been sitting around gathering
dust for a little while, so I decided to see how <a href="https://www.rust-lang.org/">Rust</a> worked
on it.</p>

<!-- more -->


<p>A bit of searching led to a
<a href="https://github.com/avr-rust/rust/">fork of Rust with AVR support, AVR-Rust</a>. This is built on
top of a <a href="https://github.com/avr-llvm/llvm/">fork of LLVM with AVR support, AVR-LLVM</a>. Both of
these projects are led by <a href="https://github.com/dylanmckay">Dylan McKay</a>.</p>

<p>The current documentation for AVR-Rust is a bit lacking, and it was
forked from a development version of Rust 1.4. The current development
version is Rust 1.7, making the fork about 4.5 months old. However,
the changes to LLVM are in the process of being merged into upstream,
laying the groundwork for merging the changes into Rust as well.</p>

<p>Let&rsquo;s start out by doing the bare minimum and try to get a version of
<code>rustc</code> that can target the AVR chip:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>git clone https://github.com/avr-rust/rust.git
</span><span class='line'>mkdir build && cd build
</span><span class='line'>../rust/configure
</span><span class='line'>make</span></code></pre></td></tr></table></div></figure>


<p>You&rsquo;ll note that there&rsquo;s nothing AVR specific here. Every Rust
compiler is actually a <em>cross-compiler</em>, a compiler that executes on
one architecture but produces code for another architecture. Because
this fork of Rust has support files for AVR, it will be able to
produce the correct executable code.</p>

<p>Unfortunately, I <a href="https://github.com/avr-rust/rust/issues/13">couldn&rsquo;t get a basic file to compile</a>
out of the box.</p>

<p>So I did what any sane person would do &ndash; I started changing code
without knowing exactly what the failure was or what the code I was
changing did.</p>

<p>First I tried updating the branch of LLVM that AVR-Rust uses. There
are two branches in the repository &ndash; <a href="https://github.com/avr-llvm/llvm/tree/avr-support"><code>avr-support</code></a> is more
actively updated and <a href="https://github.com/avr-llvm/llvm/tree/avr-rust-support"><code>avr-rust-support</code></a> lags behind.</p>

<p>Merging <code>avr-support</code> into <code>avr-rust-support</code> went smoothly, but the
Rust LLVM driver code needed to be updated to handle the newer LLVM
version. I grabbed the diff from the main Rust repository and applied
that. This seemed to work, but then I got a segfault from the stage 1
Rust compiler, deep in the internals of LLVM.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>make: *** [x86_64-apple-darwin/stage1/lib/rustlib/x86_64-apple-darwin/lib/stamp.term] Segmentation fault: 11</span></code></pre></td></tr></table></div></figure>


<p>So I continued changing more stuff!</p>

<p>I merged current Rust into the AVR fork of Rust and resolved the merge
conflicts as best I could figure out. After fixing a few new errors
and some poor merge conflicts, I was on my way. Until I hit the
segfault again.</p>

<p>That means it&rsquo;s actually time to try to figure out where the segfault
was coming from. I configured another build with some debug information:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>./configure --enable-debug --disable-docs --enable-llvm-assertions --enable-debug-assertions</span></code></pre></td></tr></table></div></figure>


<p>And built. This takes a long time, as nothing gets optimized. And then
it turns out that doing this also hides the segfault. Ugh.</p>

<p>However, I do get to a new error:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>ld: unknown option: --as-needed
</span><span class='line'>clang: error: linker command failed with exit code 1 (use -v to see invocation)</span></code></pre></td></tr></table></div></figure>


<p>Fortunately, I know where to tweak that in the source. The downside is
I&rsquo;ll need to wait for another long build cycle&hellip;</p>

<p>Continue on to <a href="http://jakegoulding.com/blog/2016/01/17/rust-on-an-arduino-uno-part-2/">part 2</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Running dnsmasq on OS X and routing to virtual machines]]></title>
    <link href="http://jakegoulding.com/blog/2014/04/26/running-dnsmasq-on-os-x-and-routing-to-virtual-machines/"/>
    <updated>2014-04-26T17:28:27-04:00</updated>
    <id>http://jakegoulding.com/blog/2014/04/26/running-dnsmasq-on-os-x-and-routing-to-virtual-machines</id>
    <content type="html"><![CDATA[<p>At work, I needed to run a <a href="https://www.docker.io/">Docker</a> container with a Rails
application that talked to another application running inside a
<a href="http://www.vmware.com/">VMWare</a> virtual machine. Adding to the complexity, I use
<a href="https://github.com/boot2docker/boot2docker">boot2docker</a>, which runs inside of <a href="https://www.virtualbox.org/">VirtualBox</a>.</p>

<p>If I only needed to access <code>rails.localdomain.dev</code> or
<code>api.localdomain.dev</code> from my Mac, I could have simply edited
<code>/etc/hosts</code> and set both domains to resolve to <code>127.0.0.1</code> and been
done with it. Unfortunately, Rails needed to be able to directly
resolve the API server.</p>

<!-- more -->


<h2>Setting up dnsmasq</h2>

<p><strong>NOTE</strong>: It&rsquo;s possible that editing <code>/etc/hosts</code> would have been
enough and I didn&rsquo;t need to set up dnsmasq at all. Read the section
about configuring the virtual machine&rsquo;s DNS first.</p>

<p>I followed <a href="http://blog.molawson.com/replace-pow-on-mavericks-10-9-with-nginx-dnsmasq-and-foreman">this tutorial</a> to install and configure
<a href="http://www.thekelleys.org.uk/dnsmasq/doc.html">dnsmasq</a>. You can ignore the parts about nginx and foreman.</p>

<p>Our Rails application must run at a domain like
<code>rails.localdomain.dev</code>, and the API server runs at
<code>api.localdomain.dev</code>, so I configured dnsmasq to manage the
<code>.localdomain.dev</code> domain.</p>

<p>I moved the hard-coded DNS entry for <code>api.localdomain.dev</code> from
<code>/etc/hosts</code> to <code>dnsmasq.conf</code>. I found this IP by logging into the
API VM and looking at the output of <code>ifconfig</code>. I&rsquo;m not certain why
this IP will not change, but that&rsquo;s what I was told.</p>

<h2>Creating a routable &ldquo;loopback address&rdquo;</h2>

<p>Originally, I had configured <code>api.localdomain.dev</code> to resolve to
<code>127.0.0.1</code>. This works fine when accessed from the Mac, but when a
virtual machine resolved that domain, <code>127.0.0.1</code> would refer to the
virtual machine itself! I needed an IP address that:</p>

<ol>
<li>Would refer to my laptop.</li>
<li>Wouldn&rsquo;t change when I changed network configuration.</li>
<li>Wouldn&rsquo;t resolve to the VM inside the VM.</li>
</ol>


<p>We can accomplish this by using an ifconfig <code>alias</code>:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>sudo ifconfig lo0 alias 10.254.254.254</span></code></pre></td></tr></table></div></figure>


<p>I picked <code>10.254.254.254</code> because it is a <a href="http://en.wikipedia.org/wiki/Private_network">private network</a>
address and it is unlikely to be used on any networks I connect to. If
I ever do have a conflict, there are many other private addresses to
choose from!</p>

<p>I edited <code>dnsmasq.conf</code> and replaced <code>127.0.0.1</code> with
<code>10.254.254.254</code>. Requests for <code>*.localdomain.dev</code> will now resolve to
an IP address that will always refer to the Mac, but that the virtual
machines will not think resolves to the virtual machine itself.</p>

<p>Big thanks to <a href="https://twitter.com/bobthebotmaker">Andre</a> for helping me find and understand how
aliasing works!</p>

<h2>Configuring virtual machine DNS</h2>

<p>Next I configured the virtual machine to route all DNS requests
through the Mac&rsquo;s resolving system. For VirtualBox, configure it
according to this <a href="http://serverfault.com/a/453260/119136">serverfault answer</a>. If you are using
Vagrant, you can add a stanza like:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>config.vm.provider "virtualbox" do |vb|
</span><span class='line'>  # Always go through OS X resolver, allowing us to redirect local domains.
</span><span class='line'>  vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
</span><span class='line'>  vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
</span><span class='line'>end</span></code></pre></td></tr></table></div></figure>


<p>I&rsquo;m not sure, but it&rsquo;s possible that these settings would use entries
configured in my Mac&rsquo;s <code>/etc/hosts</code>. This could make it so that the
dnsmasq step isn&rsquo;t required.</p>

<p>Instead of resolving through the host, I could have edited
<code>/etc/resolv.conf</code> and used <code>10.254.254.254</code> as my DNS server
instead. If you do this, you definitely need to run dnsmasq.</p>

<p>Once the virtual machine could ping <code>api.localdomain.dev</code>, I restarted
the Docker daemon to pick up the networking changes. Dropping into a
Docker container, I was able to ping the API server as well. Success!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[A final dip into Ruby's Marshal format]]></title>
    <link href="http://jakegoulding.com/blog/2013/01/20/a-final-dip-into-rubys-marshal-format/"/>
    <updated>2013-01-20T20:30:00-05:00</updated>
    <id>http://jakegoulding.com/blog/2013/01/20/a-final-dip-into-rubys-marshal-format</id>
    <content type="html"><![CDATA[<p>This is the third and last of my posts about the Marshal format. The
<a href="http://jakegoulding.com/blog/2013/01/15/a-little-dip-into-rubys-marshal-format/">first part</a> introduced the format and some straight-forward
serializations. The <a href="http://jakegoulding.com/blog/2013/01/16/another-dip-into-rubys-marshal-format/">second part</a> touched on strings and
object links. This post rounds us off with regexes, classes, modules,
and instances of objects.</p>

<!-- more -->


<h2>Regexes</h2>

<p><code>/hello/</code></p>

<pre>0408 49<span style='color: red'>2</span><span style='color: red'>f</span> <span style='color: #8FF'>0</span><span style='color: #8FF'>a</span><span style='color: #CC0'>6</span><span style='color: #CC0'>8</span> <span style='color: #CC0'>6</span><span style='color: #CC0'>5</span><span style='color: #CC0'>6</span><span style='color: #CC0'>c</span> <span style='color: #CC0'>6</span><span style='color: #CC0'>c</span><span style='color: #CC0'>6</span><span style='color: #CC0'>f</span> <span style='color: #0C0'>0</span><span style='color: #0C0'>0</span>06 3a06 4546</pre>


<p>Like strings, regexes are surrounded by an IVAR. The typecode <code>2f</code> is
ASCII <code>/</code> and denotes that this object is a regex. The length of the
string follows, again encoded as an integer. The regex string is
stored as a set of bytes, and must be interpreted with the string
encoding from the IVAR. After the string, the regex options are saved.</p>

<p><code>/hello/imx</code></p>

<pre>0408 492f 0a68 656c 6c6f <span style='color: #0C0'>0</span><span style='color: #0C0'>7</span>06 3a06 4546</pre>


<p>The regex option byte is a bitset of the five possible options. In
this example, ignore case, extend, and multiline are set (<code>0x1</code>,
<code>0x2</code>, and <code>0x4</code> respectively)</p>

<h2>Classes</h2>

<p><code>String</code></p>

<pre>0408 <span style='color: red'>6</span><span style='color: red'>3</span><span style='color: #8FF'>0</span><span style='color: #8FF'>b</span> <span style='color: #CC0'>5</span><span style='color: #CC0'>3</span><span style='color: #CC0'>7</span><span style='color: #CC0'>4</span> <span style='color: #CC0'>7</span><span style='color: #CC0'>2</span><span style='color: #CC0'>6</span><span style='color: #CC0'>9</span> <span style='color: #CC0'>6</span><span style='color: #CC0'>e</span><span style='color: #CC0'>6</span><span style='color: #CC0'>7</span></pre>


<p>The typecode <code>63</code> is ASCII <code>c</code> and denotes that this object is a
class. The length of the class name followed by the class name are
next.</p>

<p><code>Math::DomainError</code></p>

<pre>0408 63<span style='color: #8FF'>1</span><span style='color: #8FF'>6</span> <span style='color: #CC0'>4</span><span style='color: #CC0'>d</span><span style='color: #CC0'>6</span><span style='color: #CC0'>1</span> <span style='color: #CC0'>7</span><span style='color: #CC0'>4</span><span style='color: #CC0'>6</span><span style='color: #CC0'>8</span> <span style='color: #CC0'>3</span><span style='color: #CC0'>a</span><span style='color: #CC0'>3</span><span style='color: #CC0'>a</span> <span style='color: #CC0'>4</span><span style='color: #CC0'>4</span><span style='color: #CC0'>6</span><span style='color: #CC0'>f</span> <span style='color: #CC0'>6</span><span style='color: #CC0'>d</span><span style='color: #CC0'>6</span><span style='color: #CC0'>1</span> <span style='color: #CC0'>6</span><span style='color: #CC0'>9</span><span style='color: #CC0'>6</span><span style='color: #CC0'>e</span> <span style='color: #CC0'>4</span><span style='color: #CC0'>5</span><span style='color: #CC0'>7</span><span style='color: #CC0'>2</span> <span style='color: #CC0'>7</span><span style='color: #CC0'>2</span><span style='color: #CC0'>6</span><span style='color: #CC0'>f</span> <span style='color: #CC0'>7</span><span style='color: #CC0'>2</span></pre>


<p>Namespaces are separated by <code>::</code>.</p>

<h2>Modules</h2>

<p><code>Enumerable</code></p>

<pre>0408 <span style='color: red'>6</span><span style='color: red'>d</span><span style='color: #8FF'>0</span><span style='color: #8FF'>f</span> <span style='color: #CC0'>4</span><span style='color: #CC0'>5</span><span style='color: #CC0'>6</span><span style='color: #CC0'>e</span> <span style='color: #CC0'>7</span><span style='color: #CC0'>5</span><span style='color: #CC0'>6</span><span style='color: #CC0'>d</span> <span style='color: #CC0'>6</span><span style='color: #CC0'>5</span><span style='color: #CC0'>7</span><span style='color: #CC0'>2</span> <span style='color: #CC0'>6</span><span style='color: #CC0'>1</span><span style='color: #CC0'>6</span><span style='color: #CC0'>2</span> <span style='color: #CC0'>6</span><span style='color: #CC0'>c</span><span style='color: #CC0'>6</span><span style='color: #CC0'>5</span></pre>


<p>Modules are identical to classes, except the typecode <code>6d</code> is ASCII <code>m</code>.</p>

<h2>Instances of user objects</h2>

<p>Let&rsquo;s define a small class to test with.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">DumpTest</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
</span><span class='line'>    <span class="vi">@a</span> <span class="o">=</span> <span class="n">a</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p><code>DumpTest.new(nil)</code></p>

<pre>0408 <span style='color: red'>6</span><span style='color: red'>f</span><span style='color: #8FF'>3</span><span style='color: #8FF'>a</span> <span style='color: #8FF'>0</span><span style='color: #8FF'>d</span><span style='color: #8FF'>4</span><span style='color: #8FF'>4</span> <span style='color: #8FF'>7</span><span style='color: #8FF'>5</span><span style='color: #8FF'>6</span><span style='color: #8FF'>d</span> <span style='color: #8FF'>7</span><span style='color: #8FF'>0</span><span style='color: #8FF'>5</span><span style='color: #8FF'>4</span> <span style='color: #8FF'>6</span><span style='color: #8FF'>5</span><span style='color: #8FF'>7</span><span style='color: #8FF'>3</span> <span style='color: #8FF'>7</span><span style='color: #8FF'>4</span><span style='color: #CC0'>0</span><span style='color: #CC0'>6</span> <span style='color: #0C0'>3</span><span style='color: #0C0'>a</span><span style='color: #0C0'>0</span><span style='color: #0C0'>7</span> <span style='color: #0C0'>4</span><span style='color: #0C0'>0</span><span style='color: #0C0'>6</span><span style='color: #0C0'>1</span> <span style='color: #F80'>3</span><span style='color: #F80'>0</span></pre>


<p>The typecode <code>6f</code> is ASCII <code>o</code>, and denotes that this is an
object. The class name is next, written as a symbol &ndash; <code>:DumpTest</code>. The
number of instance variables is encoded as an integer, followed by
pairs of name, value. This example has 1 pair of instance variables,
[<code>:@a</code>, <code>nil</code>].</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Another dip into Ruby's Marshal format]]></title>
    <link href="http://jakegoulding.com/blog/2013/01/16/another-dip-into-rubys-marshal-format/"/>
    <updated>2013-01-16T20:00:00-05:00</updated>
    <id>http://jakegoulding.com/blog/2013/01/16/another-dip-into-rubys-marshal-format</id>
    <content type="html"><![CDATA[<p>In a <a href="http://jakegoulding.com/blog/2013/01/15/a-little-dip-into-rubys-marshal-format/">previous post</a> I started to describe some details of
Ruby&rsquo;s Marshal format. This post goes further: a larger set of
integers, IVARs, strings, and object links.</p>

<!-- more -->


<h2>Larger integers</h2>

<p>What happens once we go beyond integer values that can be represented
in one byte? Marshal simply writes the number of bytes needed to
represent the value, followed by the value, least significant byte
first. Leading zeroes are not encoded.</p>

<p><code>123</code></p>

<pre>0408 69<span style='color: #8FF'>0</span><span style='color: #8FF'>1</span> <span style='color: #CC0'>7</span><span style='color: #CC0'>b</span></pre>


<p><code>01</code> indicates that the value takes up one byte, followed by the value
itself.</p>

<p><code>256</code></p>

<pre>0408 69<span style='color: #8FF'>0</span><span style='color: #8FF'>2</span> <span style='color: #CC0'>0</span><span style='color: #CC0'>0</span><span style='color: #CC0'>0</span><span style='color: #CC0'>1</span></pre>


<p>256 requires two bytes.</p>

<p><code>2**30 - 1</code></p>

<pre>0408 69<span style='color: #8FF'>0</span><span style='color: #8FF'>4</span> <span style='color: #CC0'>f</span><span style='color: #CC0'>f</span><span style='color: #CC0'>f</span><span style='color: #CC0'>f</span> <span style='color: #CC0'>f</span><span style='color: #CC0'>f</span><span style='color: #CC0'>3</span><span style='color: #CC0'>f</span></pre>


<p>This is the largest value you can serialize as an integer. Above this,
Marshal starts serializing integers as a &ldquo;bignum&rdquo;.</p>

<h2>Negative integers</h2>

<p><code>-1</code></p>

<pre>0408 69<span style='color: #8FF'>f</span><span style='color: #8FF'>a</span></pre>


<p><code>fa</code> is -6 in two&rsquo;s complement, which mirrors how <code>1</code> is encoded as 6.</p>

<p><code>-124</code></p>

<pre>0408 69<span style='color: #8FF'>f</span><span style='color: #8FF'>f</span> <span style='color: #CC0'>8</span><span style='color: #CC0'>4</span></pre>


<p>Here the first byte is -1 in two&rsquo;s complement. This indicates that one
byte of value follows. The value has had leading <code>FF</code> bytes removed,
similar to large positive integers.</p>

<p><code>-257</code></p>

<pre>0408 69<span style='color: #8FF'>f</span><span style='color: #8FF'>e</span> <span style='color: #CC0'>f</span><span style='color: #CC0'>f</span><span style='color: #CC0'>f</span><span style='color: #CC0'>e</span></pre>


<p>-257 requires two bytes.</p>

<p><code>-(2**30)</code></p>

<pre>0408 69<span style='color: #8FF'>f</span><span style='color: #8FF'>c</span> <span style='color: #CC0'>0</span><span style='color: #CC0'>0</span><span style='color: #CC0'>0</span><span style='color: #CC0'>0</span> <span style='color: #CC0'>0</span><span style='color: #CC0'>0</span><span style='color: #CC0'>c</span><span style='color: #CC0'>0</span></pre>


<p>This is the largest negative value you can serialize as an integer
before becoming a bignum.</p>

<h2>IVARs</h2>

<p>Hang on to your seats, we&rsquo;re going to jump into strings. First,
however, we need to talk about IVARs. The crucial thing that IVARs
bring to the table is the handling of string encodings.</p>

<p><code>'hello'</code></p>

<pre>0408 <span style='color: red'>4</span><span style='color: red'>9</span>22 0a68 656c 6c6f <span style='color: #8FF'>0</span><span style='color: #8FF'>6</span><span style='color: #CC0'>3</span><span style='color: #CC0'>a</span> <span style='color: #CC0'>0</span><span style='color: #CC0'>6</span><span style='color: #CC0'>4</span><span style='color: #CC0'>5</span> <span style='color: #0C0'>5</span><span style='color: #0C0'>4</span></pre>


<p>The typecode <code>49</code> is ASCII <code>I</code> and denotes that this object contains
instance variables. After all the object data, the number of instance
variables is provided. The first instance variable is a special one &ndash;
it&rsquo;s the string encoding of the object. In this example the string
encoding is UTF-8, denoted by the symbol <code>:E</code> followed by a <code>true</code>.</p>

<p><code>'hello'.force_encoding('US-ASCII')</code></p>

<pre>0408 4922 0a68 656c 6c6f 06<span style='color: #CC0'>3</span><span style='color: #CC0'>a</span> <span style='color: #CC0'>0</span><span style='color: #CC0'>6</span><span style='color: #CC0'>4</span><span style='color: #CC0'>5</span> <span style='color: #0C0'>4</span><span style='color: #0C0'>6</span></pre>


<p>To represent US-ASCII, <code>:E</code> <code>false</code> is used instead. Both US-ASCII and
UTF-8 are common enough string encodings that special indicators were
created for them.</p>

<p><code>'hello'.force_encoding('SHIFT_JIS')</code></p>

<pre>0408 4922 0a68 656c 6c6f 06<span style='color: #CC0'>3</span><span style='color: #CC0'>a</span> <span style='color: #CC0'>0</span><span style='color: #CC0'>d</span><span style='color: #CC0'>6</span><span style='color: #CC0'>5</span> <span style='color: #CC0'>6</span><span style='color: #CC0'>e</span><span style='color: #CC0'>6</span><span style='color: #CC0'>3</span> <span style='color: #CC0'>6</span><span style='color: #CC0'>f</span><span style='color: #CC0'>6</span><span style='color: #CC0'>4</span> <span style='color: #CC0'>6</span><span style='color: #CC0'>9</span><span style='color: #CC0'>6</span><span style='color: #CC0'>e</span> <span style='color: #CC0'>6</span><span style='color: #CC0'>7</span><span style='color: #0C0'>2</span><span style='color: #0C0'>2</span> <span style='color: #0C0'>0</span><span style='color: #0C0'>e</span><span style='color: #0C0'>5</span><span style='color: #0C0'>3</span> <span style='color: #0C0'>6</span><span style='color: #0C0'>8</span><span style='color: #0C0'>6</span><span style='color: #0C0'>9</span> <span style='color: #0C0'>6</span><span style='color: #0C0'>6</span><span style='color: #0C0'>7</span><span style='color: #0C0'>4</span> <span style='color: #0C0'>5</span><span style='color: #0C0'>f</span><span style='color: #0C0'>4</span><span style='color: #0C0'>a</span> <span style='color: #0C0'>4</span><span style='color: #0C0'>9</span><span style='color: #0C0'>5</span><span style='color: #0C0'>3</span></pre>


<p>For any other string encoding, the symbol <code>:encoding</code> is used and the
full string encoding is written out as a raw string &ndash; <code>"SHIFT_JIS"</code>.</p>

<p><code>'hello'.tap {|s| s.instance_variable_set(:@test, nil)}</code></p>

<pre>0408 4922 0a68 656c 6c6f <span style='color: #8FF'>0</span><span style='color: #8FF'>7</span>3a 0645 54<span style='color: #CC0'>3</span><span style='color: #CC0'>a</span> <span style='color: #CC0'>0</span><span style='color: #CC0'>a</span><span style='color: #CC0'>4</span><span style='color: #CC0'>0</span> <span style='color: #CC0'>7</span><span style='color: #CC0'>4</span><span style='color: #CC0'>6</span><span style='color: #CC0'>5</span> <span style='color: #CC0'>7</span><span style='color: #CC0'>3</span><span style='color: #CC0'>7</span><span style='color: #CC0'>4</span> <span style='color: #0C0'>3</span><span style='color: #0C0'>0</span></pre>


<p>Additional instance variables follow the string encoding. There are
now 2 instance variables. The symbol for the instance variable name
<code>:@test</code> comes before the value, <code>nil</code>.</p>

<h2>Raw strings</h2>

<p><code>'hello'</code></p>

<pre>0408 49<span style='color: red'>2</span><span style='color: red'>2</span> <span style='color: #8FF'>0</span><span style='color: #8FF'>a</span><span style='color: #CC0'>6</span><span style='color: #CC0'>8</span> <span style='color: #CC0'>6</span><span style='color: #CC0'>5</span><span style='color: #CC0'>6</span><span style='color: #CC0'>c</span> <span style='color: #CC0'>6</span><span style='color: #CC0'>c</span><span style='color: #CC0'>6</span><span style='color: #CC0'>f</span> 063a 0645 54</pre>


<p>Raw strings are safely nestled inside an IVAR, and are comparatively
very simple. The typecode <code>22</code> is ASCII <code>"</code> and denotes that this
object is a raw string. The length of the string data is next, encoded in
the same form as integers. The string data follows as a set of
bytes. These bytes must be interpreted using the encoding from the
surrounding IVAR.</p>

<h2>Object links</h2>

<p>When the same object instance is repeated multiple times, the Marshal
encoding allows subsequent instances to reference the first instance
to save space in the stream.</p>

<p><code>a = 'hello'; [a, a]</code></p>

<pre>0408 5b07 <span style='color: #8FF'>4</span><span style='color: #8FF'>9</span><span style='color: #8FF'>2</span><span style='color: #8FF'>2</span> <span style='color: #8FF'>0</span><span style='color: #8FF'>a</span><span style='color: #8FF'>6</span><span style='color: #8FF'>8</span> <span style='color: #8FF'>6</span><span style='color: #8FF'>5</span><span style='color: #8FF'>6</span><span style='color: #8FF'>c</span> <span style='color: #8FF'>6</span><span style='color: #8FF'>c</span><span style='color: #8FF'>6</span><span style='color: #8FF'>f</span> <span style='color: #8FF'>0</span><span style='color: #8FF'>6</span><span style='color: #8FF'>3</span><span style='color: #8FF'>a</span> <span style='color: #8FF'>0</span><span style='color: #8FF'>6</span><span style='color: #8FF'>4</span><span style='color: #8FF'>5</span> <span style='color: #8FF'>5</span><span style='color: #8FF'>4</span><span style='color: red'>4</span><span style='color: red'>0</span> <span style='color: #CC0'>0</span><span style='color: #CC0'>6</span></pre>


<p>The typecode <code>40</code> is ASCII <code>@</code>. The typecode is followed by the
position of the object in the cache table. This cache table is
distinct from the symbol cache.</p>

<h2>The rest</h2>

<p>There&rsquo;s a more types that Marshal can handle, but not all of them are
interesting. The <a href="http://jakegoulding.com/blog/2013/01/20/a-final-dip-into-rubys-marshal-format/">next post</a> covers regexes, classes, modules,
and instances of objects.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[A little dip into Ruby's Marshal format]]></title>
    <link href="http://jakegoulding.com/blog/2013/01/15/a-little-dip-into-rubys-marshal-format/"/>
    <updated>2013-01-15T20:00:00-05:00</updated>
    <id>http://jakegoulding.com/blog/2013/01/15/a-little-dip-into-rubys-marshal-format</id>
    <content type="html"><![CDATA[<p>I recently tried to resolve a JRuby <a href="https://github.com/jruby/jruby/issues/456">issue involving Marshal</a>.
I&rsquo;ve used <a href="http://www.ruby-doc.org/core-1.9.3/Marshal.html">Marshal</a> before, but never needed to pay attention
to the actual bytes written to disk. I decided to write up what I
learned in the process.</p>

<!-- more -->


<h2>Version number</h2>

<pre><span style='color: red'>0</span><span style='color: red'>4</span><span style='color: #8FF'>0</span><span style='color: #8FF'>8</span></pre>


<p>I collected this data using Ruby 1.9.3p327, which has Marshal version
4.8. The version number is encoded with two bytes, one each for the
major and minor version. This version number precedes all dumps and
I&rsquo;ll ignore it for the rest of this post.</p>

<h2>Nil, true, false</h2>

<p><code>nil</code></p>

<pre>0408 <span style='color: red'>3</span><span style='color: red'>0</span></pre>


<p>The typecode <code>30</code> is ASCII <code>0</code>.</p>

<p><code>true</code></p>

<pre>0408 <span style='color: red'>5</span><span style='color: red'>4</span></pre>


<p>The typecode <code>54</code> is ASCII <code>T</code>.</p>

<p><code>false</code></p>

<pre>0408 <span style='color: red'>4</span><span style='color: red'>6</span></pre>


<p>The typecode <code>46</code> is ASCII <code>F</code>.</p>

<h2>Integers (easy)</h2>

<p><code>0</code></p>

<pre>0408 <span style='color: red'>6</span><span style='color: red'>9</span><span style='color: #8FF'>0</span><span style='color: #8FF'>0</span></pre>


<p>The typecode <code>69</code> is ASCII <code>i</code>. The typecode is followed by the value
of the integer. Zero is represented as <code>00</code>.</p>

<p><code>1</code></p>

<pre>0408 69<span style='color: #8FF'>0</span><span style='color: #8FF'>6</span></pre>


<p>Here we see that the encoded value for one is <code>06</code>, not <code>01</code> as we
might expect at first. This allows for more efficient storage of
smaller numbers. -123 &lt;= x &lt;= 122 can be encoded in just one byte.</p>

<h2>Arrays</h2>

<p><code>[]</code></p>

<pre>0408 <span style='color: red'>5</span><span style='color: red'>b</span><span style='color: #8FF'>0</span><span style='color: #8FF'>0</span></pre>


<p>The typecode <code>5b</code> is ASCII <code>[</code>. The typecode is followed by the
number of elements in the array.</p>

<p><code>[1]</code></p>

<pre>0408 5b<span style='color: #8FF'>0</span><span style='color: #8FF'>6</span> <span style='color: #CC0'>6</span><span style='color: #CC0'>9</span><span style='color: #CC0'>0</span><span style='color: #CC0'>6</span></pre>


<p>The number of items in the array is encoded in the same form as
integers. Each value in the array is encoded sequentially after the
size of the array.</p>

<h2>Hashes</h2>

<p><code>{}</code></p>

<pre>0408 <span style='color: red'>7</span><span style='color: red'>b</span><span style='color: #8FF'>0</span><span style='color: #8FF'>0</span></pre>


<p>The typecode <code>7b</code> is ASCII <code>{</code>. The typecode is followed by the number
of (key, value) pairs in the hash.</p>

<p><code>{1 =&gt; 2}</code></p>

<pre>0408 7b<span style='color: #8FF'>0</span><span style='color: #8FF'>6</span> <span style='color: #CC0'>6</span><span style='color: #CC0'>9</span><span style='color: #CC0'>0</span><span style='color: #CC0'>6</span> <span style='color: #0C0'>6</span><span style='color: #0C0'>9</span><span style='color: #0C0'>0</span><span style='color: #0C0'>7</span></pre>


<p>Like arrays, the number of items in the hash is encoded in the same
form as integers. Each pair of (key, value) is encoded sequentially
after the size of the hash.</p>

<h2>Symbols</h2>

<p><code>:hello</code></p>

<pre>0408 <span style='color: red'>3</span><span style='color: red'>a</span><span style='color: #8FF'>0</span><span style='color: #8FF'>a</span> <span style='color: #CC0'>6</span><span style='color: #CC0'>8</span><span style='color: #CC0'>6</span><span style='color: #CC0'>5</span> <span style='color: #CC0'>6</span><span style='color: #CC0'>c</span><span style='color: #CC0'>6</span><span style='color: #CC0'>c</span> <span style='color: #CC0'>6</span><span style='color: #CC0'>f</span></pre>


<p>The typecode <code>3a</code> is ASCII <code>:</code>. The typecode is followed by the length
of the symbol name and then the symbol name itself, encoded as UTF-8.</p>

<h2>Symlinks</h2>

<p>When a symbol is repeated multiple times, the Marshal encoding allows
subsequent instances to reference the first instance to save space in
the stream.</p>

<p><code>[:hello, :hello]</code></p>

<pre>0408 5b07 <span style='color: #8FF'>3</span><span style='color: #8FF'>a</span><span style='color: #8FF'>0</span><span style='color: #8FF'>a</span> <span style='color: #8FF'>6</span><span style='color: #8FF'>8</span><span style='color: #8FF'>6</span><span style='color: #8FF'>5</span> <span style='color: #8FF'>6</span><span style='color: #8FF'>c</span><span style='color: #8FF'>6</span><span style='color: #8FF'>c</span> <span style='color: #8FF'>6</span><span style='color: #8FF'>f</span><span style='color: red'>3</span><span style='color: red'>b</span> <span style='color: #CC0'>0</span><span style='color: #CC0'>0</span></pre>


<p>The typecode <code>3b</code> is ASCII <code>;</code>.  The typecode is followed by the
position of the symbol in the cache table. This table is indexed by
the order in which the symbol first appeared.</p>

<h2>The rest</h2>

<p>There&rsquo;s a lot more to the Marshal format; I haven&rsquo;t even covered
strings yet! You can find more at the <a href="http://jakegoulding.com/blog/2013/01/16/another-dip-into-rubys-marshal-format/">next post</a> in this
series, or jump right to the <a href="http://jakegoulding.com/blog/2013/01/20/a-final-dip-into-rubys-marshal-format/">last post</a>.</p>

<h2>How to explore on your own</h2>

<p>To generate the examples for this post, I hacked up a quick helper in
irb:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">dump</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</span><span class='line'>  <span class="no">File</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;/tmp/out&#39;</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span> <span class="p">{</span><span class="o">|</span><span class="n">f</span><span class="o">|</span> <span class="no">Marshal</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">f</span><span class="p">)}</span>
</span><span class='line'>  <span class="sb">`xxd /tmp/out`</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>



]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Conway's Game of Life without return values]]></title>
    <link href="http://jakegoulding.com/blog/2012/12/13/conways-game-of-life-without-return-values/"/>
    <updated>2012-12-13T15:14:00-05:00</updated>
    <id>http://jakegoulding.com/blog/2012/12/13/conways-game-of-life-without-return-values</id>
    <content type="html"><![CDATA[<p>On 2012-12-08, I attended the Pittsburgh
<a href="http://globalday.coderetreat.org/">Global Day of Code Retreat</a> facilitated by <a href="https://twitter.com/josephrkramer">Joe Kramer</a>
and <a href="https://twitter.com/jthurne">Jim Hurne</a>. As usual, I had a great time, and got to meet
new people from the Pittsburgh tech scene. It&rsquo;s always good for me to
remember that there are non-Ruby developers out there! I even started
the day off by doing the Game of Life in C#.</p>

<p>One of the more contentious constraints of the day was &ldquo;no return
values&rdquo;. I feel like I was the only one in the room that liked this
constraint at all!  As such, I wanted to finish it up to see what my
<a href="https://github.com/shepmaster/gdcr-no-return-values">final code</a> and observations would look like.</p>

<!-- more -->


<h3>Goal</h3>

<p>As I understand it, the point of this constraint is to explore
&ldquo;<a href="http://pragprog.com/articles/tell-dont-ask">tell don&rsquo;t ask</a>&rdquo;, with a secondary exploration of
<a href="http://martinfowler.com/articles/mocksArentStubs.html">mocks vs. stubs</a>.</p>

<h3>Constraint modifications</h3>

<p>I made some small tweaks to the constraint to deal with how Ruby
works and to avoid work orthogonal to the goal.</p>

<ul>
<li>Allow return values from constructors</li>
<li>Allow return values from standard library classes</li>
<li>Allow return values from private methods</li>
</ul>


<p>In Ruby, constructors are methods on a Class instance that return a
new instance of the class. Since everything in Ruby is an object, it
would be impossible to make progress if we didn&rsquo;t allow creating new
objects.</p>

<p>The goal of the constraint isn&rsquo;t to rewrite all of Ruby&rsquo;s standard
library. If we cannot use return values from the standard library, we
couldn&rsquo;t do something as simple as <code>a = 1 + 1</code>! Our newly-created
code will not return values, so it is safe to use return values hidden
away inside of our objects.</p>

<p>Allowing private methods to return values isn&rsquo;t strictly necessary,
but it allows us to reduce code duplication. Technically, we could
inline the private methods where they are used, but that would be
ugly. Since these methods are private, they won&rsquo;t add to the surface
area of our objects and shouldn&rsquo;t conflict with the goal of the
exercise.</p>

<h3>Things I liked</h3>

<p>I usually start out Conway&rsquo;s with the ability to see if a cell is
alive, followed quickly by the ability to bring a cell to life. This
means the first thing I do is rely on return values. This time, I
began with the concept of a UI that would be told when a cell is
alive. I found this interesting as I usually skip over the display
completely, leaving it as a &ldquo;trivial&rdquo; thing to be added later.</p>

<p>The <code>Board</code> class came into existence while implementing the
<code>time_passes</code> method because I needed to have both the current and
next board state. I like that this concept was reified; the <code>Game</code>
class deals with coordinating the rules and a board, but the <code>Board</code>
class deals with the particulars of the board state.</p>

<p>I was forced into giving human names to more things than I usually
would, such as <code>has_two_neighbors</code>, or <code>AliveCellRules</code>. I find that
this is the extended version of creating a well-named temporary
variable.</p>

<h3>Things I didn&rsquo;t like</h3>

<p>There are two rule-related classes, one for alive cells and one for
dead cells. The alive cell rules class is almost 100%
duplication. This could be reduced using Ruby&rsquo;s <code>alias</code> at the cost of
reduced readability, and still wouldn&rsquo;t help the duplication in the
dead cell rules. It&rsquo;s hard to tell if this would be good or bad in the
absence of future changes, but I don&rsquo;t like it as it stands now.</p>

<p>I wanted to create a <code>Point</code> class to abstract the concept of x / y
coordinates and also to have a place to hang the idea of
&ldquo;neighbors&rdquo;. Unfortunately, it would have solely existed to return
values: a list of points, equality comparisons, etc. I think this
would be an ideal example of a value type.</p>

<p>I love Ruby&rsquo;s <code>Struct</code>; I have written too many class initializers
longhand to ever want to go back. As far as I am concerned, <code>Struct</code>
reduces the work to make an initializer from <em>O(n)</em> to <em>O(1)</em>.
Unfortunately, it automatically creates a public <code>attr_accessor</code>,
which would be too tempting to use. I also avoided <code>attr_reader</code> for
the same reason, even though I could have made the reader
private. Seeing all the bare instance variables makes me
uncomfortable.</p>

<h3>Interesting implementation details</h3>

<p>For each public method, I returned <code>self</code>. In Ruby, the last executed
statement is implicitly returned. Returning <code>self</code> avoids accidentally
relying on a return value. In production code I wouldn&rsquo;t go this
overboard, trusting the caller to not use incidental return values. In
a language like Java, I would declare the method as void.</p>

<p>I&rsquo;ve never used <code>flat_map</code> before, but I&rsquo;m going to keep my eyes open
for more places to use it. I&rsquo;m not at the point where it comes without
thinking, but looking for <code>ary.map{ ... }.flatten(1)</code> should be easy
enough. Also, I learned that <code>flatten</code> can take an argument that
controls how deep it will go.</p>

<p>I swear that there is an existing method that does the equivalent of
<code>ary.reject { |x| x == CONSTANT }</code>, but I couldn&rsquo;t find it. <code>delete</code>
will mutate the array in place, which isn&rsquo;t quite the same.</p>

<h3>Tests</h3>

<p>As the code progressed, I had to start using RSpec&rsquo;s <code>as_null_object</code>
more frequently. This is because closely situated cells began
interacting and would be output to the user interface. I wasn&rsquo;t
interested in these outputs, but they weren&rsquo;t incorrect. After enough
tests needed a null object, I changed the test-wide mock, which may
have been too broad a change.</p>

<p>All of the tests that involve time passing have two duplicated
lines. These lines could have been pulled into the rarely-used <code>after</code>
block. I&rsquo;ve never seen code that does this, and I&rsquo;m not sure how I
feel about it.</p>

<p>I don&rsquo;t know what order I prefer for the <code>should_receive</code> calls
relative to the rest of the setup. In this case, I chose to put the
message expectations at the top of the test block.</p>

<h3>Final thoughts</h3>

<p>Like most exercises during Code Retreat, preventing return values has
benefits and disadvantages. I like how certain concepts were forced to
become reified and that I had to think more about the consumer of my
code. Contrariwise, I missed not being able to use <code>Struct</code> and really
wanted a <code>Point</code>.</p>

<p>Will I change how I code because of this? Maybe a little bit. It
probably would be good practice to avoid return values at first blush,
but I certainly won&rsquo;t stop using them completely. One thing I might
look further into is Ruby 1.9&rsquo;s <a href="http://www.ruby-doc.org/core-1.9.3/Enumerator.html"><code>Enumerator</code></a>. This would
allow me to provide a nice function that takes a block or returns an
enumerable for further chaining.</p>

<p>Feel free to read over the <a href="https://github.com/shepmaster/gdcr-no-return-values">code on GitHub</a> if you are
interested!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Refactor and make changes in different commits]]></title>
    <link href="http://jakegoulding.com/blog/2012/11/04/refactor-and-make-changes-in-different-commits/"/>
    <updated>2012-11-04T14:31:00-05:00</updated>
    <id>http://jakegoulding.com/blog/2012/11/04/refactor-and-make-changes-in-different-commits</id>
    <content type="html"><![CDATA[<p>If you combine refactoring and making a change to your code into the
same commit, you are going to have a bad time.</p>

<!-- more -->


<p>Just in case you&rsquo;ve forgotten, <a href="http://en.wikipedia.org/wiki/Refactoring">refactoring</a> is</p>

<blockquote><p>the process of changing a computer program&rsquo;s source code without
modifying its external functional behavior</p></blockquote>

<p>When I review a commit that claims to be refactoring, I shift into to
a very specific mindset. I visualize myself as a world-class goalie,
ready to stop any rogue features that come my way; I&rsquo;m going to stand
my ground, no matter what.</p>

<p>Contrast refactoring to adding new functionality. In that case, the
author should be adding new objects that fulfill a responsibility,
following the <a href="http://en.wikipedia.org/wiki/Open/closed_principle">open/closed principle</a>. When I review a commit
that adds new functionality, I pay attention that the tests cover the
new functionality, the minimum amount of code was added, and that the
new code is sufficient.</p>

<p>These are very different things to review for.</p>

<h3>Why&rsquo;s it bad?</h3>

<p>When you combine refactoring and feature addition into the same
commit, you double the work required to review it. In addition to
figuring out if each changed line is correct, you also have to figure
out what &ldquo;correct&rdquo; even means!</p>

<p>Beyond the doubled work, you have to change your mindset for <strong>every
line of code</strong>. That&rsquo;s an amazing amount of context switching. It&rsquo;s
very hard to thoroughly review each line when it&rsquo;s hard to even
remember what you are reviewing for.</p>

<p>Combining these disparate actions into one commit isn&rsquo;t something that
we do maliciously. In fact, it&rsquo;s likely the opposite: good programmers
have an innate drive to make the code better all the time. Sometimes
we see a little problem that we just <em>have</em> to fix up.</p>

<p>The problem is that when we have our programmer hats on, we don&rsquo;t
always think about what this commit will mean to others
downstream. This could mean reviewers, approvers, testers,
documenters, whatever needs to happen after the commit.</p>

<h3>What do I do when my commit is too big?</h3>

<p>There are a few main techniques I use when I discover I&rsquo;ve done work
that should be in different commits.</p>

<p>If I haven&rsquo;t committed yet, and the changes are separate enough, I use
<code>git add -pu</code> to add certain lines of code and not others.</p>

<p>If the changes overlap with each other, I will edit a specific section
of the file until it looks like the intermediate change I really
wanted. I <code>git add</code> the file and immediately revert my editor
changes. I then repeat with the next section.</p>

<p>If I have already committed, then I go into an interactive rebase and
<code>edit</code> the particular commits that are too big. I often create a
throw-away branch so I can easily compare the original and modified
branches to make sure they end up the same.</p>

<p>All of these techniques create &ldquo;false history&rdquo; &ndash; I didn&rsquo;t <em>really</em>
make that small step. After I&rsquo;m finished, I run a little script that
checks out each commit and runs my tests.</p>

<p>Sometimes, trying to preserve my changes isn&rsquo;t worth the time, or I
can see into the future a bit and know ahead of time that I am about
to make a big set of changes. In these cases, I try a spike: I make
the changes willy-nilly, writing down each step as I do it. Then I
throw it away and <em>invert the order of steps</em>. This allows each step
to happen in the order I would prefer, and I often improve on each
step.</p>

<h3>Isn&rsquo;t too many small commits just as bad?</h3>

<p>I&rsquo;ve heard something like this before:</p>

<blockquote><p>It&rsquo;s so small, it doesn&rsquo;t deserve it&rsquo;s own commit</p></blockquote>

<p>I&rsquo;ve <strong>never</strong> had to review a commit that was too small. I have had
to review a commit that was too large. I&rsquo;m willing to take the risk of
creating many small commits, especially if all the changes are going
to be made one way or another.</p>

<p>If a commit is small, then I can open it, read the commit message, and
review it within seconds.</p>

<h3>What do I do as a reviewer?</h3>

<p>I use a modified version of the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">single responsibility principle</a>
that applies to commits:</p>

<blockquote><p>A commit should have one, and only one, change.</p></blockquote>

<p>I try to follow steps like these:</p>

<ol>
<li>Read the whole commit message. It should have no <strong>and</strong>s, <strong>or</strong>s,
or <strong>but</strong>s. If it does, I kick it back to the author and request that
the commit be split up into those pieces. Otherwise, I sear the commit
message into my brain.</li>
<li>Read the diff of the commit and evaluate each change against the
commit message. If a line doesn&rsquo;t fit with the message, mark the
change as unrelated. If it does, review the line as usual.</li>
<li>Sometimes I keep reading the diff once you I find an unrelated
line, other times I stop at the first one; the original author may be
faster at separating the concerns.</li>
<li>Make sure to thank the author when they provide a commit with a
single focus &ndash; positive reinforcement lets us know that we are on the
right track!</li>
</ol>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Run your tests in a deterministic random order]]></title>
    <link href="http://jakegoulding.com/blog/2012/10/18/run-your-tests-in-a-deterministic-random-order/"/>
    <updated>2012-10-18T18:56:00-04:00</updated>
    <id>http://jakegoulding.com/blog/2012/10/18/run-your-tests-in-a-deterministic-random-order</id>
    <content type="html"><![CDATA[<p>Running your tests in a random order is a good idea to help shake out
implicit dependencies between tests. Running your tests in a
deterministic random order is even better.</p>

<!-- more -->


<h4>What&rsquo;s an implicit dependency?</h4>

<p>It&rsquo;s easy to accidentally create order-dependent tests:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">it</span> <span class="s2">&quot;creates a widget&quot;</span> <span class="k">do</span>
</span><span class='line'>  <span class="no">Widget</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="nb">name</span><span class="p">:</span> <span class="s1">&#39;Awesome Widget&#39;</span><span class="p">)</span>
</span><span class='line'>  <span class="no">Widget</span><span class="o">.</span><span class="n">count</span><span class="o">.</span><span class="n">should</span> <span class="n">eql</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">it</span> <span class="s2">&quot;deletes a widget&quot;</span> <span class="k">do</span>
</span><span class='line'>  <span class="no">Widget</span><span class="o">.</span><span class="n">first</span><span class="o">.</span><span class="n">delete</span> <span class="c1"># Implicitly requires the first test to have been run</span>
</span><span class='line'>  <span class="no">Widget</span><span class="o">.</span><span class="n">count</span><span class="o">.</span><span class="n">should</span> <span class="n">eql</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<h4>Why should I care?</h4>

<p>Dependencies between tests are bad for a number of reasons:</p>

<ol>
<li>When a single test fails, you need to run many tests to reproduce
the failure. This makes reproduction slower and more annoying.</li>
<li>The test method is no longer complete documentation. The required setup
for the test is located in many different methods.</li>
<li>The complexity of the test is hidden. What looks like a two line
test may actually comprise hundreds of lines of code. Complex test
code is often an excellent indicator of complex production code.</li>
</ol>


<p>Running tests in a random order isn&rsquo;t enough; you need to be able to
reproduce the same random order before you can fix it! <a href="https://www.relishapp.com/rspec/rspec-core/v/2-11/docs/command-line/order-new-in-rspec-core-2-8">RSpec</a>
and <a href="http://www.bootspring.com/2010/09/22/minitest-rubys-test-framework/">MiniTest</a> both offer a way to specify the random seed
on the command line or with environment variables. Unfortunately, the
<a href="http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html">Surefire</a> plugin for Maven does not offer a way to specify
the seed, even though it allows random ordering.</p>

<h4>Continuous integration servers</h4>

<p>At work, we use <a href="http://code.google.com/p/gerrit/">gerrit</a> for code reviews and
<a href="http://jenkins-ci.org/">Jenkins</a> as our CI server. Whenever a new or updated commit
is pushed to gerrit, a build is started in Jenkins. There is also a
Jenkins job to build <code>origin/master</code> every 15 minutes if it has been
updated.</p>

<p>The Gerrit/Jenkins combination allows you to retrigger a specific
build in case there were environmental issues that have since been
fixed. Unfortunately for us, retriggering was being used as a way to
avoid dealing with test failures due to order dependencies. To
encourage us to stop and address our order dependency problem, we
updated both jobs to use a deterministic seed.</p>

<p>For the Gerrit builds, we used the Gerrit change number, which remains
constant across multiple revisions of the same commit. The Gerrit
plugin makes this value available as a environment variable during
script execution.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>rspec <span class="nv">SPEC_OPTS</span><span class="o">=</span><span class="s2">&quot;--seed $GERRIT_CHANGE_NUMBER&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>For the <code>origin/master</code> build, we chose to use the Git hash of the
commit. Since the hash contains letters, we used a shell one-liner to
scrape out something that looks reasonable as a seed.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">SEED</span><span class="o">=</span><span class="k">$(</span>git rev-parse HEAD | tr -d <span class="s1">&#39;a-z&#39;</span> | cut -b 1-5<span class="k">)</span>
</span><span class='line'>rspec <span class="nv">SPEC_OPTS</span><span class="o">=</span><span class="s2">&quot;--seed $SEED&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<h4>Does it work?</h4>

<p>Just a few days after making the above changes, another developer came
to me with a strange problem. His commit was unable to pass the tests
in Gerrit, but the failing test had nothing to do with his changes. We
ran the tests locally using the seed from the Jenkins server and were
able to reproduce the problem. Ultimately, we traced the problem to a
request spec that modified some core configuration settings and didn&rsquo;t
reset them successfully. Success!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Watch out for lost updates when using Capybara with Selenium]]></title>
    <link href="http://jakegoulding.com/blog/2012/10/10/watch-out-for-lost-updates-when-using-capybara-with-selenium/"/>
    <updated>2012-10-10T19:41:00-04:00</updated>
    <id>http://jakegoulding.com/blog/2012/10/10/watch-out-for-lost-updates-when-using-capybara-with-selenium</id>
    <content type="html"><![CDATA[<p>At work, I am still working on finding and squashing fun test
failures. In this case, &ldquo;fun&rdquo; means tests that have an intermittent
failure rate of 5% (or less!). The test issue I worked on today had to
do with the &ldquo;lost update&rdquo; problem.</p>

<!-- more -->


<h3>The lost update problem</h3>

<p><a href="http://www.amazon.com/gp/product/0321503627/ref=as_li_ss_tl?ie=UTF8&amp;tag=jakgousblo-20&amp;linkCode=as2&amp;camp=217145&amp;creative=399369&amp;creativeASIN=0321503627">Growing Object-Oriented Software, Guided by Tests</a> has a great
description and diagram of the problem:</p>

<p><img src="http://jakegoulding.com/images/blog/lost-update.png" alt="The lost update problem" /></p>

<p>The short version is that when you poll a system for its state, it&rsquo;s
entirely possible to miss the state you are looking for. In the
diagram, the color changes to red and then to blue before the test
ever has a chance to see that it was red. Since this system will never
go back to red, the test will incorrectly fail.</p>

<h3>The lost update problem in Capybara</h3>

<p>Like many other sites, we use the <a href="http://datatables.net/">DataTables</a> jQuery
plugin to show tabular data. A test that ensured that the filtering
worked looked something like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">wait_for_table_loading</span>
</span><span class='line'>  <span class="n">dialog</span> <span class="o">=</span> <span class="n">page</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;.loading_dialog&#39;</span><span class="p">)</span>
</span><span class='line'>  <span class="n">wait_until</span> <span class="p">{</span> <span class="n">dialog</span><span class="o">.</span><span class="n">visible?</span> <span class="p">}</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">wait_for_table_ready</span>
</span><span class='line'>  <span class="n">dialog</span> <span class="o">=</span> <span class="n">page</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;.loading_dialog&#39;</span><span class="p">)</span>
</span><span class='line'>  <span class="n">wait_until</span> <span class="p">{</span> <span class="o">!</span> <span class="n">dialog</span><span class="o">.</span><span class="n">visible?</span> <span class="p">}</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">it</span> <span class="s1">&#39;filters the list&#39;</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">visit</span> <span class="n">list_path</span>
</span><span class='line'>  <span class="n">click_on</span> <span class="s1">&#39;Filter by active&#39;</span>
</span><span class='line'>  <span class="n">wait_for_table_loading</span>
</span><span class='line'>  <span class="n">wait_for_table_ready</span>
</span><span class='line'>  <span class="n">page</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="s1">&#39;.data-item&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">should</span> <span class="n">have</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="o">.</span><span class="n">items</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Enabling filtering triggers some slow backend activity, which brings
up the loading dialog. The test waits for that dialog to appear and
disappear before continuing on. Now the entire table is populated and
we can safely see how many elements are in the table.</p>

<p>However, the test will fail if the backend is <em>too fast</em>. The loading
dialog will appear and disappear almost immediately. The test will
time out waiting for the loading dialog that will never appear
again. This behavior can be reliably replicated by adding a sleep to
the test between lines 13 and 14.</p>

<h3>A Capybara solution</h3>

<p>In order to make the test more robust, I rewrote it as:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">it</span> <span class="s1">&#39;filters the list&#39;</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">visit</span> <span class="n">list_path</span>
</span><span class='line'>  <span class="n">click_on</span> <span class="s1">&#39;Filter by active&#39;</span>
</span><span class='line'>  <span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_css</span><span class="p">(</span><span class="s1">&#39;.data-item&#39;</span><span class="p">,</span> <span class="ss">:count</span> <span class="o">=&gt;</span> <span class="mi">3</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>The test now ignores the loading dialogs completely, instead asking
Capybara to find a particular number of elements. Asking Capybara to
find things in this manner will let the test leverage the built-in
waiting facilities of Capybara.</p>

<p>In this test, the number of data items won&rsquo;t change once the table is
loaded, so it is a safe state to poll. As an additional benefit, the
test now has fewer lines of code and is clearer.</p>

<p>As a downside, when the test fails, the Capybara error message doesn&rsquo;t
include how many items were found, which isn&rsquo;t as informative as the
equivalent message from the RSpec matcher.</p>

<p>Also, this test still ultimately relies on polling the DOM, so it&rsquo;s
possible for similar bugs to pop up in the future.</p>

<h3>The GOOS solution to the lost update problem</h3>

<p>GOOS provides a solution to the lost update problem that can avoid the
problems with polling completely:</p>

<p><img src="http://jakegoulding.com/images/blog/lost-update-fixed.png" alt="The solution to the lost update problem" /></p>

<p>The system under test must be modified to provide notifications when
something interesting happens. This system now has a listener that is
notified when the color changes and what the color is changed to. The
test supplies a simple listener that accumulates the changes and
offers a nice API suited for the tests.</p>

<h3>A hypothetical Capybara solution without polling</h3>

<p>I can imagine a Capybara test that looks something like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">it</span> <span class="s1">&#39;filters the list&#39;</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">visit</span> <span class="n">list_path</span>
</span><span class='line'>  <span class="n">wait_for_js_event</span><span class="p">(</span><span class="s1">&#39;table.loaded&#39;</span><span class="p">)</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">click_on</span> <span class="s1">&#39;Filter by active&#39;</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>  <span class="n">page</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="s1">&#39;.data-item&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">should</span> <span class="n">have</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="o">.</span><span class="n">items</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Under the hood, there&rsquo;s some extra JavaScript going on. The
<code>wait_for_js_event</code> method would inject some JavaScript into the
running Selenium session that creates an event listener and binds it
to the given event. This listener just collects all the events it
receives. After yielding the block, the test code then polls the event
listener, waiting for the event to be captured.</p>

<p>It&rsquo;s entirely possible that code that does this already exists, but I
don&rsquo;t know of it. It wouldn&rsquo;t be a large amount of code to write, but
it would straddle the borders of Capybara, Selenium and JavaScript.</p>

<p>This might be a useful thing for <a href="http://pivotal.github.com/jasmine/">Jasmine</a> tests, so it might
already exist in that ecosystem.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Finding a race condition in Capybara with Selenium]]></title>
    <link href="http://jakegoulding.com/blog/2012/10/08/finding-a-race-condition-in-capybara-with-selenium/"/>
    <updated>2012-10-08T20:40:00-04:00</updated>
    <id>http://jakegoulding.com/blog/2012/10/08/finding-a-race-condition-in-capybara-with-selenium</id>
    <content type="html"><![CDATA[<p>At work, we&rsquo;ve been using <a href="https://github.com/jnicklas/capybara">Capybara</a> and
<a href="https://code.google.com/p/selenium/">Selenium</a> to test our newest web application. Many of us
have used this combination before for our own projects, but it&rsquo;s new
territory for a work project.</p>

<p>Every so often, we would get this error from a specific test:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>Selenium::WebDriver::Error::StaleElementReferenceError:
</span><span class='line'>  Element not found in the cache - perhaps the page has changed since it was looked up</span></code></pre></td></tr></table></div></figure>


<p>The error was intermittent, so we fell into the seductive but
dangerous trap of simply rerunning our tests whenever it
failed. Recently, I had a bit of time and decided to dig into it and
fix it once and for all.</p>

<!-- more -->


<p>My first task was to see if I could reproduce the error locally. We
often saw the error when running the tests on our <a href="http://jenkins-ci.org/">Jenkins</a>
continuous integration server, so there was the possibility that the
problem was environmental. However, we also knew that the failure was
intermittent, so we couldn&rsquo;t be sure it was environmental even if the
test passed locally a few times.</p>

<p>I rigged up a small shell script to simply run the test over and over
again while I wandered away from my computer. The script looked
something like:</p>

<figure class='code'><figcaption><span> (test-script-runner.sh)</span> <a href='http://jakegoulding.com/downloads/code/test-script-runner.sh'>download</a></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'><span class="c">#!/bin/bash</span>
</span><span class='line'><span class="nb">set</span> -eu
</span><span class='line'>
</span><span class='line'><span class="nv">failures</span><span class="o">=</span>0
</span><span class='line'>
</span><span class='line'><span class="k">for </span>run in <span class="sb">`</span>seq 20<span class="sb">`</span>; <span class="k">do</span>
</span><span class='line'><span class="k">    if</span> ! rspec -e <span class="s1">&#39;the bad test&#39;</span>; <span class="k">then</span>
</span><span class='line'><span class="k">        </span><span class="nv">failures</span><span class="o">=</span><span class="k">$((</span><span class="nv">$failures</span> <span class="o">+</span> <span class="m">1</span><span class="k">))</span>
</span><span class='line'>    <span class="k">fi</span>
</span><span class='line'>
</span><span class='line'><span class="k">    </span><span class="nb">echo</span> <span class="s2">&quot;Test run $run complete, $failures failures&quot;</span>
</span><span class='line'><span class="k">done</span>
</span></code></pre></td></tr></table></div></figure>


<p>I&rsquo;m sure there&rsquo;s a proper statistical manner to determine how many
times the test would have to be run without failing to be reasonably
certain that the test won&rsquo;t fail, but I didn&rsquo;t have to worry about
that &ndash; the test failed somewhere within the first ten or so runs.</p>

<p>Now that I knew the test could fail on my local setup, it was time to
dig into what the test was doing. The test was fairly concise and
readable (which I highly appreciated) and looked something like:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">it</span> <span class="s1">&#39;deletes the element&#39;</span><span class="p">,</span> <span class="ss">:js</span> <span class="o">=&gt;</span> <span class="kp">true</span>  <span class="k">do</span>
</span><span class='line'>  <span class="n">visit</span> <span class="n">path_to_the_page</span>
</span><span class='line'>  <span class="n">click_on</span> <span class="s1">&#39;Remove item&#39;</span>
</span><span class='line'>  <span class="n">page</span><span class="o">.</span><span class="n">should_not</span> <span class="n">have_css</span><span class="p">(</span><span class="s2">&quot;.item&quot;</span><span class="p">,</span> <span class="ss">text</span><span class="p">:</span> <span class="s2">&quot;Old text&quot;</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>The exception was coming from line #4 &ndash; when the test made the first
assertion about the elements. Unfortunately, the stacktrace isn&rsquo;t very
useful, as it mostly contains references to the JavaScript running
inside of Firefox. The exception text indicates that the test has a
reference to an element, but it isn&rsquo;t available in the cache
anymore. Considering that the test just deleted the element, this is
certainly suspicious.</p>

<p>At this point, I cloned the capybara repository and started poking
around. A <code>git grep</code> quickly found where <code>has_no_css?</code> was
<a href="https://github.com/jnicklas/capybara/blob/1.1_stable/lib/capybara/node/matchers.rb#L171">defined</a>. Following the thread of code led to
<code>has_no_selector?</code>, which calls the <code>all</code> <a href="https://github.com/jnicklas/capybara/blob/1.1_stable/lib/capybara/node/finders.rb#L109">method</a>. This method
had a pretty clear split between the &ldquo;finding&rdquo; part of the code and
the &ldquo;filtering&rdquo; part. There was no magic I used here to see this, just
previous experience debugging race conditions.</p>

<p>I opened up the installed gem and inserted a sleep directly into the
code between the &ldquo;finding&rdquo; and &ldquo;filtering&rdquo; sections. It&rsquo;s ugly doing
this, but it&rsquo;s good to try to not change too many things at once when
debugging. I played with the sleep value a bit and eventually found a
value that reliably reproduced the failure. Success!</p>

<p>Well, maybe not <em>complete</em> success, but at least a step in the right
direction. Even though I could reproduce the problem, I had only
reproduced it in our production application, and I had modified my
installed gem directly. It was time to make a nice test case.</p>

<p>I created a new Rails app and added the requisite RSpec gems. Since we
only need a simple HTML page with a bit of JavaScript to remove the
element, I modified the index.html that ships with Rails to have the
JavaScript inline and created an element and link to wire the action
to.</p>

<p>Since I knew that I would want to make changes to Capybara, I used the
<code>:path</code> parameter in the Gemfile to point to my local checkout of
Capybara. This is an awesome feature of <a href="http://gembundler.com/">Bundler</a> that you might not
know about. It also means I&rsquo;m not messing with my generally-available
copy of Capybara, which is good for my sanity.</p>

<p>I then created a stripped-down version of the test, the same as the
example above. After getting everything hooked up, I ran the test but
it didn&rsquo;t fail. This was bad news &ndash; I had done a few big steps between
the production app and the smaller test case &ndash; which one of them could
have changed the behavior?</p>

<p>This is where my knowledge of our production system came in useful. In
that application, we aren&rsquo;t just removing something from the page, we
are persisting that deletion to disk. Doing that can add some time
before the JavaScript fires to remove the item. I changed the test
JavaScript to have a delay less than the delay in Capybara and ran the
test again. It failed, just like we wanted it to. To be sure, I ran
the test case a bunch of times to make sure it always failed and for
the expected reason. Success!</p>

<p>Well, almost. Even though I had a test case, I still needed to show
that code to someone who could do something about it. Checking back at
the <a href="https://github.com/jnicklas/capybara">Capybara website</a>, I looked for how to submit a
ticket. Right at the top is a nice, clear comment:</p>

<blockquote><p>Need help? Ask on the mailing list (please do not open an issue on GitHub)</p></blockquote>

<p>So, I pushed my changes to Capybara to <a href="https://github.com/shepmaster/capybara">my fork</a> and
updated my test app to use a remote git version of the gem (another
cool Bundler feature). I then pushed <a href="https://github.com/shepmaster/capybara-race">my test case</a> to
GitHub as well. I took a bit of time to create a short README so that
anyone stumbling on the test app would have a clue as to what it was.</p>

<p>After that, it was just a small matter to write up a clear email to
the Capybara list. I still find emailing new lists scary. Who knows
how the list will respond? This time I got a <a href="https://groups.google.com/forum/?fromgroups=#!topic/ruby-capybara/O3Ib6INOP58">nice surprise</a>:</p>

<blockquote><p>That&rsquo;s a very nice bug report, Jake.</p>

<p>It appears to be a bug indeed. I&rsquo;ve been able to reproduce it on master as well.</p></blockquote>

<p>A GitHub issue <a href="https://github.com/jnicklas/capybara/issues/843">has been opened</a>, and the bug is well on the
way to being fixed. Yay for Open Source!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Name your variables by the roles they play]]></title>
    <link href="http://jakegoulding.com/blog/2012/10/03/name-your-variables-by-the-roles-they-play/"/>
    <updated>2012-10-03T19:49:00-04:00</updated>
    <id>http://jakegoulding.com/blog/2012/10/03/name-your-variables-by-the-roles-they-play</id>
    <content type="html"><![CDATA[<p>Have you ever seen a variable with a terrible name? This is of course
a trick question; everyone has. I&rsquo;d like to look at a particular
variable-naming annoyance: naming the variable based on the class
name.</p>

<!-- more -->


<p>In a statically-typed language without type inference, like Java, you
have likely seen something like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="n">FooBarZed</span> <span class="n">fooBarZed</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FooBarZed</span><span class="o">(</span><span class="kc">true</span><span class="o">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>In a dynamically typed language, like Ruby, it would look more like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">foo_bar_zed</span> <span class="o">=</span> <span class="no">FooBarZed</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="kp">true</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>This style of code may make sense to you. Maybe your class names are
self-describing, and so duplicating the class name as the variable
name is just &ldquo;reusing a good thing&rdquo;.</p>

<p>The problem with naming variables in this style is that the class
name, at <em>best</em>, describes what is special about an object or how the
object works. When you are using the object, you want to know why you
are using it &ndash; you want to know the <em>role</em> of the object:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># this variable name says nothing new to the reader of the code</span>
</span><span class='line'><span class="n">url_fetcher</span> <span class="o">=</span> <span class="no">UrlFetcher</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s1">&#39;http://example.com/&#39;</span><span class="p">)</span>
</span><span class='line'><span class="c1"># this variable name explains why we want to do something</span>
</span><span class='line'><span class="n">conversion_rate_fetcher</span> <span class="o">=</span> <span class="no">UrlFetcher</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s1">&#39;http://example.com/&#39;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>As a pragmatic argument, think how often you rename a class to better
describe it. Now think how often you&rsquo;ve changed a class name AND all
the variables to match the new class.</p>

<p>The role that an object plays changes at vastly different rate than
the name of the class. It&rsquo;s unlikely that the role will change
dramatically, as that new role would probably be better represented by
a brand new object. Similarly, code that uses that object is unlikely
to want an object that fills a different role without a rewrite of how
the calling code works.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Using Ruby blocks to ensure resources are cleaned up]]></title>
    <link href="http://jakegoulding.com/blog/2012/10/01/using-ruby-blocks-to-ensure-resources-are-cleaned-up/"/>
    <updated>2012-10-01T18:05:00-04:00</updated>
    <id>http://jakegoulding.com/blog/2012/10/01/using-ruby-blocks-to-ensure-resources-are-cleaned-up</id>
    <content type="html"><![CDATA[<p>In programming, cleaning up resources you have created is an
easily-overlooked problem. In languages like C, you have to clean up
everything by hand: memory, files, network sockets, etc. Languages
that have a garbage collector take away the need to explicitly free
memory, but you still have to manage the other resources.</p>

<!-- more -->


<p>In Ruby, we can use blocks to help ensure resources are closed. You&rsquo;ve
probably seen this idiom when dealing with files. The File class
ensures that the file is closed after the block is finished:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">File</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;file.txt&#39;</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">file</span><span class="o">|</span>
</span><span class='line'>  <span class="c1"># ... work with the file ...</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>However, this can only be used when the lifespan of the resource is one
method call. If you need to keep the file around in an instance
variable, then you cannot use this pattern, and must fall back to
explicitly closing the resource:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Foo</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">initialize</span>
</span><span class='line'>    <span class="vi">@file</span> <span class="o">=</span> <span class="no">File</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;file.txt&#39;</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">close</span>
</span><span class='line'>    <span class="vi">@file</span><span class="o">.</span><span class="n">close</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>To make this code nicer on yourself and others that have to use it,
you should add a method that handles closing the resource for you,
just like <code>File</code> does:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Foo</span>
</span><span class='line'>  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">open</span>
</span><span class='line'>    <span class="n">foo</span> <span class="o">=</span> <span class="no">Foo</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'>    <span class="k">yield</span> <span class="n">foo</span>
</span><span class='line'>  <span class="k">ensure</span>
</span><span class='line'>    <span class="n">foo</span><span class="o">.</span><span class="n">close</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Unfortunately, this clean implementation of <code>open</code> has a problem if
the constructor of <code>File</code> can throw an exception. The exception will
occur before the variable <code>foo</code> can be set to anything, so the <code>close</code>
message will be sent to <code>nil</code> instead, causing another exception!
<em>This <strong>certainly</strong> didn&rsquo;t happen in any code I was writing&hellip;</em></p>

<p>To handle this case, we have to give up on using the implicit `begin&#8220;
block from the function and create our own scope:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Foo</span>
</span><span class='line'>  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">open</span>
</span><span class='line'>    <span class="n">foo</span> <span class="o">=</span> <span class="no">Foo</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'>    <span class="k">begin</span>
</span><span class='line'>      <span class="k">yield</span> <span class="n">foo</span>
</span><span class='line'>    <span class="k">ensure</span>
</span><span class='line'>      <span class="n">foo</span><span class="o">.</span><span class="n">close</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now we can safely create a <code>Foo</code> and close it all in one place, but
what if another object wants to keep an instance of <code>Foo</code> around for
longer? It&rsquo;s nice to transparently handle both cases:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Foo</span>
</span><span class='line'>  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">open</span>
</span><span class='line'>    <span class="n">foo</span> <span class="o">=</span> <span class="no">Foo</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">return</span> <span class="n">foo</span> <span class="k">unless</span> <span class="nb">block_given?</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">begin</span>
</span><span class='line'>      <span class="k">yield</span> <span class="n">foo</span>
</span><span class='line'>    <span class="k">ensure</span>
</span><span class='line'>      <span class="n">foo</span><span class="o">.</span><span class="n">close</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Be careful when using JUnit's expected exceptions]]></title>
    <link href="http://jakegoulding.com/blog/2012/09/26/be-careful-when-using-junit-expected-exceptions/"/>
    <updated>2012-09-26T18:52:00-04:00</updated>
    <id>http://jakegoulding.com/blog/2012/09/26/be-careful-when-using-junit-expected-exceptions</id>
    <content type="html"><![CDATA[<p>For many people, JUnit is the grand-daddy of testing frameworks. Even
though other testing frameworks came first, a lot of people got their
start with JUnit.</p>

<p>People often start out testing with simple Boolean assertions, then
move on substring matching, then maybe on to mocks and stubs. At some
point, however, most people want to assert that their code throws a
particular exception, and that&rsquo;s where our story starts.</p>

<!-- more -->


<p>When JUnit 3 was the latest and greatest, you were supposed to catch
the exception yourself and assert if no such exception was
thrown. Here&rsquo;s an example I tweaked from <a href="http://radio.javaranch.com/lasse/2007/05/17/1179405760728.html">Lasse&rsquo;s blog</a> and the
JUnit documentation for <a href="http://kentbeck.github.com/junit/javadoc/latest/org/junit/Test.html"><code>@Test</code></a>.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Test</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">void</span> <span class="nf">test_for_npe_with_try_catch</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>    <span class="k">try</span> <span class="o">{</span>
</span><span class='line'>        <span class="k">throw</span> <span class="k">new</span> <span class="nf">NullPointerException</span><span class="o">();</span>
</span><span class='line'>        <span class="n">fail</span><span class="o">(</span><span class="s">&quot;should&#39;ve thrown an exception!&quot;</span><span class="o">);</span>
</span><span class='line'>    <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">NullPointerException</span> <span class="n">expected</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>        <span class="c1">// go team!</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>With the newest versions of JUnit 4 (4.11 at the time of writing),
there are two more options available to you: the <a href="http://kentbeck.github.com/junit/javadoc/latest/org/junit/Test.html"><code>@Test</code></a>
annotation and the <a href="http://kentbeck.github.com/junit/javadoc/latest/org/junit/rules/ExpectedException.html"><code>ExpectedException</code></a> rule.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Test</span><span class="o">(</span><span class="n">expected</span> <span class="o">=</span> <span class="n">NullPointerException</span><span class="o">.</span><span class="na">class</span><span class="o">)</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">void</span> <span class="nf">test_for_npe_with_annotation</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>    <span class="k">throw</span> <span class="k">new</span> <span class="nf">NullPointerException</span><span class="o">();</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Rule</span>
</span><span class='line'><span class="kd">public</span> <span class="n">ExpectedException</span> <span class="n">thrown</span> <span class="o">=</span> <span class="n">ExpectedException</span><span class="o">.</span><span class="na">none</span><span class="o">();</span>
</span><span class='line'>
</span><span class='line'><span class="nd">@Test</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">void</span> <span class="nf">test_for_npe_with_rule</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>    <span class="n">thrown</span><span class="o">.</span><span class="na">expect</span><span class="o">(</span><span class="n">NullPointerException</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
</span><span class='line'>    <span class="k">throw</span> <span class="k">new</span> <span class="nf">NullPointerException</span><span class="o">();</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Both of these forms offer a lot in the way of conciseness and
readability, and I prefer to use them when I need to test this kind of
thing. However, both forms can cause a test to pass when it shouldn&rsquo;t
when the code can throw the exception in multiple ways:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Test</span><span class="o">(</span><span class="n">expected</span> <span class="o">=</span> <span class="n">NullPointerException</span><span class="o">.</span><span class="na">class</span><span class="o">)</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">void</span> <span class="nf">test_for_npe_but_which_one</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>    <span class="n">CoolObject</span> <span class="n">obj</span> <span class="o">=</span> <span class="k">new</span> <span class="n">CoolObject</span><span class="o">(</span><span class="kc">null</span><span class="o">);</span>
</span><span class='line'>    <span class="n">obj</span><span class="o">.</span><span class="na">doSomeSetupWork</span><span class="o">(</span><span class="mi">42</span><span class="o">);</span>  <span class="c1">// What actually throws the exception</span>
</span><span class='line'>    <span class="n">obj</span><span class="o">.</span><span class="na">calculateTheAnswer</span><span class="o">();</span> <span class="c1">// What we want to throw the exception</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>In languages that have lambdas or equivalents, this problem is easily
avoided. For example, you can use <a href="https://www.relishapp.com/rspec/rspec-expectations/v/2-11/docs/built-in-matchers/raise-error-matcher"><code>expect</code></a> and <a href="https://www.relishapp.com/rspec/rspec-expectations/v/2-11/docs/built-in-matchers/raise-error-matcher"><code>raise_error</code></a> in RSpec:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">it</span> <span class="s1">&#39;throws_a_npe&#39;</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">obj</span> <span class="o">=</span> <span class="no">CoolObject</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="kp">nil</span><span class="p">)</span>
</span><span class='line'>  <span class="n">obj</span><span class="o">.</span><span class="n">do_some_setup_work</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
</span><span class='line'>  <span class="n">expect</span> <span class="p">{</span> <span class="n">obj</span><span class="o">.</span><span class="n">calculate_the_answer</span> <span class="p">}</span><span class="o">.</span><span class="n">to_raise</span><span class="p">(</span><span class="no">NoMethodError</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<h4>Alternate solutions</h4>

<p>Until a version of Java is released with lambdas, I see no better
solution than using try-catch blocks, the old JUnit 3 way. You could
define an interface and then create anonymous classes in the test to
have the desired level of granularity. This is a pretty bulky syntax,
any variables you use in the object would need to be declared final,
and then you have to explictly run the code!</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Test</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">void</span> <span class="nf">test_for_one_of_two_npe_bulky_syntax</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>    <span class="kd">final</span> <span class="n">CoolObject</span> <span class="n">obj</span> <span class="o">=</span> <span class="k">new</span> <span class="n">CoolObject</span><span class="o">(</span><span class="kc">null</span><span class="o">);</span>
</span><span class='line'>    <span class="n">obj</span><span class="o">.</span><span class="na">doSomeSetupWork</span><span class="o">(</span><span class="mi">42</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">new</span> <span class="nf">GonnaThrowException</span><span class="o">(</span><span class="n">NullPointerException</span><span class="o">.</span><span class="na">class</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>        <span class="kd">public</span> <span class="kt">void</span> <span class="nf">test</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>            <span class="n">obj</span><span class="o">.</span><span class="na">calculateTheAnswer</span><span class="o">();</span>
</span><span class='line'>        <span class="o">}</span>
</span><span class='line'>    <span class="o">}.</span><span class="na">run</span><span class="o">();</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>If you can rephrase the problem slightly, you might be able to use the
fact that <code>ExpectedException</code> can assert on the exception message to
restrict your test. If you know that <strong>only</strong> your error can include a
certain string, then checking for that string could prevent tests from
passing when they shouldn&rsquo;t.</p>

<p>Another solution would be to modify your code or tests so that you
don&rsquo;t have to deal with the problem in the first place. If you can
move the setup code into a <code>@Before</code> block, then the exception
wouldn&rsquo;t be caught by the test. If you can change your code so it
cannot throw the exception multiple ways, or if it throws different
exceptions, then that would also allow you to sidestep the problem.</p>

<p><strong>Update 2012-09-27</strong></p>

<p>David Bradley points out that if you configure the JUnit rule right
before the expected exception, you can reduce the possibility of
error. Unfortunately, exceptions thrown <em>after</em> the desired line will
still cause the test to pass incorrectly. This may not be a problem in
practice, as you are unlikely to continue the test after an exception
should be thrown, and most Java tests do not have a teardown phase.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Rule</span>
</span><span class='line'><span class="kd">public</span> <span class="n">ExpectedException</span> <span class="n">thrown</span> <span class="o">=</span> <span class="n">ExpectedException</span><span class="o">.</span><span class="na">none</span><span class="o">();</span>
</span><span class='line'>
</span><span class='line'><span class="nd">@Test</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">void</span> <span class="nf">test_for_npe_with_rule_at_last_moment</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>    <span class="n">CoolObject</span> <span class="n">obj</span> <span class="o">=</span> <span class="k">new</span> <span class="n">CoolObject</span><span class="o">(</span><span class="kc">null</span><span class="o">);</span>
</span><span class='line'>    <span class="n">obj</span><span class="o">.</span><span class="na">doSomeSetupWork</span><span class="o">(</span><span class="mi">42</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">thrown</span><span class="o">.</span><span class="na">expect</span><span class="o">(</span><span class="n">NullPointerException</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
</span><span class='line'>    <span class="n">obj</span><span class="o">.</span><span class="na">calculateTheAnswer</span><span class="o">();</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// Any exceptions here will still cause the test to pass incorrectly</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>



]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[A refactoring example: lots of if-else statements on strings]]></title>
    <link href="http://jakegoulding.com/blog/2012/09/24/a-refactoring-example-if-else-on-strings/"/>
    <updated>2012-09-24T12:00:00-04:00</updated>
    <id>http://jakegoulding.com/blog/2012/09/24/a-refactoring-example-if-else-on-strings</id>
    <content type="html"><![CDATA[<p>I recently did a bit of work that turned out to be a great exercise
for refactoring a huge sequence of if-else statements based on
strings. There are a few ugly bits left, so I&rsquo;m still poking at it,
but I am pleased with my progress so far.</p>

<!-- more -->


<p>While the original code was Java, the meat of the problem can be
easily shown in Ruby. Translating it to Ruby also makes it easier to
make sure I don&rsquo;t accidentally share any proprietary information!</p>

<p>The problem involves processing a hunk of XML to create nested
configuration objects. The original implementation used a sequence of
if-else blocks, but the Ruby version would have used a <code>case</code>
statement.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">ObjectsFromXML</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">element</span><span class="p">)</span>
</span><span class='line'>    <span class="n">new_object</span> <span class="o">=</span> <span class="kp">nil</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">case</span> <span class="n">element</span><span class="o">.</span><span class="n">name</span>
</span><span class='line'>    <span class="k">when</span> <span class="s1">&#39;foo&#39;</span>
</span><span class='line'>      <span class="n">f</span> <span class="o">=</span> <span class="no">Foo</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">element</span><span class="o">[</span><span class="s1">&#39;name&#39;</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'>      <span class="n">parent</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
</span><span class='line'>      <span class="n">new_object</span> <span class="o">=</span> <span class="n">f</span>
</span><span class='line'>    <span class="k">when</span> <span class="s1">&#39;bar&#39;</span>
</span><span class='line'>      <span class="n">b</span> <span class="o">=</span> <span class="no">Bar</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">element</span><span class="o">[</span><span class="s1">&#39;name&#39;</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'>      <span class="n">b</span><span class="o">.</span><span class="n">weight</span> <span class="o">=</span> <span class="n">element</span><span class="o">[</span><span class="s1">&#39;weight&#39;</span><span class="o">].</span><span class="n">to_f</span>
</span><span class='line'>      <span class="n">parent</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
</span><span class='line'>      <span class="n">new_object</span> <span class="o">=</span> <span class="n">b</span>
</span><span class='line'>    <span class="c1"># ... about 20 of these cases in total</span>
</span><span class='line'>    <span class="k">else</span>
</span><span class='line'>      <span class="k">raise</span> <span class="s2">&quot;Invalid node&quot;</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">element</span><span class="o">.</span><span class="n">children</span><span class="o">.</span><span class="n">each</span> <span class="p">{</span> <span class="o">|</span><span class="n">c</span><span class="o">|</span> <span class="n">process</span><span class="p">(</span><span class="n">new_object</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>This function has some issues: it is pretty long, and each case has
some similarity to the next but are different enough to be
annoying. The code certainly doesn&rsquo;t try to adhere to the Single
Responsibility Principle!</p>

<p>My first step was to split the blocks into classes with a common interface.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">FooHandler</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">element</span><span class="p">)</span>
</span><span class='line'>    <span class="no">Foo</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">element</span><span class="o">[</span><span class="s1">&#39;name&#39;</span><span class="o">]</span><span class="p">)</span><span class="o">.</span><span class="n">tap</span> <span class="o">|</span><span class="n">f</span><span class="o">|</span>
</span><span class='line'>      <span class="n">parent</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">BarHandler</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">element</span><span class="p">)</span>
</span><span class='line'>    <span class="no">Bar</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">element</span><span class="o">[</span><span class="s1">&#39;name&#39;</span><span class="o">]</span><span class="p">)</span><span class="o">.</span><span class="n">tap</span>
</span><span class='line'>      <span class="n">b</span><span class="o">.</span><span class="n">weight</span> <span class="o">=</span> <span class="n">element</span><span class="o">[</span><span class="s1">&#39;weight&#39;</span><span class="o">].</span><span class="n">to_f</span>
</span><span class='line'>      <span class="n">parent</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">ObjectsFromXML</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">element</span><span class="p">)</span>
</span><span class='line'>    <span class="n">new_object</span> <span class="o">=</span> <span class="kp">nil</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">case</span> <span class="n">element</span><span class="o">.</span><span class="n">name</span>
</span><span class='line'>    <span class="k">when</span> <span class="s1">&#39;foo&#39;</span>
</span><span class='line'>      <span class="n">new_object</span> <span class="o">=</span> <span class="no">FooHandler</span><span class="o">.</span><span class="n">new</span><span class="o">.</span><span class="n">process</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">element</span><span class="p">)</span>
</span><span class='line'>    <span class="k">when</span> <span class="s1">&#39;bar&#39;</span>
</span><span class='line'>      <span class="n">new_object</span> <span class="o">=</span> <span class="no">BarHandler</span><span class="o">.</span><span class="n">new</span><span class="o">.</span><span class="n">process</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">element</span><span class="p">)</span>
</span><span class='line'>    <span class="c1"># ... other cases</span>
</span><span class='line'>    <span class="k">else</span>
</span><span class='line'>      <span class="k">raise</span> <span class="s2">&quot;Invalid node&quot;</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">element</span><span class="o">.</span><span class="n">children</span><span class="o">.</span><span class="n">each</span> <span class="p">{</span> <span class="o">|</span><span class="n">c</span><span class="o">|</span> <span class="n">process</span><span class="p">(</span><span class="n">new_object</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>This corrals the item-specific code to an item-specific class. If I
need to change how the Bar class is created, only the BarHandler class
needs to be updated.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">FooHandler</span>
</span><span class='line'>  <span class="c1"># As above...</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">BarHandler</span>
</span><span class='line'>  <span class="c1"># As above...</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">ObjectsFromXML</span>
</span><span class='line'>  <span class="kp">attr_reader</span> <span class="ss">:handlers</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">initialize</span>
</span><span class='line'>    <span class="vi">@handlers</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class='line'>    <span class="n">handlers</span><span class="o">[</span><span class="s1">&#39;foo&#39;</span><span class="o">]</span> <span class="o">=</span> <span class="no">FooHandler</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'>    <span class="n">handlers</span><span class="o">[</span><span class="s1">&#39;bar&#39;</span><span class="o">]</span> <span class="o">=</span> <span class="no">BarHandler</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'>    <span class="c1"># ... other cases</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">element</span><span class="p">)</span>
</span><span class='line'>    <span class="n">handler</span> <span class="o">=</span> <span class="n">handlers</span><span class="o">.</span><span class="n">fetch</span><span class="p">(</span><span class="n">element</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="p">{</span> <span class="k">raise</span> <span class="s2">&quot;Invalid node&quot;</span> <span class="p">}</span>
</span><span class='line'>    <span class="n">new_object</span> <span class="o">=</span> <span class="n">handler</span><span class="o">.</span><span class="n">process</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">element</span><span class="p">)</span>
</span><span class='line'>    <span class="n">element</span><span class="o">.</span><span class="n">children</span><span class="o">.</span><span class="n">each</span> <span class="p">{</span> <span class="o">|</span><span class="n">c</span><span class="o">|</span> <span class="n">process</span><span class="p">(</span><span class="n">new_object</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now all the handlers are in a hash, keyed by the expected element
name. This allows me to pull out the correct handler and go. The
<code>process</code> function now only needs to be concerned with picking the
right handler and dealing with children elements.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">FooHandler</span>
</span><span class='line'>  <span class="c1"># As above...</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">element_name</span>
</span><span class='line'>    <span class="s1">&#39;foo&#39;</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">BarHandler</span>
</span><span class='line'>  <span class="c1"># As above...</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">element_name</span>
</span><span class='line'>    <span class="s1">&#39;bar&#39;</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">Handlers</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">initialize</span>
</span><span class='line'>    <span class="vi">@handlers</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="n">handler</span><span class="p">)</span>
</span><span class='line'>    <span class="vi">@handlers</span><span class="o">[</span><span class="n">handler</span><span class="o">.</span><span class="n">element_name</span><span class="o">]</span> <span class="o">=</span> <span class="n">handler</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">element</span><span class="p">)</span>
</span><span class='line'>    <span class="n">handler</span> <span class="o">=</span> <span class="n">handlers</span><span class="o">.</span><span class="n">fetch</span><span class="p">(</span><span class="n">element</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="p">{</span> <span class="k">raise</span> <span class="s2">&quot;Invalid node&quot;</span> <span class="p">}</span>
</span><span class='line'>    <span class="n">handler</span><span class="o">.</span><span class="n">process</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">element</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">ObjectsFromXML</span>
</span><span class='line'>  <span class="kp">attr_reader</span> <span class="ss">:handlers</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">initialize</span>
</span><span class='line'>    <span class="vi">@handlers</span> <span class="o">=</span> <span class="no">Handlers</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'>    <span class="n">handlers</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="no">FooHandler</span><span class="o">.</span><span class="n">new</span><span class="p">)</span>
</span><span class='line'>    <span class="n">handlers</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="no">BarHandler</span><span class="o">.</span><span class="n">new</span><span class="p">)</span>
</span><span class='line'>    <span class="c1"># ... other cases</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">element</span><span class="p">)</span>
</span><span class='line'>    <span class="n">new_object</span> <span class="o">=</span> <span class="n">handlers</span><span class="o">.</span><span class="n">process</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">element</span><span class="p">)</span>
</span><span class='line'>    <span class="n">element</span><span class="o">.</span><span class="n">children</span><span class="o">.</span><span class="n">each</span> <span class="p">{</span> <span class="o">|</span><span class="n">c</span><span class="o">|</span> <span class="n">process</span><span class="p">(</span><span class="n">new_object</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now we have moved the concept of the expected element name into the
handler itself. This makes sense, as each handler should know what
name it expects, not some other piece of code. I also took the
opportunity to move the code purely related to the handlers to a new
class that is highly focused on that one responsibility.</p>

<p>Some further refinements happened after this last point. The
<code>ObjectsFromXML</code> class became another <code>Handler</code> class, which made it
the same level of abstraction as the other handlers and removed a
redundant <code>process</code> method. The return code was removed because it
wasn&rsquo;t used except in a few tests. Iterating over children was moved
to each class that could contain children.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[The stages of code review]]></title>
    <link href="http://jakegoulding.com/blog/2012/07/01/the-stages-of-code-review/"/>
    <updated>2012-07-01T13:37:00-04:00</updated>
    <id>http://jakegoulding.com/blog/2012/07/01/the-stages-of-code-review</id>
    <content type="html"><![CDATA[<p>We recently started using <a href="http://code.google.com/p/gerrit/">gerrit</a> to perform code reviews for
a legacy C codebase that I work on. I also help out on a couple of
newer Java and Ruby projects that have had the benefit of having code
reviews and testing infrastructure from day one.</p>

<!-- more -->


<p>Starting to use gerrit on our C code has led me to think about how we
approach code reviews, and I&rsquo;ve identified some stages that we have
gone through. It&rsquo;s loosely sorted by the order in which we adopted
each check. While not every commit needs each point, this is a general
idea of what might be required.</p>

<ol>
<li><p>Functionality</p>

<p>This was what started us on the code review path &ndash; sometimes we
would commit something that just didn&rsquo;t work right. Occasionally,
the code wouldn&rsquo;t even compile! To try and address these problems,
we would have a coworker give the code a once-over before pushing
it. We actually started doing this long before gerrit by walking
over to another desk.</p></li>
<li><p>Function names</p>

<p>One of our explicit coding conventions is that non-static functions
must be prefixed with the module name they belong to. This keeps us
sane and helps prevent name collisions. We also have a few
conventions for constructors, destructors and other common
patterns. These are all easy to check for and was something we
started doing almost immediately.</p></li>
<li><p>Resource leaks</p>

<p>After a few annoying memory leaks got committed, we started looking
at the code with a critical eye for all kinds of leaks. Resource
leaks are easy enough to add and subsequently miss, especially in</p>

<ol type="a">
<li>Leaks are really just a special type of non-functioning code,
but one that bites you days/weeks/years later instead of
immediately.</li>
</ol>


<p>Sometimes we use a tool such as <a href="http://valgrind.org/">valgrind</a> to test for
leaks, but in many cases we just inspect the code visually. We
check to see if resources are handled consistently and pay special
attention to various error conditions.</p></li>
<li><p>Efficiency</p>

<p>For better or worse, we almost always worry about how optimized our
code is. Sometimes this is can be a valuable exercise, but in most
situations it was probably overkill. There&rsquo;s just a warm fuzzy
feeling when you catch an O(n<sup>2</sup>) algorithm that could be
O(n log n), even if you spend more time coming up with the faster
algorithm than will ever be saved in runtime. Since this focus on
optimization is part of our culture, it has found it&rsquo;s way into our
reviews.</p></li>
<li><p>Tests</p>

<p>Tests fall lower on this list than I would prefer. Unit testing C
code is, at best, hard and/or annoying. Add the fact that trying to
test code that was never designed to be tested is painful, and you
can easily see why we tend to turn a blind eye when a commit
doesn&rsquo;t add any new tests.</p></li>
<li><p>Documentation</p>

<p>Code written in C should probably have more documentation than most
other languages, simply due to all the ways you can shoot yourself
in the foot. For example, you can&rsquo;t tell if any given function will
take ownership of the pointer you pass it, that information has to
be documented somewhere. When we prefix our function names with the
module name, the descriptive part of the name is often shortened to
prevent extremely long function names. This means the function
documentation is vital to understand what the function does.</p>

<p>Reviewing documentation often comes down to checking that it makes
sense and is proper English. It&rsquo;s impressive how mangled a sentence
can get when you refactor the code it is trying to describe.</p></li>
<li><p>Coding style</p>

<p>Many different aspects of code fall into &ldquo;style&rdquo;: the contents,
formatting and spelling of comments; the names of variables, static
functions, and structures; the size and complexity of functions;
the contents of structures.</p>

<p>These stylistic issues can be difficult to talk about in a code
review, since sometimes it comes down to personal preference. The
best you can do is express your preference and try to sway the
other developer to your line of thinking. It helps if both people
realize that the code has to be read and maintained by the entire
development group.</p></li>
<li><p>Test style</p>

<p>Test code style is an entirely different kettle of fish from
production code style. A test needs to focus on how a user would
want to use the code. It should minimize the clutter required to
perform the test so as to make the test as readable as possible,
while still highlighting the interesting part under test.</p>

<p>Similar to code style, the difficulty reviewing tests comes from
differences in personal preference. This is compounded by the fact
that we are not as experienced with writing great tests yet.</p></li>
<li><p>Commit message</p>

<p>Right now, this is my holy grail of code reviews. I once spent 15
minutes skimming through the git log to determine if we had a
preferred <em>verb tense</em> in our commit messages (we did). More
reasonably, this can involve ensuring that commit messages are
capitalized consistently and that they describe why a change is
being made, not just what or how.</p></li>
</ol>


<h4>Where we are now</h4>

<p>The C project is currently somewhere around the &ldquo;Tests&rdquo; or
&ldquo;Documentation&rdquo; stages. The Java and Ruby projects expect tests and
documentation, so they hover around the &ldquo;Code style&rdquo; and &ldquo;Test style&rdquo;
stages; I&rsquo;ve only had one or two opportunities to correct someones
verb tense :&ndash;).</p>
]]></content>
  </entry>
  
</feed>
