Programming Tip #1 – The “Why Does That Work!?” Effect

Hi All,

Most of you probably don’t know but I was quite early to the world of development (started at 11), and one thing I always thought would go away in time was those moments where you look at your own code and literally have NO idea why it works. It will defy everything you know to be true, break the laws of computing and sometimes even physics itself.

10 years on I still find myself looking at my own code and going “What the hell!?” and have come to realise that in 99.8% of cases (authentic and obviously non-made up statistic!) it’s 2 bugs in my code balancing each other out to make 0 bugs. Sounds great right!? Bugs fixing themselves!! Wrong! And here’s why:

This is an inevitable slippery slope between the perception of “working” and the fact that frankly you’ve coded a timebomb, and in Britain, bombs are illegal.

Just because your code sneaks past the compiler’s rigid checks or Visual Studio in all of it’s fancy gloriousness doesn’t highlight the error of your ways, doesn’t mean you’ve coded the 1st bug free piece of software in human history. If you get the “Why does that work” feeling chances are maybe even months down the line you’re going to be debugging 1 fault that is the result of a chain of bugs down the line; and that’s not pretty.

Imagine this:

private void DisplayNumber(string Number) {

try {

int intNumber = Convert.ToInt32(Number);
Interaction.MsgBox(intNumber);

}

catch {

Interaction.MsgBox(0);

}

}

obviously a very simple bug to spot, but no error is generated and the program will tell the user a number they didn’t even type in. You can imagine then if GetNumber was a function in a large program and it returned a completely different value to what the user specified but still looks as if it worked. Could be at least month before a company realises their financial department is sending out automatic invoices with the value of £0 attached to them ;).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s