Monday 25 January 2010

Magic Numbers

int _health = 100;
_health -= 5;

What do you see?

Firstly, If you have no idea what the preceding lines mean, you're probably better off not reading the rest of this post as it won't mean a whole lot to you. I'd go and check facebook if I were you, I am sure something more interesting is going on over there anyway, however if you change your mind you are welcome to stay.

If you do understand what on earth I am going on about (Greeting fellow nerd), and you see a perfectly acceptable line of code, please get out of my sight and write a console application that prints the line 'I am sorry for my hacking' 100 times. I'm kidding, please don't do that, but please do read on to see why you are quite mistaken.

What you should in fact see is a terrifying example of hacky code. The above line, while maybe, (and I mean maaaaybe) acceptable in a program a few hundred lines long, it is in fact the bane of a programmer's existence when the program begins increasing in size and complexity.

What the '5' represents in that line of code is a magic number, an arbitrary value that can easily and accidentally be changed (often with disastrous consequences), or prove extremely difficult to change if there are lots of them (I'll come to that later). In your programs you should never have values like this appearing in your code. When ever you see a value like this it should scream at you with the sound of an opera singer's wail after a grand piano has just been dropped on their toe.

Whenever you see a value like this, you should immediately think exactly what it is meant to be representing, and then replace it with a variable!

Variables are fantastic things and should be used as much as possible. What they allow you to do is represent a type, be that an integer, a floating point value, or one of your own classes, with a piece of text, that you the programmer can easily process and understand. As the name is associated with a type you know exactly what kind of information it represents too. So for example, an improvement on our previous example is:

int _health = 100;
int _damage = 5;
_health -= _damage;

Immediately it is clearer what is happening, but that really isn't the best part. Imagine if damage appears in multiple places all over your program, suppose you then decide that you want the damage to be '10', not '5' any more (I told you I'd come to it later). If you were not using variables you would have to search all over the program to find every context where '5' was being used to change the health, and updates it's value to '10'. This can easily lead to bugs, and take an inordinate amount of time finding every '5'. If instead you had used _damage, you need only change that value once where it is declared, and all the other values will be updated accordingly.

We can also declare variables with the const keyword if you do not want the value to change. const tells the compiler that the value cannot change after the variable has been defined. This stops you the programmer from accidentally changing the value later in the program, possibly introducing more bugs into your code.

There are many more reasons for why variables are useful, and a myriad of examples, but I won't go into them just now. I hope you found my rant vaguely informative, if not mildly entertaining. I am now going to double check my code and I fear I'll eat my words when I see all the magic numbers staring back at me.

4 comments:

Unknown said...

Congrats you are well on your way to passing Programming 1, Rob Miles will be please.

I would say you are well on your way to becoming a grumpy of g..i..ee..t..k (one of the two) but I agree with you, it is very annoying to read someones code with lazy values like this.

another one I learn resently is

string line;
if(line == ""){}

should be written for readablity as

if(line == string.empty){}

making it clearer.

tomhhh said...

lol true, it is just such an easy thing to forget even now, especially when you are trying to do something quickly, and then you forget to go back and change it. Ah well I wanted to write something and not revise :)

Unknown said...

Why do you leave spaces after opening and before closing brackets? It's maddeningly annoying :P Also, some typos:

"wale" should be "wail".
"programmers'" should be "programmer's"
No "of" after "myriad".
"When ever" should simply be "whenever"

*breathes*. My inner grammar Nazi is now slightly calmer :P

Nice post though, variables ftw. I'm starting to wish I'd done CS instead of chemistry :(

tomhhh said...

Sorry man I will work on the spelling, it bugs me too, I'll edit it now. You totally should have done CS by the way ;)