Wednesday, January 26, 2005

Query Your Code

Lately, I've been doing a handful of code reviews for clients. I have access to all sorts of sophisticated tools (like OptimizeIt and TogetherJ), but I find that one of the tools I use the most is the *nix "find" utility combined with regular expressions. Sometimes, simplest is best.

If you had to analyze some data that lived in a relational database, would you look at all the records one at a time? Of course not -- you would execute a query that looks for troublesome records. I've been using the same technique against a body of source code. Curious about who is firing constructors on your boundary classes?

find -name "*.java" -exec grep -V "new .*Boundary.*(" {} \;

[Find all files named "*.java" from the current directory down and send each of these files into the grep command, which looks for the regular expression for constructor calls to any class with "Boundary" in the name.]

Or what about this:

find -name "*.java" -not -regex ".*Db\.java" -exec grep -H -n "new .*Db" {} \;

[Find all non-Db Java files that fire constructors on DB classes. In other words, determine the coupling between the boundary classes and all other classes.]

This technique depends on consistent naming patterns for your files. If you can rely on this, you can query your code base for analysis.

One of my common themes is to avoid drowning in impressive but unsuitable tools. If you go rabbit hunting, you can use a shotgun, a bazooka, or an atomic bomb. One of these tools is better than the others. Similarly, using a simple but effective command line tool beats the expensive ones for some tasks.

Treat your code as data and apply the same techniques you use on data analysis on your code base.


tweedledeetweedledum said...

This blog is awesome! If you get a chance you may want to visit this pda software site, it's pretty awesome too!

Anonymous said...

One good resource for kid game software and many more free software alternatives is
Yes it is my site and I would love for you to drop by for a second. From there you will have free access to several of my products such as The Marketing Toolbar (which is goldmine of information on how to do things quickly and on the cheap, thus saving you time and money).
Also for webmasters I have created Webmaster Wizards, which will help you with almost every aspect of putting code on your site from things such as legal and privacy disclaimers to popunders. I have so much content and free software there I could fill up this whole blog, so check it out Thanks, Sincerely Rob Rudd

Anonymous said...

New software automatically creates everything you need for your own highly lucrative list building Joint monitoring softwarenetwork monitoring software

Anonymous said...

This easy to use software will automatically monitor all your websites notifying you immediately of any problems.faster download softwarefaster download software

Anonymous said...

Yo, you have a Terrific blog here! Lots of content means more readers, more readers means more Sales!
I'm definitely going to bookmark you!
I have a bus computer info speedbus computer info speed site/blog. It pretty much covers bus computer info speed Problems with your Windows Xp Computing !
Come and check it out if you get time We are just a Click Away ! :-)