While I always smirk at System.out.println("Got here somehow..."); statements... there is some value to print statements in general.
Now... you should never ever use System.out print statements. They are easy to forget in your code and make others laugh at your print statements that sometimes have great level of profanity in them. The most proper way is to you one of the logging frameworks that would allow you great level of control (display all levels of logging including debug on development environment, while only displaying error level messages on production).
Personally, I favor stepping through my code via a debugger to see what's going on.
So when do I use print statements to debug? Counters...
While stepping through your code one by one might one on a single run, what do you do when you need to process thousands records? Are you going to do it manually? Think not.
The way to work with print lines here is to print out certain message, for example
"Retrieved record from database"
"Cleaned raw record"
"Record passed through process A"
"Exception occurred while processing A logic"
"Record passed through process B"
"Exception occurred while processing B logic"
"Finished"
now, when your process finished running, you will have quiet of few of there records... Yes, you could have tried to declare global variable to give you number of results for each... but what if you are running multiprocessing program, what if <insert some type of complication here>. To make things easier, take generated records in the log and use good old grep command to count number of instances of that particular phrase in the log file.
This way you can generate statistics of
"Retrieved record from database" 1000
"Cleaned raw record" 1000
"Record passed through process A" 820
"Exception occurred while processing A logic" 180
"Record passed through process B" 620
"Exception occurred while processing B logic" 200
"Finished" 620
Now it is easy to see how much records you loose and where and now you can pint point the problem and get a bigger magnifying glass to the trouble spot and you did not have to run it through 1,000 times yourself by hand.
Hope this simple example showed the value of print statements and provided you with another way to troubleshoot your code!
No comments:
Post a Comment