Luke Smith Write today, wrong tomorrow

Shouldn’t debugging be taught?

I got home from work this evening and two of my housemates were having coding issues
with their projects, both of which were schoolboy errors which a little bit of debugging
would solve. However neither housemate could fix their problems.

One was with C# code similar to below

ArrayList
al1 = new ArrayList();
ArrayList al2 = new ArrayList();

foreach (object bob in somecollection)
{
al2.Clear();
al2.Add(bob);
al1.Add(al2);
}

>

He couldn't figure out why each item in al1 always contained the same values.
What was happening was each time the foreach loop looped al2 kept the same reference
to memory, but always cleared its contents so the final value of al2 always contained
the last bob in somecollection. And because when you add an object to an ArrayList
you actually add a reference to that objects memory each al2 being added to the
list always pointed to the same memory. What he needed to do was create a new instance
of al2 inside the loop and so have its own place in memory.

He had tried a number of hacks to get it to work, without success. Hacking isn't always
the way to get something to work, actually knowing how to use the debugging tools
is. By tracing the steps through the loop he could have seen what was happening each
time, noticing that when al2 was cleared every item in al1 was also cleared (kinda
gives it away there thats the culprit).

The second problem involved decoding a byte to a string, of length 30. If the actual
string was less than 30 characters the resulting string contained '' (NULL) characters
to make up a length of 30. He was then trying to add two strings together (s3 = s1
+ s2) and displaying the output, but only s1 was ever being shown! Once again using
the debugging tools, he was just outputing s1 and s2 separately which resulting in
both showing the correct strings, would have shown that the two variables s1 and s2
contained a number of NULL characters at the end.

I went through university having never been shown about the debugging tools available,
and how to use them to solve simple and complex problems. It was only through alot
of non university projects, both at work and in the Imagine Cup, that I discovered
how to properly use the debugging tools which took extra time which in the workplace
= $$$. Shouldn't at least the basics of how to properly debug your code be covered
at university? Give students a VERY buggy application for coursework and ask them
to submit the fixed code with reasons why they changed certain parts of the code and
how they discovered that was a bug and what tools they used to discover it? Development
isn't just about writing code but also about fixing it!

One of my first biggest challenges was debugging C++ code that dealt with EP1
and EPG teletext files. Out came the old hex editors and asciitable with
alot of tracing through the functions to figure out what bytes were being
corrupted and where.

comments powered by Disqus