Journal entry

DRY variables

The concept of DRY code is simple, Don’t Repeat Yourself. So if you look at some code you’ve just written and notice it’s looking a bit moist, AKA the same stuff repeating over and over, there’s a good chance it’s an opportunity to refactor.

This is also true with variable declaration. Take the this example:

var umbrella = 'open';
var raincoat = 'on';
var gumboots = 'on';
var music = 'Singing in the rain';
var action = 'Jumping in puddles like a man';
var danceMoves = 'The Swim';

Notice how when declaring loads of variables we tend to repeat var a lot? Not very DRY now is it? Let’s put a towel over it:

var umbrella = 'Closed',
  raincoat = 'off',
  gumboots = 'off',
  music = 'Walking on sunshine',
  action = 'slip, slop, slap',
  danceMoves = 'The twist';

Small difference’s like this add up over the course of a project so it’s best to make these your default habits when you’re a beginner.