Perl Program Repair Shop

and

Red Flags Table of Contents

June 3, 2002

Program Repair Shop (1–10) What is This Class About? 2 - A Brief Note

About ’Conciseness’ 3 - Counting Lines 4 - Counting Lines of Code: Rules 8 - Introduction: Families of Variables 9 - Families of Variables 10

Section 1: Merging Reports (14–68) Two-Headed Monster 15 - Families of Vari-

ables 16–25 - Digression about Directory Names 17 - 7 lines become 5 19 - Make Relationships Explicit 20–22 - Comments 21 -

GrabFileList 23 - CopyFiles 26–39 - Con-

catenation 28 - Parameter Passing 38 -

CreateInput 41 - CreateOutputDir 45–50 -

Unnecessary Variables 46 - Diagnostics 48 - Structure vs. Function 49 - Minimize Struc-

ture 51 - FindLastOut 52 - GetPadString 55–

66 - The Cardinal Rule of Computer Programming 56 - Repeated Code is a Mistake 57 - Repeated Code 63 - Reminder 65 - We’re Finished! 67 - On Design 68

Section 2: Directory Copying (69– 102)

Indentation 70 - Boolean Values 71 - Precedence Problem 73 - Global Variables 74 - Function Arguments 75 - Dirhandles 77 - Repeated Tests 78–94 - Try it Both Ways 79 - Error Messages 80 - Botched Recursion 83 - Function Conception 84 - Main Loop 85 - Don’t take two steps forward and one step back 86 - 5 lines become 0 91 - Programmers Write Too Much Code 92 - Do What You Mean 95 - Say what you Mean 96 - Leaning Toothpick Syndrome 97 - Temporary Variables are Temporary 98 - A Note About Portability 99 - Directory Copier 102

Section 3: Some Miscellaneous Red Flags (103–140)

  \"  104  -  print
print print print print 106 - Many Very

Long Strings 107 - C-style for loop 108 - A Lucky Find! 109 - my 110 - Superstition 111 - Global Lexicals 113 - Excessively Decorated Comments 114 - C-style for Loop 116 - Unnecessary Variables 117 - Single Scalar Variable in Quotes 118 - Array Length Variables 119 - Unnecessary Shell Calls 120–126 - Shell Call Security Disaster 122 - Capturing the same pattern repeatedly 135 - swswsw 137 - Randal’s Rule 140

Section 4: A CGI Program (141–179) my 142 - Use Smaller Scope 143 - Re-

peated Data Structure 144–147 - Try it Both Ways 145–174 - Avoid Special Cases 150 - Repeated Code 152–164 - Special Cases 153 - Digression: Code and Data Structure Should be in Harmony 154 - Unnecessary Subroutine 157 - Special Values 161 - Don’t confuse internal and external representations of data 162 - print table 165–168 - Tools 167 - evaluate board 171 - Let’s Not Be Doctrinaire 173 - Uniform Data Representa-

tions 175 - get computer choice 176 -

Pause 178 - Higher-Order Functions 179

Section 5: System Administration (185–215)

Indentation 186 - Loop Hoisting 187 - Pattern Matching 188 - $#array 189–193 - Boolean Context 194 - Special Cases 195 - Variable use immediately follows assignment 197 - The Result 198 - if and else 199 -

if or else 200 - else / if 204 - The Con-

dition that Ate Michigan 206 - The Condition that Ate Michigan (Digression) 208–210 - if () { while () { if () { if () { while () { if () { ... } }

} } } } 209 - if-else 211 - unless-else 214 -

Minimum 215

Section 7: Generating Reports (216– 275)

Families of Variable Names 217 - Automatic Testing 222 - Making Two Passes 224–253 - Preprocessing 226–229 - Maintenance Dilemmas 228 - Avoid Special Cases 230–255 - Arbitrary Numeric Constants 231 - Flags 233 - Try it Both Ways 236–245 - Punctuation 237 - Avoid Excess Punctuation 238 - The Condition that Ate Michigan 239 - Extraction 241 - cleanup 242 - Main Loop Finished 248 - Missing Element 0 249 - Repeated Code 251 - printout 252–263 - Say What You Mean 254 - The Maintenance Programmer 256 - C-Style for Loops 257 - elsif 258 - Special Cases 261 - Generating Reports 271 - KLOC 272 - What to Remember 273 - Thank You 274 - Bibliography 275

Section 8: Residue of the Red Flags (277–298)

Families of Variables 278 - Compound Compound Compound Statements 280 - Anecdote About Superstition 281 - Try it Both Ways 282 - Avoid Special Cases 284–294 - Store Related Values Together 285 - Avoid Families of Variable Names 290 - Don’t take two steps forward and one step back 291 - What About Matt Wright? 298