Friday, February 09, 2018

The Struggle To Keep Programs Simple

Debugging code is hard. Really hard. The more complicated the code the harder it is to debug. Brian Kernighan said it well.

debugging

But oh do my students write clever code. Well, they think it is clever code. If nothing else it is complicated. It’s not really their fault though. Not completely.

Looking at student code the other day I realized that their tool set is limited. They don’t always have the tools to write code simply. I’ve blogged about this a bit before (The Complex Question of Complexity in Programming ) but it is something I keep struggling with. Many problems, like the Lights Out project in that referenced blog post, can be written in far fewer lines of code if you know about arrays. Without arrays it takes many lines of code and there are many places things go wrong. On the other hand the many lines are simple lines while the array version is more complex and has to be thought about a lot more.

Which way is better? Well that is open to debate. Plus it depends on the experience of the programmer.

It’s more than tools though. My students have been writing a project that involves a lot of if statements. They are doing the Ski Lift Ticket Project that I have blogged about before. There  are a lot of ways of constructing this solution. Some are more complicated than others. It always seems like some students want to make it very complicated. Why? I’m trying to figure that out but my working theory is planning. That and working in stages.

Some students try to handle both the age range and the resort status at the same time. For an experienced programmer this is not a big deal. For a raw beginner this can be a bridge too far. Students who first think about the age ranges and test that and then look at the resort status tend to do better. Better as in finish first and have less complicated solutions.

Too often beginners just start writing code without planning. Rather than being that fast way this is the long way. It also tends to result in more complicated code as line after line is added to handle situations that were not thought of in advance. Obviously I talk about planning all the time. And I try to model it when we work though exercises as a class. But well, kids in high school, know better. Just ask them – they’ll tell you.

I keep trying though. I also try to show them code and point out the design steps and the simplicity that makes it easier to follow. And I give them more tools as well. A lot of times a light goes off and a student says “I could have done this other thing a lot easier if I had known about that.” Yep, so keep learning new tools!

No comments: