Learning To Write By Reading

One of the most useful learning experiences I have had was working with editors. These talented people read my prose and make corrections, suggestions, edits, deletions and make some very helpful comments. Over time this has improved my writing quite a bit. Though of course it could still be a lot better. At the same time reading (or editing) other people’s work has also been a good learning experience. Somehow it is easier to see the run on sentences, mismatched tenses in other people’s writing than in ones own. Conversely it is also rewarding to see the clever turn of a phrase, good use of alliteration and other writing practices as other people use them. Over and over I have heard professional writers say/write that if you want to become a good writer you have to read a lot.

The same is true of writing computer software. I know I keep coming back to suggesting code reviews and having students read other people’s code on my blog but I really do believe in it. Closely related I am becoming more and more convinced that reading and modifying code should be more widely used as a teaching/learning tool. This all hit me specifically recently in the middle of a demo I was doing.

I was creating Pong using XNA as I have done many times before. (The demo script and sample code can be found via How To Create A Pong Game in XNA) Now I didn’t write this code. My good friend Sam Stokes wrote it. And to be fair the code was optimized for ease of demo and minimal code use not for demonstrating good practices. But a side benefit (just as if we planned it :-)) is that it has good potential as an “editing” exercise. Take for example this sample:


   1: class Paddle
   2: {
   3:     //position of paddle
   4:     public Point pos;
   5:     public int speed;
   7:     //constructor - position
   8:     public Paddle(int x, int y)
   9:     {
  10:         pos = new Point(x, y);
  11:         speed = 6;
  12:     }
  13: }

Look at lines 4 and 5. Public variables? Ouch! Obviously it takes a few more lines of code to make them private because one also has to add getting and setting routines. I think that the first thing most students would see is what they “have to do” but I think the real learning comes with talking about what those routines would enable them to do. In a phrase “bounds checking.” As it is calling code can easily set unreasonable speeds and locations. Not a good thing.

What if the calling code set a negative speed? Do you want to allow that?

Theoretically the speed should determine the distance the object moves. Do you want to allow the calling routine to move the paddle more than speed points? Actually do you want a calling routine to change the location directly at all? Or do you want to add a move method that takes a direction (say up or down in this case) and have the object move itself? That reduces the need for setting routines at least for the class?

For a simple 13 lines of code (including comments and blank lines) there is actually a lot of discussion that can go on with this code. And discussion is a good basis for learning. Do you do this sort of thing with your students? As you review code from others do you think about the “what if” or the “how can I make this better?” or the “what other options are there?” for that problem? A useful exercise for everyone I think. And you?

Comments (0)

Skip to main content