Friday, September 28, 2012

Find The Bug

The big problem I have with using professional quality sample code in class is that it is too professional. By that what I mean is that it is too complete and often too complicated for beginners to use. Oh it is great as an example to read and study for sure. On the other hand the complexity can be overwhelming to the point where beginners find it difficult to use or to extrapolate from the specific example to more general usage. This is not a problem for experienced programmers (usually) but can make it difficult to use with students who are just starting out.
For those reasons a lot of sample code for beginners is over simplified. This can mean problems as well. One problem is that if beginners see enough code without error handling they tend not to include it in their own projects.
What I like for sample code is very simple code that is easy to understand but that lends itself to easy expansion and improvement. I want it to be the start for experimentation. Recently I found such a sample on Code Project.
Create Pie Chart Using Graphics in C# .NET - hari19113
This article shows how to create a Pie chart using the Graphics class in C#.
It’s a simple C# project and even though it is not commented it should be easy for most beginners to understand. In fact I think it is easy to expand and the routines in it could be fairly easily moved to other projects. But it is far from perfect. There are some coding practices that are less than ideal. Specifically a) some variables that should not have as large a scope as they do and b) a bug that is not unrelated to that variable scope.
Here is what happens. If you add a number of numbers to the sample program and press the View button you get a nice little pie chart like this:
PieChart1
Then you realize that you want a fourth number in the chart so you add it and press View again. You will get something like this:
PieChart2
Something is clearly wrong here. What could it be? I gave you a hint earlier. The example code can be obtained on Code Project or from my own website at www.acthompson.net/PieChart.zip  I found two slightly different ways to fix this bug. One way involved adding a line while the other involved moving a line. I tend to prefer the second but I’ll leave it to the reader to find what works best for them. [Insert evil laugh here]
BTW I used Visual C# Express 2010 which is a free download. Students of Java should have little trouble trying this “exercise.”

No comments:

Post a Comment