The Good The Bad and The Ugly – Scripting Games 2013 Event 1

The submission deadline of the 2013 Scripting Games has passed and now all of the scripts are open for judging. For the most part the community has been doing a pretty good job of judging the scripts. In my opinion the ratings are on par with the what I would give as a rating for the entries. I have had some time to review the submissions and prepare some notes on the submissions.

Readability

I am not a big fan of the whole one-liner syndrome, that is the need to compact everything into a single line of code. Sure, its great if the code naturally fits into a single line, but for god sake don’t force it. If the line contains a “;” then its not a one-liner! The following code was submitted for an event:

[codesyntax lang="powershell"]
$Source="C:\Application\Log";$Destination="\\NASServer\Archives";Get-ChildItem $Source -recurse -Filter *.log|%{If ($_.LastWriteTime -lt (Get-Date).AddDays(-90)){md -Force ($Destination +"\" + $_.Directory.Name)|Out-Null;Move-Item $_.FullName ($Destination + "\" + $_.Directory.Name + "\" + $_.Name) -Force}}
[/codesyntax] 

This code is very hard to read even without the word wrap. If the code was broken up at every “;” then we would have this:

[codesyntax lang="powershell"]
$Source="C:\Application\Log" 
$Destination="\\NASServer\Archives" 
Get-ChildItem $Source -recurse -Filter *.log|%{If ($_.LastWriteTime -lt (Get-Date).AddDays(-90)){md -Force ($Destination +"\" + $_.Directory.Name)|Out-Null Move-Item $_.FullName ($Destination + "\" + $_.Directory.Name + "\" + $_.Name) -Force}}
[/codesyntax] 

Just splitting it up like that makes it much easier to read. Now, if you some additional line breaks and indenting you are left with this:

[codesyntax lang="powershell"]
$Source="C:\Application\Log"
$Destination="\\NASServer\Archives"
Get-ChildItem $Source -recurse -Filter *.log |
%{
    If ($_.LastWriteTime -lt (Get-Date).AddDays(-90))
    {
        md -Force ($Destination +"\" + $_.Directory.Name) | Out-Null
        Move-Item $_.FullName ($Destination + "\" + $_.Directory.Name + "\" + $_.Name) -Force
    }
}
[/codesyntax] 

Make your scripts readable because you never know who might need to view your script and know how it works. But even more importantly you will find that you make far less mistakes when you are able to read your own code.

Backticks

I had bookmarked a couple of scripts that used backticks that I wanted to comment on. However Richard Siddaway beat me to it by saying “Backticks are baaaaaad; don’t do backticks”. Take a look at his article and realize that

99.9% recurring of the time you don’t need backticks as line continuation markers

Aliases

Aliases are great when you are entering PowerShell commands in the shell. But when you are writing scripts, especially when you are sharing those scripts, do not use aliases. Aliases in scripts make it harder to understand what a script does and you can’t always guarantee that a system will have the exact same aliases.

Documentation

One area that I did notice a number of scripts doing very well was documentation. Documenting your script is never a bad thing. In the advanced event the documentation was in the form of advanced help. Help was not a specific requirement for the event but most of the submissions that I reviewed included it. That is awesome and keep up the good work. The beginners did a pretty good job as well by inserting basic comments into their entries.