Tuesday, March 22, 2011

Finding Code Smell Comments with PowerShell

Adam Goucher has a great blog post on looking for comment-based code smells in software you’re testing.

Every occurrence of //TODO, //HACK, or God forbid, //FIX, is a pointer to a spot in the codebase where someone took a shortcut. More often than not those shortcuts highlight unfinished or outright sloppy work – spots rife for bugs, complexity, and all the other messes associated with pain.

Adam’s post has a Python script you can use to report on these problems. PowerShell lets you do the same thing as well in a bit terser fashion. Here’s how I skinned this cat in PowerShell:

Get-ChildItem . -include *.cs -recurse 
		| Select-String -pattern "//todo","//hack","//fix"
The output you’ll get will show the full path to the file, the line number, and the comment, like so[1]:
Tests\FunctionalTests\Telligent.Evolution.Tests.Selenium\Telligent.Evolution.Tests.Selenium\Common\Utilities.cs:158: //HACK: TolLower is a workaround for Unfuddle 3458. Remove when fixed.
You can redirect this to a text file, or you can capture it to a variable like $stinkyFiles so you can get counts, etc.

[1] The offending author of that line is rumored to look suspiciously like this blog’s author who declined to comment on the matter.

Updated: Fixed snippet formatting

No comments:

Subscribe (RSS)

The Leadership Journey