The Specification is the Documentation Part Two

Posted by Rahoul Baruah on August 5th, 2008 under Beautiful Code, Designing Great Software, Ruby on Rails and Software Development, Writing Reliable, Bug-Free Code  •  No Comments

Two (related) thoughts on “The Specification is the Documentation“.

One of the things that I like to do, when developing, is to start with a sketch (you know, with 95g/m2 paper and a 6B pencil) of how the UI will look. There are two reasons for this. Firstly, it helps communications with the client - they can see something concrete, while it’s also blatantly apparent that there’s a long way to go yet. Secondly, it puts me in the position of starting from the ideal position and working “downwards” to what is possible, rather than starting from what is easy and working “upwards” to what is nice. In other words it forces me to raise my standards.

Writing my specifications with the helper methods as described is the coding equivalent. I start with a high-level, abstract, description of the problem I am trying to solve (”given a logged in user and three gizmos, expect the gizmos to be tagged when I go to the ‘tag-gizmos’ page”). I then work on setting up the environment (implementing the helpers) and the getting the specification to pass (implementing the code). Again, I start with the ideal position and work downwards as I implement.

The second thing is that it is one of those techniques where I am sure that I am along the right lines. How can I tell? Because as soon as I wrote my first specification in this way it “felt right”. After I had written a couple more I wanted to go back to every piece of code I had ever written and rewrite it all in this new way.

The Specification is the Documentation

Posted by Rahoul Baruah on August 1st, 2008 under Beautiful Code, Designing Great Software, Ruby on Rails and Software Development, Writing Reliable, Bug-Free Code  •  1 Comment

In a former life I used to write “functional specifications”. These were long, dense, hard-to-read documents that detailed what an application (not yet written) was supposed to do. I would spend (literally) weeks typing these things up, the customer would read it, think they understand and I would quote them based upon the document. And then the project would go over budget as all the tiny subtle details became apparent about two weeks before deadline day. But, even worse, the document would slowly become out of date, as changes were made in response to feedback - while the spec stayed unchanged.

As you might expect, this lead to general disillusionment with functional specifications. What’s the point if they didn’t help with the budget and didn’t reflect the actual application?

But when I read about Test-Driven Development the key thing that struck me was the tests became a living embodiment of what the application is supposed to do. They are a specification; but not only that, they are a specification that has to remain up to date.

Only one problem. They are written in code. Making them meaningless to the client. In fact, often, some tests were so obscure they were only meaningful to me; as I was writing them. Come back to it six months later and try and figure out why that change has made it fail? No idea.

Which is why RSpec and its Stories are so exciting. A Story is a text document that describes the required behaviour of the application. Read that again. A text document; written in English. So your clients can read them. Can help to write them. You then supply some Ruby code, that matches the sentences in your stories and associates them with code. That code is run, testing your application and proves that it does what it is supposed to (providing you’ve written your test code correctly of course).

Story: measure progress towards registration goals
  As a conference organizer
  I want to see a report of registrations
  So that I can measure progress towards registration goals
  Scenario: one registration shows as 1%
    Given a goal of 200 registrations
    When 1 attendee registers
    Then the goal should be 1% achieved 

  Scenario: one registration less than the goal shows as 99%
    Given a goal of 200 registrations
    When 199 attendees register
    Then the goal should be 99% achieved

Now, I have to admit, I’ve not used RSpec Stories in anger yet. But it has had a strong effect on my “unit” tests.

The difference between stories and unit tests are that stories test your full stack. Go to ‘/’, fill out the text fields, click the submit button, it should insert into the database successfully and then show these three records on the ‘/whatever’ page. Unit tests will check that the form is shown when you go to ‘/’. A separate test will check that your record can be inserted into the database. Another test will prove that ‘/whatever’ asks the database for three records.

But, having read about Stories, the way I write my unit tests have changed. Check this controller specification out:

describe ReportsController, "at the admin site" do
  it "should show this month by default" do
    given_the_admin_site
    given_a_system_user

    when_getting :index do

      expect_to_find_orders_for Date.today

    end

    assigns[:date].should be_today
    assigns[:orders].should == @orders
  end
end

Not quite plain English. But, when it comes to maintenance, using given, expect and when as prefixes for your helpers makes a world of difference.

 

UPDATE: now using block syntax around the “when_getting” statement

How Ruby on Rails can save you money …

Posted by Rahoul Baruah on July 31st, 2008 under General  •  1 Comment

Well not quite.

Instead, it’s a (biased) look at how using Rails for your next project can give you a better return on investment.

Basically, your major costs are your team and your servers (and you definitely get what you pay for when it comes to your development team).

Everything else tends to zero.

In praise of the Dock

Posted by Rahoul Baruah on July 29th, 2008 under Designing Great Software  •  No Comments

I personally can’t stand the Dock on Apple’s Mac OS X. The Dock (shown vertically) It doesn’t feel right to me, it doesn’t hold enough information nor does it display the information it does have correctly. And I feel like it is stealing screen space from me (and “auto-hide” is just a crappy hack that feels even worse).

But, ages ago, as a “thought experiment” I decided to design my own “computer user interface” from scratch. Something that non-computer users would understand. You see, to people like me, things like “double-clicking” and “holding the Control key while pressing S” are easy. To some people, it’s not that they’re difficult, it’s just that they’re more difficult than they need to be. Hence so many people single-click when they should be double-click and double-click when they should single-click. Hence so many people use the Caps Lock key, instead of holding down Shift, to type in Capitals.

So, what would an interface for these “non-expert” users look like? A computer is a multi-function device, so I tried to think of a physical multi-function device. The one that came to mind was a stereo. A range of different functions (”tuner, phono, cd, tape” erm I’m showing my age now). So you have a row of buttons. Click a button (once) and the stereo’s function changes.

If you were to model that then the perfect “non-computer” interface would have a row of big, clear, buttons that you click once. When you do that, the computer’s function changes.

And that’s when it hit me. That’s the Dock in OS X (or more correctly NeXTStep). Click an application’s button (once) and all that applications windows are brought to the front. It becomes the computer’s primary function. A simple model with simple interaction, perfect for the user that’s just starting out or lacking in confidence.

Guess what? It’s the obligatory iPhone post

Posted by Rahoul Baruah on July 22nd, 2008 under General  •  No Comments

Written using the WordPress application for the iPhone.

Quick summary: the 3G is great (especially as EDGE coverage was patchy at best), I’ve not noticed the GPS but the AppStore makes it.

Suddenly Safari is no longer the most used application on my phone.

Should you buy one? It’s both the best and worst phone I’ve ever used. Worst because it has no MMS (essential if you have non-geek friends), it is crippled with regards to ring tones and you can’t use it as a modem for your laptop. But it’s also the only phone I actually WANT to use. In fact it’s rarely in my pocket for more than an hour at a time.

With User Experience, the tiny details make a good app great

Posted by Rahoul Baruah on July 12th, 2008 under General  •  No Comments

NetNewsWire for the iPhone is great, but has a flaw that spoil it.

NetNewsWire for the iPhone

The User Experience is all wrong when you read an article.

Your eye starts at the top of the page, you scan down the page to read the preview, then you have to go all the way back to the top to tap the title, in order to view the full article. To make things worse, you then need to scan all the way back down to see the progress indicator. I don’t mind about the “Open in Safari” button in the top right - that’s an exceptional use-case, not the norm. But to have your eyes jumping up and down while in the regular flow of usage is not good.

The fix is simple - add a button, in the footer, for viewing the full article (replacing the “tap the title to read the full article”). So your eyes make a single movement, top to bottom, ending on the progress indicator in the bottom-right.

Setting up a mock object to test a :dependent => :destroy association in RSpec and Rails

Posted by Rahoul Baruah on July 10th, 2008 under Beautiful Code, Ruby on Rails and Software Development, Writing Reliable, Bug-Free Code  •  No Comments

One of the great advantages of using mock objects to test and specify your objects is that you concentrate solely on the thing you are testing.  

If you weren’t using mocks to tests that a controller re-shows the “new” form if given an invalid object, you would do post :create, :model => { … } where … is a set of fields that are invalid. This means that, when writing your spec, you have to remember what it is that makes that model invalid. This also means that, every time you change that model, and its rules for validity, you potentially have to amend the controller test as well. In other words, you are not actually testing the controller in isolation, you are also testing the model.

Using mocks lets you write the following:


  @model = mock Model
  Model.should_receive(:new).and_return(@model)
  @model.should_receive(:save).and_return(false)
  post :create, :model => { :some => :fields }
  response.should render_template('/models/new')

In other words, it doesn’t matter what parameters you send to create. The Model (class) will return you a mock instance of model, and the mock instance will return false on save (you can actually get save! to raise an ActiveRecord::RecordInvalid - but I’ve had some difficulties with that). In other words, the real model is no longer part of the test and you are concentrating on the behaviour of the controller alone.

This concentrating on what is important is a vital advantage when using mocks.

But how does this work on testing models?

I wanted to test that a default value was copied from one field to another under certain circumstances. So I set up a method, called set_default_value and wrote some specs to ensure that it was working. Then I wrote the following:


  it "should set the default value before validation" do
    @model = Model.new
    @model.should_receive(:set_default_value)
    @model.valid?
  end

This failed, as it should do. Then I set a before_validation :set_default_value on the model and the spec passed. Each spec concentrates purely on what is important - a couple to show that set_default_value works under different circumstances, and one to show that it is called when it is supposed to be.

What about testing a :dependent => :destroy association?

Unfortunately, that can’t be done without saving at least one of the objects in the association (which means knowing enough about it to make it valid). But, as David Chelimsky (Mister RSpec) points out on the RSpec mailing list, you can do it using mock objects for part of the association. Which was a relief as my “child” object was complex with a whole set of interdependencies of its own.

To DRY or not DRY?

Posted by Rahoul Baruah on July 9th, 2008 under Beautiful Code, Ruby on Rails and Software Development, Writing Reliable, Bug-Free Code  •  1 Comment

A very interesting article about how DRY you should be in your specs.  

http://lindsaar.net/2008/6/24/tip-24-being-clever-in-specs-is-for-dummies

Personally I agree with everything said.  Readability comes first, even at the expense of efficiency and DRY; “be nice to those who have to maintain the code”.  The really interesting thing though is the example is actually quite DRY - it’s more about how you organise and order the code, more than repeating yourself.  

The Funny World of Advertising

Posted by Rahoul Baruah on July 9th, 2008 under General  •  No Comments

I saw the “Your life on your Blackberry” ad the other day.  Obviously designed to promote the Blackberry as a consumer device (because of the iPhone?) my first thought was “that’s the first Microsoft ad I’ve seen in ages”.  My wife’s first thought was “how dated was that?”.  

 

Let’s hope Microsoft do better with their upcoming promotion for Windows Vista.  

User Interface design for the iPhone

Posted by Rahoul Baruah on July 4th, 2008 under General  •  No Comments

John Gruber finds a great example of iPhone UI design. Sometimes you
have to wonder what goes through people’s heads.

http://www.flickr.com/photos/gruber/2635257578/