Debugging trick – Conditional break point

One of the most important developer activities is debugging.
Imagine there are 1000 employees in a record and application is giving error for a particular employee lets say with name “prem”. Console error says its some time inside the loop where all the records are iterated. Generally people put a break point  before the error prone line number and watches the value to find out the state. If its linear flow its good but if it is the loop then one has to wait for ( worst case 1000, best case within 10 ) repetition to get to know the root cause.

Here comes the experience of developer and facility of tool IDE which is called as conditional break point. By doing the right click on break point one can the property of conditional break point. you just need to simple give the condition ,as in if clause ->  emp.getName().equals(“prem”). By virtue of this condition the code will break only when the employee name is “prem”. This will save a lot of time to watch and analyze all the names of employees manually to find out the buggy/error prone employee object “prem”.

In such cases previously i use to do System.out.println(), recompile and run app time and again. then before check in the code need to remove systemouts. Now I realize it was such a pain and when better tricks are available 😉

For more details to set conditional break point using Eclipse visit the below link:
http://eclipse.dzone.com/tips/effective-debugging

what is moneky patching ?

Monkey-patching is the dangerous-yet-frequently-useful technique of re-opening existing classes to change or add to their behavior. The term monkey patch only refers to dynamic modifications of a class or module at runtime, motivated by the intent to patch existing third-party code as a workaround to a bug or feature which does not act as you desire.

Do not confuse it with sub class and use, that is some thing which the compile time stuff in java and related languages. I can be though of as using filters or reflection to modify or tweek the run time behavior of a class.there are libraries like cglib (code generation library – which adds or intercepts the main class file) which helps to do that.

In monkey patching main class is called and you additional behaviour, however in subclass you have to explicitly call the sub class.