Scripting games-major dislike

One of the things we were asked to blog about as Scripting Games judges was things we liked and disliked. This code is a major dislike

Get-ChildItem $sourceDirectory | ? {$_.PsISContainer } |
     % { $subDirectory = $_ ; Get-ChildItem ("$sourceDirectory\$subDirectory") -Include *.LOG -Recurse } |
         ? { $logFile = $_ ; $logFile.LastWriteTime -le $modifiedCutOffDate } |
             %  { $logFileAndSubDirDictionary.Add($logFile, $subDirectory) }

Two things really make this stand out as how not to do things:

  1. Using % & ? as aliases in a script. They are tolerable (just) in an interactive command but have no place in a script. Tab completion is so easy. Use the proper command.
  2. Putting multiple commands on a line separated by ;      It makes the code hard to read and awkward to work out whats going on. It also makes script testing, debug and maintenance much more difficult

Avoid these two things in your scripts

Leave a Reply