Browse DevX
Sign up for e-mail newsletters from DevX


Boost Efficiency with a Performance-tuned Debugging Strategy-4 : Page 4




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Flexibility and Scalability
The debugging technique demonstrated in the previous section, while very efficient, is neither flexible or scalable. First, we'll deal with the issue of increasing flexibility. Consider the following scenario. Your application needs to maintain a log of crucial system activity that exacts a minimum performance penalty. During the development phase, this log should report every database query and the exact value of the results returned. During the production phase, only critical exceptions should be logged and there will also be times when the values returned from the database should be logged.

How can a system be developed to meet these demands? The solution is to create a Debug class containing one or more logging methods, and a static final integer instead of a boolean primitive. Rather than simply declaring that debugging is turned on or off, the integer would indicate what the debugging level of the system is currently at. For example, 6 levels could be created: 1—Critical Failures, 2—Serious Errors, 3—Standard Exceptions, 4—Database Values, 5—Key Variable Values, 6—Interesting System Events. Then you could have your debugging statements evaluate the current debugging level and declare how serious the information is that they are reporting. Consider the following code sample:

if ( Debug.dbgLevel <= 3 ) Debug.logIt( "blah blah blah" );

With this one line of code, we have indicated that the dbgLevel variable (of type int) must be compared to the integer '3'. If the current debug level is equal to or lower than 3, then the following line is not executed. Essentially, this debugging line is a debug level 3. If the debugging level is set to three or greater, then this line executes, otherwise, it doesn't. We now have a flexible solution, but what about scalability?

To create a scalable debugging framework, we can build upon the flexible framework outlined above. One way to do this would be to pass the debug level into the "logIt()" method. This would allow the Debug class to take that value and process the other String parameter in a particular way, depending upon the nature of the debugging info. Critical errors could spawn a separate thread to send an e-mail to the System Administrator informing her of the error, serious errors and standard exceptions could be recorded in an error database as well as a flat file, database query values could be logged to a special file, and all other information could be logged to a common file. Any or all of these could be changed to meet the changing needs of the system, and the system as a whole could scale to meet the demands of the growing enterprise.

As we advance in the software development industry, the code we produce is expected to be clean, efficient, and reliable. Perhaps even more than that, we are expected to be able to rapidly diagnose and solve any problems in our code. A key component in meeting these expectations is to have a consistent, well-designed debugging strategy. We have outlined some of the common performance pitfalls in typical debugging techniques, examined some more efficient approaches, and seen how our debugging framework can be made more flexible and able to scale to meet the changing technical and business needs.

Sample Applications
There are two sample applications for you to get a feel for how to apply the debugging techniques outlined in this article:

BasicDebug.java demonstrates a minimal debugging framework that should be sufficient for any project that needs debugging info during development but none during production.

AdvancedDebug.java demonstrates a more robust debugging framework that utilizes the debugging level technique, allowing for increased flexibility in how much information is recorded and how the system responds to certain circumstances.

DevX Java Pro Kyle Gabhart is senior software engineer for Brainbench who has developed and presented Java courseware and is an "expert" on AskMeHelpDesk. He currently is collaborating with WROX press on a new Java and XML book.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date