Wednesday, May 09, 2018

Programming is easy. Debugging is hard.

I may be in a minority in this but in some ways the most fun part of my job is debugging student code. Students are very clever at wring code that doesn’t work in obscure ways. Some how they manage to create errors that look like they are in one place but are really in a completely different place.

The end of the year involves a lot of debugging. Students know more ways to do things wrong than every before. It’s a lot of fun. No, really it is.

Now I know that letting students struggle with logic errors is sometimes (often? mostly?) a good thing. On the other hand they usually lack the experience to see the possibilities. In general I find that struggling students are focused on the wrong part of their code. At some point a teacher has to step in and help out a little.

Today I had a student working on a Pong program and it was not recognizing when the ball hit a paddle. He was looking at the speed of the ball, the arrangement of the code, the working of the if statements and generally focused on a lot of different code. The problem could not be with the IntersectsWith method of the rectangle class. That is part of the system library so it must work. Right?

Maybe we are not updating the location of the rectangle in the right place and time? Maybe we’re checking in the wrong place in the code? Maybe it is a timing problem?

In the end it turns out that all of the code was correct EXCEPT that he was updating the height and width when he should have been updating the width and height. Yes, the order of the parameters matters. I’m kicking myself that I didn’t check that first but at least I did think of it eventually. Next time I will check that first.

And to think I posted a somewhat related post last week (My Code is the Same as Yours But Mine Doesn’t work ) Sigh! I should have known better.

5 comments:

Mike Zamansky said...

It doesn't get easier: https://cestlaz.github.io/posts/2015-12-17-im-an-idiot.html/

Tami Brass said...

This is my 5th year of working with 6th graders on block-based coding (and resulting debugging) with SLNova. Debugging requires noticing, attention to detail, patience, and tenacity, along with an understanding of the language being used. It can be incredibly frustrating and stressful, but it's so important to what we do. Sometimes the most simple mistakes are the most difficult to find. Peer programming and peer code review can be a huge help. Iterative design with a built in mechanism to force kids to test each change or addition is hard but helps as well. I'm thinking about having kids do periodic practice debugging activities with code I create and having them spot the error(s), but it doesn't seem as meaningful in isolation. I'd love to learn more about how others teach the process.

Alfred C Thompson II said...

Since I solved this once today, two other students had the same problem. Of course this time I was able to see it right away. :-)

Garth said...

Ain't this fun? Teaching math is so boring compared to programming. Students can only do a math problem wrong a very limited number of ways. A program has syntax, logic, and all those other things I should know and have forgotten for the moment. Sometimes you cannot find what is wrong no matter how many print statements you stuff in.

Anonymous said...

Some of my (high school) students are working on image processing (grayscale, sepia, invert, posterize...) using Python. In some cases, although the program runs without errors, the results are not correct. This is a good learning experience.