Once nice little features of most debuggers that I have been exercising recently is the ability to log information on a breakpoint. This can be a really useful was to understand code without having to modify it is involve byte code modification.

Let's consider this very trivial and inefficient implementation of a function to return the n'th number in the Fibonacci sequence.

public class Fib { public long fib(long number) { return number < 1 ? 0 : // Breakpoint here number < 2 ? 1 : fib(number - 2) + fib(number - 1); } public static void main(String[] args) { Fib fib = new Fib(); System.out.println(fib.fib(10L)); } }

Now we add a simple breakpoint, I am going to modify it slightly so that it doesn't halt; but it will log a simple expression which gives us the current value of `number`

This gives you the following output for our rather inefficient code:

ebugger connected to local process. Source breakpoint: Fib.java:11, evaluate(number)=10 (long) Source breakpoint: Fib.java:11, evaluate(number)=8 (long) Source breakpoint: Fib.java:11, evaluate(number)=6 (long) Source breakpoint: Fib.java:11, evaluate(number)=4 (long) Source breakpoint: Fib.java:11, evaluate(number)=2 (long) Source breakpoint: Fib.java:11, evaluate(number)=0 (long) Source breakpoint: Fib.java:11, evaluate(number)=1 (long) Source breakpoint: Fib.java:11, evaluate(number)=3 (long) Source breakpoint: Fib.java:11, evaluate(number)=1 (long) Source breakpoint: Fib.java:11, evaluate(number)=2 (long) ... sometime later Source breakpoint: Fib.java:11, evaluate(number)=1 (long) Source breakpoint: Fib.java:11, evaluate(number)=2 (long) Source breakpoint: Fib.java:11, evaluate(number)=0 (long) Source breakpoint: Fib.java:11, evaluate(number)=1 (long) 55

So what we have done here is to add trace post-hoc, you might not even have access to the source and still be able to get useful information out of what the code is doing.

Now the obvious take away from the above example is that we are computing the same value over and over again. So here is another version of the code which instead uses a Map to store previously computed values in the sequence. Note also it is more of a pain to add trace to this code than in the previous case without making the Lambda less pretty.

import java.util.HashMap; import java.util.Map; public class Fib { Map<Long, Long> data = new HashMap<>(); { data.put(1L, 1L); } public long fib(long number) { return data.computeIfAbsent(number, n -> n < 1 ? 0 : fib(n - 2) + fib(n - 1)); // Breakpoint here } public static void main(String[] args) { Fib fib = new Fib(); System.out.println(fib.fib(10L)); } }

It is worth noting two things here, first of all your breakpoint log expression should be evaluating the value of the Lambda parameter `n`

and that the second is that you should always put your expression on a new line so that future developer can easily breakpoint it. (Not matter now much smaller/ prettier/ clever it would look if you put the code on one).

So the trace output is now as follows, much better as each value is only computed the once.

Debugger connected to local process. Source breakpoint: Fib.java:17, evaluate(n)=10 Source breakpoint: Fib.java:17, evaluate(n)=8 Source breakpoint: Fib.java:17, evaluate(n)=6 Source breakpoint: Fib.java:17, evaluate(n)=4 Source breakpoint: Fib.java:17, evaluate(n)=2 Source breakpoint: Fib.java:17, evaluate(n)=0 Source breakpoint: Fib.java:17, evaluate(n)=3 Source breakpoint: Fib.java:17, evaluate(n)=5 Source breakpoint: Fib.java:17, evaluate(n)=7 Source breakpoint: Fib.java:17, evaluate(n)=9 55

The screen grabs in this blog are from Jdeveloper; but similar features are available in Intelij, Netbeans and if you are a bit crafty you can get something similar in Eclipse.

## No comments:

Post a Comment