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/

Microsoft Equipt

Posted by Rahoul Baruah on July 3rd, 2008 under General  •  No Comments

Maybe it’s just me but I think Microsoft’s branding is awful. “Equipt”
means you are getting two unrelated products with names totally unlike
the one on the box you are actually buying.

Whuffie

Posted by Rahoul Baruah on June 13th, 2008 under General  •  No Comments

Tara Hunt’s presentation from Fuel is worth a look - not short, but an excellent overview of how community-based networking works.  

 

http://www.slideshare.net/missrogue/making-whuffie

The Key to Software Project Success

Posted by Rahoul Baruah on June 13th, 2008 under Managing Successful Projects  •  No Comments

It’s not your technical ability.  That’s a given, otherwise you wouldn’t be in this job would you?

It’s not finding a push-over client.  The client is busy and has their own problems to solve.  

It’s a combination of organisation, management and communication.  

Stay organised.  Always know what is outstanding, who is doing it and when it will be done.  

Manage your client.  Software projects are strange in that so much of the process is intangible until final delivery.  So manage your client.  Guide them to where you need them to be.  Make it easier for them to be a good client than a bad one.  It won’t always work - sometimes there are clients who simply don’t fit - but for the most part it’s your job to keep them on the straight and narrow.  

Talk to them.  Always keep them up to date.  Respond to questions as quickly as possible - at least within a day.  Even if the answer is “I’m a bit busy at the moment but I’ll get back to you before the end of the week”.  With all these intangibles flying around the client looks to you to keep them grounded.  

A perfect example of this is a recent job I have been working on.  My client was a marketing agency.  Their client was a small business - we mainly dealt with the MD and Head of Marketing.  There was also a couple of people from an SEO involved.  

For some reason every job I did for them seemed to take a lot longer than the estimate.  I got the feeling that, despite the eventual, fantastic, results, there was a lingering undercurrent.  So I persuaded them to organise the project through good old Basecamp.  

And the reasons for the delays became immediately apparent to everyone, from the SEOs to the MD.  What would start out as 8 To-Do items assigned to me quickly became 30-odd To-Do items, assigned to everyone else involved - as questions were raised, slight changes in direction were needed and designs were reworked.  

With all this stuff out in the open the next phase of the project went much more smoothly than the earlier ones - because I managed the client into organising things through a better channel for communication.  

Because, the way I look at it is, at the end of the day, it’s your job, your client and your responsibility to keep things ticking over.  Don’t you think?  

Ruby on Rails Basics

Posted by Rahoul Baruah on May 21st, 2008 under Beautiful Code, Designing Great Software, Managing Successful Projects, Ruby on Rails and Software Development, Writing Reliable, Bug-Free Code  •  No Comments

Sometimes, it’s worth stating the basics for all to see:

  • Follow the Model-View-Controller paradigm.  In particular, your views house your user-interface, your models handle the application and your controllers mediate between the two.  Controllers do not contain rules, conditionals dealing with business conditions, queries looking for objects related to the one in question.  All those things belong in the models.  
  • Use ActiveRecord conventions at all times.  It may not be the most efficient thing in the world but for most of you out there, it doesn’t matter.  
  • Use ActiveRecord and migrations to define and manage your database.  All your business related stuff stays in one place - the models - including defining relations (associations), validations and rules.  In particular, ActiveRecord associations makes some tasks easy (very simple many to many relations) and gives you things (such as polymorphic associations) that you can’t safely define using straightforward SQL.  With judicious use of validations you can ensure that the data stays safe.  
  • Use RESTful designs in your controllers.  This means that your controller very rarely grows beyond seven actions (index, show, new, create, edit, show, destroy) - and RESTful routing ensures that GETs and POSTs go to the right places.  If you need to extend things then add a sub-controller.  For example, if you were transferring an employee to another, you could have a route: /employees/1 for the employee him/herself, /employees/1/transfers/new to let the user define which company the employee is moving to and /employees/1/transfers/create to actually do the transfer.  And if you’ve defined your model correctly, the implementation of /employees/1/transfers/create should be as simple as @employee.transfer_to(@company)
  • Write your tests (or specs) first.  One - this gives you a series of small wins, which is good for your self-esteem.  Two - it helps to clarify your thinking.  ”I need to write something that does X and Y, resulting in Z … oh hang on, do I really need Y?  What about U and V?”
  • Use mock objects when testing your controllers.  Why bother coming up with lots of test data when all you really need to say is “if I the model saves correctly then redirect to X, if it doesn’t save then show Y”.  In the “transferring employees” example above, you only need to test that the controller calls “transfer_to” on your model.  Of course, your model will have already tested that transfer_to does what is expected of it.  
  • If you find yourself repeating a bit of code, or writing something similar then STOP and refactor.  Add methods to the application controller, create a new controller descendant, stick it in a view helper, add a module to your models, write a partial (and if it needs parameters, write a helper that takes a set of parameters and does the call to render :partial for you)
  • Did I say write tests and specs first?
  • Oh, and if it starts getting complicated you’re almost definitely doing it wrong.