Being Careful About Examples

Computer Science Teacher
Computer Science Teacher - Thoughts and Information from Alfred Thompson

Being Careful About Examples

Rate This
  • Comments 5

Yesterday I posted some very simple sample code. I have to admit that I spent a lot of time on some of the details but may not have spent enough on some of the other details. This is always an issue when working on sample code for demonstrating a specific, often detailed concept. This is also an issue with code for tests and quizzes BTW. I remember one of my APCS students complaining that the APCS test was all about showing bad coding examples. A bit harsh perhaps but when writing code for a test one is very constrained by space. The good news is that these examples can lead to having some great conversations with students. For example, in the case of yesterday’s code samples I received email from a teacher friend of mine calling me out. Sort of.

Should you teach students to break out of loops or to use a better loop to meet the need? In your programs today(?) you use a for loop to go through the elements until the end, but if you find a bad value break out. Shouldn’t you have used a do while loop? That way you place clear conditions for when to stop looping?

One example of the code in question is this C# example.

for (int i = 0; i < t.Length; i++)
{
    if (!Char.IsLetter(t, i))
    {
        MessageBox.Show("Alphabetic characters required");
        break;
    }
}

In that code I use a break statement to exit the loop early. Standard structured programming, as I learned in college many years ago, says that is bad. The “rule” is that a loop should have one and only one exit point. When you add more exit points you add the potential for problems with people understanding the code, maintenance issues, and unexpected consequences when programs are extended or modified in the future.

A more strictly by the rules structured programming code might me something like:

int i = 0;
 
while ( i < t.Length && Char.IsLetter(t, i))
{
    i++;
}
if (!Char.IsLetter(t, i))
{
    MessageBox.Show("Alphabetic characters required");
}

That’s probably still not the best way but it at least demonstrates the principle of one exit for the loop. This brings up the point I was making earlier about simple samples being more difficult than it may seem. The first example shows the comparison pretty clearly in a simple if statement. The second example shows the same thing but the Boolean expression in the while is more complicated then the one in if statement. Also in the second example the loop “feels” kind of forced to me. Of course often times in a real program there would be other things going on inside a loop that we are using. In a more complex “real” program the value of using the while  loop might actually be more obvious.

We could write a very complicated example that would demonstrate several concepts or features of course. The problem there is both one of focus  and potentially too much complexity making it hard for beginners to understand the code. Often then we are faced with the real trade off between great code all up and simple easy to understand samples that may not be ideal code.

When these examples sparks conversation in or outside of class be sure to take advantage of these as real learning opportunities. You can always pretend you did it on purpose. Smile



  • The better way would probably be:

    if(!t.All(Char.IsLetter))

     MessageBox.Show("Alphabetic characters required");

    But this is assuming we have LINQ like high level functions

  • The question of whether loops should have exactly one exit point is a stylistic one, and like most such stylistic issues there are competing concerns.  Using a for-with-break introduces certain risks, but using the more complicated while does also.  Which risks are greater?  I believe the risks of the more complicated while are significantly greater in this example, as evidenced by the fact that the while version throws an exception on the empty string.

  • Ouch! Not checking for a null string. I actually thought about that at one point in testing but ignored it because it did not relate to the concepts I was interested in highlighting. Of course someone should always do that.

  • The other issue with the while loop, is that you repeat yourself, writing the same thing twice, with the duplicated call to IsLetter().  If you decide later that you need to change that to IsLetterOrNumber(), you have two places to fix it.

  • I *knew* people would find flaws in the new loop. Would you beleive it was a ploy to stimulate discussion? :-)

Page 1 of 1 (5 items)