Learning & Teaching Code Style & Appreciation

Yesterday in an informal COSC208 (C and C++ programming) lecture at the University of Canterbury, discussion broke out on how to teach and learn coding Style. Here are my 2 cents;

I believe it's not possible to effectively teach & learn Style with single-developer programs that have a clearly limited life span -- such as all code written for COSC208 and most undergraduate university papers. Once the paper has finished and the grade has been determined, who is going to maintain the student's code? Who is going to read your code? No one. So why would the author/programmer/student bother to make it easy to read or maintain, or care for Style? They would not -- except if the prof mandates it. But as pointed out yesterday, that is very resource-consuming and fails to teach an appreciation for Style.

Writing code for open source projects or other multi-developer projects requires that the code be well-documented, easy to read and understand, and have Style. Without this, the code is simply not valuable, useful, or in the case of open source, it will simply not be committed. OS communities often place more value on Style than the code or algorithm itself -- and rightly so. Imagine trying to fix a bug in someone's code that has bad Style, or even just understand it! Well you probably can not imagine that unless you have done it before. And that is precisely how an appreciation for style is learned!

I think the only way to teach/learn Style is to work with other programmer's code, ideally old code, or code that has a large intended life-span. How this can be achieved in undergraduate university papers is another question. Since peer-review, which works so well in open source, is clearly risky and problematic in university environments, perhaps past student's anonymized code could be used? Tasks could be;

  • This program has a bug. Fix it.
  • Review this code with respect to 208's C Style guide.
  • In terms of the Foo and the Bar, what happens on line 123 (some line in the midst of the most complex part of the program)

I think it would be valuable to repeat these tasks with code with both good and bad Style, for students to appreciate the difference. (i.e. How much easier was it to understand program A than B?)

In the context of COSC208, these programs could be incorporated into quizzes with fairly efficient usage of resources. One past-student's anonymized code could be used for all students in one quiz question. Further, the same anonymized code can have multiple "Style-appreciation" questions in one quiz and can be reused year after year.