Tuesday 4 June 2013

Assuming Values of Variables is Bad - Function Parameter Lists

I've had a situation just now, which amused me, a none programmer decided to sit down and help work out some logic for a program they were testing and they worked this logic out and sent their findings back to me as some pseudo code.

Great I thought, someone actually thinking about the task at hand, the chap involved is actually champing at the bit to become a programmer I think - the fool, he has a far more interesting job as it is, and he'd earn no more money nor gain more plaudits - anyhow...

I received this missive and was immediately struck by how obvious it was this chap was not a programmer, or not a competant programmer - for he'd fallen into the same trap a none-programmer or poor programmer would have fallen into.

They had assumed the starting position of their research, assumed up was up, or the sky was blue, and us programmers know never to assume the start point of a test, never assume something is right, go take a look, add a watch, output it, if it an input know its value before you assume it, check its not null, check its in range... We just know this, its second nature if you're worth your salt.

And it was really crushing to have to reply to this chap - and I had to, I couldn't just say thanks ba-bye now, cus he's waiting for a new build from me - and I had to say "Fantastic, thanks a bunch - BUT"... and then explain he'd assumed he knows what's going on.

If he's trying to become a programmer, then good luck to him, he'll soon learn not to dance with the devil - hopefully before he make too many of these mistakes - but those poor programmers out there assuming stuff like that - I'll not forgive you like I forgave this chap...


Addendum:

Code horror, I just found this cardinal sin:

void foo (int param1, int
param2, string param3)

The breaking of the line in the middle of the list of parameters is fine, but breaking it between the type and the name... arg, argh... hurl heavy object.. NOOOOO!!!!!

I much prefer to either have short lists of params all together so you can read them:

void foo (int param1, int param2, string param3)

or if that goes off the right, or the list is much much longer, breaking the list down vertically, so it can be read off, or comments put with each parameter, thus:

void foo (
int param1, // First first
int param2, // Used to calculate Tax
string param3); // The name of a good psychiatrist

No comments:

Post a Comment