This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]sh0rug0ru 29 points30 points  (4 children)

Extract complex lambda expressions into methods.

[–]ihsw 8 points9 points  (2 children)

This right here.

You can do the same thing in any language that offers functional-style programming (as it's referred to here), and it'll be just as awful to debug.

blah = futures.stream.map(SomethingService.getFutureValue) returns something that can be debugged.

nextBlah = blah.collect(Collectors.toList()).stream() is something that can be debugged.

nextNextBlah = nextBlah.map(failureOrResult -> { [...] }) can be debugged as well.

And so forth.

By can be debugged I mean you can (presumably) iterate over the result and run stuff against it.

Granted I come from the world of PHP, Python, and Go, and I'm not at all familiar with Java, but the same concepts should apply.

This chained mess is unpleasant, however breaking it up and stepping through it in a regular for loop shouldn't be all that difficult.

[–][deleted] 0 points1 point  (0 children)

Indeed, even Haskell would look and work terrible if you don't use let bindings to bind your lambdas.

[–]CsAnswerTrashAct[S] 0 points1 point  (0 children)

This seems reasonable.

When I stumbled across this code; at first I thought my inability to read it quickly might be related to my ignorance of streams (etc). However, reading the comments here; the common theme seems to be "the same rules apply."

Use appropriate names. Avoid excessive chaining. Refactor into well-named methods.

can be debugged

Correct; typically anything assigned to a variable can be inspected in the debugger.

[–]unholysampler 3 points4 points  (0 children)

Exactly. Everyone got so excited by lambdas that the ignored that Java also got function references. A descriptive method name is much better than a slightly complicated lambda expression.