Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Debugging lambda expressions  RSS feed

 
Roel De Nijs
Sheriff
Posts: 11333
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Until now I didn't get my hands dirty on lambda expressions (shame on me, I know ), so this might be a stupid question.

So far I have seen plenty of lambda expressions and I always wonder how you can debug this kind of code. Because these statements mostly are "one line" statements (using method chaining) and you'll probably be interested in the predicate itself and not in the implementation code of e.g. the filter method.

Reusing the same problem (print all the odd numbers in a list greater than 15 without any duplicates) from my other thread. Let's assume there's a bug in our code (just hypothetical of course, we don't write bugs ). How can you debug your code?

With the non-functional solution of your code it's fairly easy: you set a breakpoint on the line with the if statement and with each iteration you'll be able to inspect variable i and see if it's added to set or not:
But with the Java 8 code, it seems less straightforward.If I put a breakpoint at the 1st filter statement will it stop with every iteration without going through the filter method itself. Or do I need to format my code in a specific way to achieve this goal, e.g.:Probably depends on the IDE you will be using...

Kind regards,
Roel
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37180
515
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Eclipse, setting a breakpoint works fine with the original formatting. First, Eclipse stops in the i%2 part. If I keep pressing F7 (step into), it eventually gets into filter method. Granted this isn't terribly useful, but I think it is what you asked about.

I use three main techniques for working with lambdas:
1) Unit tests (regression is nice)
2) The peek method so I can see what is in the stream at different points in time.
3) A conditional breakpoint so I can stop on a certain pass.

 
Roel De Nijs
Sheriff
Posts: 11333
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:In Eclipse, setting a breakpoint works fine with the original formatting. First, Eclipse stops in the i%2 part. If I keep pressing F7 (step into), it eventually gets into filter method. Granted this isn't terribly useful, but I think it is what you asked about.

As an Eclipse user myself, that's exactly what I wanted to know Maybe IDEs will improve in the future to debug lambda expressions more easily.

Jeanne Boyarsky wrote:I use three main techniques for working with lambdas:
1) Unit tests (regression is nice)
2) The peek method so I can see what is in the stream at different points in time.
3) A conditional breakpoint so I can stop on a certain pass.

Seem to be very reasonable techniques. Certainly number 1 (of course) and 3, the 2nd one reminds me to the dark age of webapp development when you had to use nothing but alert statements to debug your javascript code. Those good old days!
 
Cay Horstmann
author
Ranch Hand
Posts: 195
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to add a thought to the very nice explanation how to use the Eclipse debugger. Have another look at the loop and the Java 8 code. With the loop, it's very reasonable to ask "How do I debug that?"

But with the stream code? I mean, we filter to keep even numbers, and numbers > 15, then put them in a set, and print them.

What is there to debug???

That aspect of lambdas sometimes gets overlooked. Together with a well-designed library, they can reduce your debugging effort because the code just does what it should.

Cheers,

Cay

 
Roel De Nijs
Sheriff
Posts: 11333
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cay Horstmann wrote:But with the stream code? I mean, we filter to keep even numbers, and numbers > 15, then put them in a set, and print them.

What is there to debug???

That aspect of lambdas sometimes gets overlooked. Together with a well-designed library, they can reduce your debugging effort because the code just does what it should.

That's of course true! With lambdas you only have to focus on what needs to be done and let the API take care about the rest.

In this very simple example you'll see with just a glance what's happening and what the code will do. But I have seen much more complicated examples (20-25 lines), certainly if you need to write your own custom downstream collector with a supplier, accumulator, and combiner. Then it could be useful to have the possibility of debugging your code.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!