Archive for October, 2007

The editing pass

Wednesday, October 10th, 2007

I always like to think of “writing code” as similar in many ways to “writing” in general. Which is probably why http://michael-mccracken.net/wp/2007/10/09/the-editing-pass/ makes sense to me. (via Gruber).

I give paper sections and important emails a while to sit after I write them, and they always benefit from another look with fresh eyes. I think that doing this with code is worth thinking about.

Once you get a piece of code to the point where you believe it works – it’s passing its tests – go back over it and edit it. That is, go back and edit it for clarity, flow, and style. Just as if it were an essay.

I think this association between “coding” and “writing” is why so many of the best programmers I have met also did philosophy. To write a philosophical piece, you have to be able to order complicated and abstract ideas and make them concrete in a simple, understandable way.

Convenience Wins

Tuesday, October 9th, 2007

http://www.fistfulayen.com/blog/?p=127

Want radio? No problem. Click play, get radio. Want video? Awesome. Click play, get video. Want a track on-demand? Oh have we got a deal for you! If you’re on Windows XP or Vista, and you’re in North America, just download this 20MB application, go through these seven install screens, reboot your computer, go through these five setup screens, these six credit card screens, give us $160 dollars and POW! Now you can hear that song you wanted to hear…if you’re still with us. Yahoo! didn’t want to go through all these steps. The licensing dictated it.

Analogue Rights Management

Sunday, October 7th, 2007

http://blog.vagueware.com/2007/10/7/rights-managment-in-the-18th-century

You might be forgiven for thinking that it’s only since the rise of (the illegal version of) Napster a few years back that “the Establisment” has had to deal with uppity teenagers who don’t understand “the rules”.

Corporate Culture is important

Saturday, October 6th, 2007

http://www.marketwatch.com/news/story/story.aspx?guid=%7B861065D6%2D3BAE%2D4F5C%2DBF1C%2DC4B7BDE76899%7D

… these creative types felt they were caged animals under the Microsoft umbrella and it was hurting their creativity … it doesn’t bode well for Microsoft management, which showed that it can’t handle creative types.

I’ve said before that culture is vital to the health of an organisation and that an organisation’s culture stems directly from the attitudes of the people in charge.

The infamous iPhone SDK

Friday, October 5th, 2007


There appears to be a lot of gnashing of teeth regarding the non-existence of the iPhone SDK. Some are saying that it is proof that Apple are evil and out to get us. There are rumours that the iPhone is about to undergo a platform shift and his Steveness is just saving Apple the trouble of yet another compatibility layer (although they must be getting good at them now). Even more likely is the idea that the iPhone is waiting for Leopard. Gruber’s take on this is:

It’s entirely possible that Apple is committed to opening up the iPhone to development but that they aren’t yet ready, and, as per their usual policy, are keeping their mouths shut in the meantime.

This strikes me as the most likely. We all know that his Steveness is not too keen on third-party developers, even ones with as much style as Mac developers. But it is a smartphone and as such will be pretty odd without an SDK.

However, it did make me think back to one time where I knocked together a Delphi component. We needed a UI component and could not find a third-party one that looked good for the right price. So I spent three or four days building one. I think I used to cost the company approximately £200 per day – so let’s say the component cost £800. I showed it to people and they thought it looked great – it did the job it needed to and did it reasonably well. Other people in the organisation started using it and, as a result of the feedback, I enhanced it. Probably another day – putting the cost at around £1000. Then my boss came over and asked what we would need to do to sell this component. Armed with a fountain pen, a piece of paper and some blind guesswork I came up with the following:

Remove the dependency on a third-party component 5 days
Add functionality to deal with overlaps (not needed for us but will be the first feature asked for) 15 days
Documentation and User Guide 4 days
Extensive Testing 4 days
Total 28 days

In other words, my £1000 project turns into a £6600 project – 1 week turns into over a month. And these were rough estimates with a high margin of error (and, it has to be said, subject to the vagueries of my memory, several years on). Of course you would then need to chuck in support staff of some sort, as soon as you get your first sale. Where the original code did what it did and could be passed around the organisation without a worry, as soon as you get third parties involved the whole enterprise becomes a whole order of magnitude more complicated. We never did sell the component – it just wasn’t worth the effort.

I’m sure that there is an iPhone SDK waiting to be released somewhere. But I bet it is Leopard dependent and I bet it is not ready for general consumption. The whiners will just have to wait.

Nuts by sundstrom.

If wishes were iPhones

Friday, October 5th, 2007

http://diveintomark.org/archives/2007/10/04/if-wishes-were-iphones

I thought the big draw for Apple hardware was that “It Just Works.” By breaking it, you must know you’re giving up the “Just Works” factor, so what’s left? Rounded corners?

Improving your Email Support

Thursday, October 4th, 2007

http://www.userscape.com/blog/index.php/site/10_steps_to_improving_your_email_customer_service/

No matter how an email conversation ends, I always try and be the last response. Even if it’s just a simple “no problem” or “let me know if you have any other trouble”. First, I like to make sure there’s resolution in the customers mind. If I’m the last one to respond then there’s no doubt that the request is completed to the customers satisfaction. Second, it shows that you’re willing to go the extra mile. There are very few companies I’ve ever dealt with where after I sent a “thanks for fixing it” type message they responded. Those that have tend to stand out from the crowd.

A method for successful software development

Wednesday, October 3rd, 2007


I’m just thinking out loud here – based on stuff I’ve done well in the past. But this is no rigorous scientific method – just a set of notes for me to refer back to.

* Write a couple of paragraphs describing the application. Note down keywords (especially important nouns and verbs) and maybe a “what, who, why and how” type analysis (when and where may or may not be applicable, depending upon the application).
* Show this to other people and get their feedback.
* Sketch out the UI. On paper, with a 6B pencil and an eraser, or on a whiteboard, with a camera. Do a flowchart for each of your major tasks (look at the verbs from before) and then a quick sketch of the pages involved. As you sketch out the pages you will probably find that you need to change the flowcharts. Think long and hard about it and then change if you must.
* Show this to other people and get their feedback.
* Create a new project and add it into subversion.
* Take your flowcharts and decide which controllers will be involved in each task. [I'm not sure about this point - but I reckon you probably don't want a single controller to be involved in more than one task - however each task will probably require many controllers].
* Generate the controllers and then use them to create static HTML/CSS mockups of your UI. The only “active” portion of these mockups should be links navigating through the task. As you do the mockups you will probably think of new functionality – add these to your flowchart immediately – if it becomes apparent when you have actual pages in front of you it’s probably valid (as opposed to the “up in the air” additions above).
* Get your mockups to respond to a parameter: mockup_style; if this is blank then show the page, if this is “empty” then show the “blank slate” page, if this is “error” then show the “error” page. In other words, think about how it looks in those three important states up-front.
* Show these mockups to people and get their feedback.
* Write functional tests for your controllers. Deal with things like showing the blank slate page when there is no data to display (I like to use assert_select to search for a div called “whatever_blank_slate”).
* Dealing with each controller in turn, get the functional tests to pass. In order to do this, you will probably have to create models. As you create the model add unit tests and test fixtures – and then use these to make the functional tests actually work with real data in a real database.
* Do the bare minimum to the models, fixtures and unit tests to get the functional tests to pass. Even if you know the next controller will need a new method adding to the model, don’t add it in early. You never know – your boss may change his mind and decide that that function is not needed (or more likely, can wait till version 1.1) – in which case, why make things complicated before it is needed?
* Whenever you find yourself writing code that is similar to code elsewhere within the project stop and refactor it into a helper, a method on the controller, a base (inheritance) model class or a mixin module. Don’t Repeat Yourself.
* Related to the above, put business functionality into the models (unit tested) – for example if your view needs to show the last four invoices but one, add a method to your customer class called “last_four_invoices_but_one”. Don’t get your controller to grab all the invoices and slice the array itself.
* Once all your functional test passes, run the app in at least two (preferably three) browsers. In theory, if your functional tests are any good, you should see pretty much a working task. Any bugs you find should be reproduced in the tests, if at all possible, and then fixed.
* Show this to people and get their feedback.
* Refactor your controller and models to tidy them up. Remember you have very high test coverage so you should not break anything without being alerted to the problem.
* Move on to the next functional test.
* When they are all done – you should have a working application!
* When the inevitable changes requests come in, you should have full coverage of unit and functional tests, making change management easier than it would be without those tests.

As I say this is thinking out loud – I’ve never done all of these at the same time – but I’m certainly planning on following this process on one of my own projects in the near future.

Gears by csotelo.

Single Table Inheritance in Ruby on Rails

Tuesday, October 2nd, 2007


I remember years ago, when Object-Orientated Programming became fashionable, every single text on it (at least those that I could be bothered to read) repeated the mantra “OO is about inheritance”. Of course, that’s rubbish, but when you’ve been dealing with structs in C or Cobol it’s probably an easy way of thinking of things – objects are these data structures with these extra bits.

Nowadays, I rarely use much inheritance, beyond extending what my framework gives me. And I find myself using it much more in statically-typed (compile-time type checked) languages. I think splitting my functionality into a web of tiny objects gives me much more flexibility (as opposed to ending up with a couple of huge objects with layers of functionality added through generations of inheritance). Think of a chain of people – you ask a question of A, who in turn asks her friend B, who in turn asks his boss C, who in turn asks his wife D. In a different situation you may ask the question of B who gets the answer off A. Each of A, B, C and D is small and simple with a tiny public interface. Compare that to asking every question of an ABCD amalgamation – large and unwieldy with a large public interface.

Of course, Ruby does allow inheritance. It’s not always needed for reuse as you have mixins, but there are times when it is useful. And Rails lets you use inheritance in your models – it’s not always needed as, for joining disparate classes together you can use polymorphic associations, but there are times when it is useful.

So now I’ve tried to warn you off, how do you do it?

Imagine an address book application. You have basic AddressBookEntries which fall into two categories – People and Companies. This is an excellent candidate for inheritance as there is a fair amount of stuff shared between People and Companies (name, address, telephone number) but also some different stuff (People belong to Companies, Companies have a list of People and a VAT (tax) number).

To start with you create a model – AddressBookEntry.

Your migration may look like:

create_table :address_book_entries do | t |  # system fields  t.column :created_on, :datetime  t.column :updated_on, :datetime  t.column :lock_version, :integer, :default => 0  t.column :type, :string  # common fields  t.column :name, :string  t.column :address, :text  t.column :telephone, :string  # person specific fields  t.column :company_id, :integer  # company specific fields  t.column :vat_number, :stringend

Note that we have split the fields into different sections – system fields, common fields, person fields and company fields. Not strictly necessary but a nice to have.

Our model looks like this:

class AddressBookEntry < ActiveRecord::Base

end

But where are our people and companies?

Two new model files – person.rb and company.rb are needed.

# person.rbclass Person < AddressBookEntry  belongs_to :companyend# company.rbclass Company < AddressBookEntry  has_many :peopleend

There you go. All done (apart from the unit tests).

So how do you use this?

Simple – create a company and a person.

tiny_co = Company.create :name => 'Tiny Co', :address => 'Tiny Towers', :telephone => '4321', :vat_number => '987654321'

dave = tiny_co.people.create :name => 'Dave', :address => '22 Acacia Avenue', :telephone => '1234'

If we now look at our table we will see the following (some fields omitted because I’m lazy):

id type name company_id
1 Company Tiny Co null
2 Person Dave 1

The key is the “type” field we added. Rails treats this as one of its special “magic” fields and when you create an instance of Person or Company, Rails fills it out for you. Likewise, if you call AddressBookEntry.find(1) it will return you an instance of Company (not AddressBookEntry) – it uses the type to govern what it instantiates.

This means that Rails hides most of the mechanics of inheritance from you – you just write your models and they automagically do the right thing. There are some issues – your fixtures have to contain all descendants in a single file (in this case address_book_entries.yml) and it is perfectly legal to write dave.vat_number = '54321' (as the vat_number field, which technically belongs to a Company, not a Person is still accessible to all AddressBookEntries).

But Rails has made inheritance in a database about as easy as I can imagine. There is a little bit more to inheritance – with ActiveRecord abstract classes, but that’s a story for another day.

Photo by spektator