Wednesday, October 31, 2012

When Static Analysis Is Not Enough

Wikimedia Commons under GNU FDL 1.2

We've all had the experience of needing to determine if a particular piece of code is still used. IDE's are fantastic at this type of static analysis. Just because your IDE informs you that a piece of code is referenced, though, doesn't necessarily mean that the code is still used at run time in a production environment.

A simple way to find out if a piece of code is executed is to insert a log statement. However, what do you do when there are multiple entry points to the piece of code and you suspect only some of them are still used? You would really like to know which code paths are exercising the functionality so that you can prune the obsolete references.

A simple and clever way accomplish this is by inserting the following commons logging statement:

When the suspect code is encountered, a log message will result along with a stack trace. The stack trace will let you know the code path used to arrive at the log statement!

No comments:

Post a Comment