Once upon a time in a land, far, far away there were 2 developers, Bryden and Tom. As they were walking through the forest they happened upon a dragon. The dragon told them that if they could answer 1 question correctly he would not turn them into silicon chips for his dinner. His question was this. If I have 2 armies one with x tasty soldiers and one with y tasty soldiers and z edible ones. There may be an undefined number of tasty soldiers in the second army if so treat that as 0. What number do you get if you multiply the size of the 2 armies together?
Bryden being a developer and having his trusty laptop with him, got to work writing some code to find the answer. He created 3 edit boxes a button and a label. The code looked like this.
Buttonclicked()
{
Label.intval = combine(editx.intval, edity.intval ?? 0 + editz.intval);
}
Combine (value1,value2)
{
return value1 * value2;
}
Tom being a clever developer said, let’s test this before we answer the dragon. He found it did not give the correct answer. So where was the problem. After much wailing and gnashing of teeth, the 2 developers finally identified the issue. But how did they achieve this mighty feat? They employed the subtle arts of debugging. They inspected the code, and still couldn’t see the problem. Next they started thinking about how to break the problem down. So the next step was to run the code and have a breakpoint. Once the breakpoint was hit, they inspected all the values read from the edit boxes. So far so good. So next they stepped into the Combine method and checked the parameters to that method. Hmm, value 2 was not what was expected. It looked like it only contained editz.integervalue, how could that be.
Oh, operator precedence… Ah that makes sense they said and corrected the code.
As a result they lived happily ever after.
But even better Bryden and Tom remembered the key points about debugging.
- Work out how to reproduce the problem.
- Break the problem up, presented with a bug in a large body of code, finding it is hard. Find the part where things go wrong.
- Be aware what values variables should have and check them as you debug.
- When debugging follow the logic flow through each method checking it goes through the logic branches you expect.
I find it amazing how often I forget these 4 key things. Then I relax and remember to work through these and then the solution just presents itself.