Friday, August 28, 2009

order matters

When we code, we constantly have to create instances and set their parameters. How should we order statements when we're creating several instances of the same type? Should we create the instances first and then set their parameters?


var mercury : Planet = new Planet();
var venus : Planet = new Planet();
var earth : Planet = new Planet();
var mars : Planet = new Planet();

mercury.moons = 0;
venus.moons = 0;
earth.moons = 1;
mars.moons = 2;


Or should we group together each instance's creation and settings?


var mercury : Planet = new Planet();
mercury.moons = 0;

var venus : Planet = new Planet();
venus.moons = 0;

var earth : Planet = new Planet();
earth.moons = 1;

var mars : Planet = new Planet();
mars.moons = 2;


Or should we group together constructor calls and settings but leave variable declarations separate:


var mercury : Planet;
var venus : Planet;
var earth : Planet;
var mars : Planet;

mercury = new Planet();
mercury.moons = 0;

venus = new Planet();
venus.moons = 0;

earth = new Planet();
earth.moons = 1;

mars = new Planet();
mars.moons = 2;


I prefer this last approach. It's easiest to read and it groups together the parts of the code I'm most likely to change in tandem. But it's not always possible:


var planets : Array = new [ new Planet(), new Planet(), new Planet(), new Planet() ];

for each ( var planet : Planet in planets ) planet.moons = Math.floor( Math.random() * 10 );


Also, I'd consider a different type of ordering if parameters values of one instance are related to parameter values of another instance:


var mercury : Planet;
var venus : Planet;
var earth : Planet;
var mars : Planet;

mercury = new Planet();
mercury.moons = 0;

venus = new Planet();
venus.moons = 0;

earth = new Planet();
earth.moons = 1;

mars = new Planet();
mars.moons = 2;

mercury.distanceFromSun = 30000000;
venus.distanceFromSun = mercury.distanceFromSun + 30000000;
earth.distanceFromSun = venus.distanceFromSun + 30000000;
mars.distanceFromSun = earth.distanceFromSun + 50000000;


As always, we should remember that the primary job of code is to communicate our intentions to other programmers (sometimes the "other programmer" is the original programmer two months in the future, when he's forgotten his original intentions). So we can often make ordering decisions by thinking about the story we're trying to tell. Compare this ...

"Once upon a time there lived a beautiful princess, an evil dragon, and a brave knight. The princes was 16 years old; the dragon was 200 years old; the night was 28 years old. The princess had blonde hair; the dragon was bald; the knight had red hair. One day, the princess wandered down to the lake. The dragon liked to eat peasants. The knight fought in many tournaments."

... with ...

"Once upon a time there lived a beautiful princess. She was 16 years old and had blond hair. One day she wandered down to the lake. There was also an evil dragon. He was 200 years old, bald, and he liked to eat peasants. Also, there was a brave knight who was 28 years old. He had red hair, and he fought in many tournaments."

There's no right answer. What's important is to think each case through and decide which approach best communicates your intentions.

(Thanks Tim for sparking this topic.)

No comments:

Post a Comment