Program Repair Shop


Red Flags

Sample Slides

Miscellaneous Code

allines: I found this in comp.lang.perl.misc. I don't know just what this is supposed to be doing, but it's doing it wrong. Here are before and after shots.

xcopy: Another comp.lang.perl.misc find. Here someone is trying to reimplement the DOS xcopy command, which recursively copies a directory. This example is very instructive, so I spend a lot of time on it in the class. Here are the original and finished versions.

This example is noteworthy for its exceptional family of variable names, which, as usual, should be replaced by an array.

The only thing I can say about this example is that even if the author wants to do it the wrong way, this is the wrong wrong way.

I open the class with an example of a red flag, one that's easy to recognize and to fix: Families of Variable Names. If you have a family of scalar variables with similar names, you almost certainly should have used a compound data structure. Amit Indap kindly referred me to this exceptionally putrid example.


I do a lot of work when I'm looking for examples and topics for the class. Often, I have an idea about a bad practice that I can discuss. Then I examine my archive of usenet articles and IRC transcripts looking for examples of the practice, to see if it really is a common problem. Sometimes it turns out that the bad practice is different from what I thought, or is not very widespread; then I usually leave it out.

For example, in October 2001 I got the idea that perhaps most uses of $#array would be improved by switching to something else. For example, $#array is often used in expressions such as $array[$#array] to select the last element of an array, but I think $array[-1] is clearly superior. So I had a hypothesis that $#array would be a red flag.

I did a study in which I examined 9037 comp.lang.perl.misc articles and found 192 appearances of $#array. Then I looked to see how many of these would be better written some other way. Eventually I decided that although many uses of $#array were problematic, it was not actually a red flag: more often than not, code with $#array cannot be easily improved by getting rid of $#array.

Here's the study.

More information about the class

O'ReillyNet article

Return to: Universe of Discourse main page | Perl Paraphernalia | Classes and Talks