Wednesday, 14 September 2016

Check it out...err I mean 'in', dude!

I love source control (not agape love or anything like it but I really do dig it). It gives me great peace of mind knowing that if I make a stupid mistake in my code (and let's face it, it's gonna happen) I can compare the current version of the code with any previous version and revert back if I need to.

Thing is, I don't like having to do that - I want my code to be sharp, readable, cool but most of all right. And I want this to be the case before I check it in (commit). The problem is that there is not always a bud available who has the time and is in the frame of mind to review my code when I need him\her to. Don't even think about scheduling code reviews, that is just a pain in the rectum - it messes with my mojo when someone interrupts me when I am in the zone just because 4:30 has arrived on their watch.

Then there is the problem of dudes who haven't read Code Complete and like to write 2000 line functions because they haven't learnt abstraction. Who wants to be the poor sod who has to review that mountain so that Harry fancy pants can do his check in? No thanks mate, I got a life.

Ok, so let's assume I've done my unit tests and I am happy with my code and am ready to check it in but want a second pair of eyes to quickly review it before I do. Well, this is DevOps baby, just automate it! "How?" you say. Well the most excellent dudes on the PowerShell Team have written a script analyzer that will do just that.

PS   C:\>Install-Module -Name PSScriptAnalyzer
Once you've got that installed you run the following command:

PS   C:\>Invoke-ScriptAnalyzer -Path $script_path

and presto, you get a list of best practice rules that you have violated. I had to google each one of them but there is usually an explanation handy that will tell you what to do to correct your code. And, if you don't agree with said advice you can tell the analyzer to exclude certain rules like so:

PS   C:\>Invoke-ScriptAnalyzer -Path $script_path -ExcludeRule PSUseShouldProcessForStateChangingFunctions
Neat huh?

No comments:

Post a Comment