The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Ulle (2) [Avatar] Offline
#1
Dear authors,

I always appriciate if someone motivates, why the thing he does or promotes is a good idea. But if the explanation lacks a solid foundation, then the idea gets discredit on its own.
That came to my mind, when I read the motivation you gave in chapter 1, why lambda expressions help when passing methods as parameters: "this programming technique is shorter, clearer and less error prone than the common tendency to use copy and paste".

I believe that lamdas will be used by experienced developers. And I can not believe, that it is common tendency among them, to use copy and paste. There are several other advanced techniques they might use (design patterns like template method, refactoring to method and so on).
It would help (at least me), if you could explain, why lamdas are more useful than these advanced techniques.

Kind regards,
Ulrich
raoul-gabriel.urma (37) [Avatar] Offline
#2
Re: Motivating Lamdas
Hi Ulrich,

Thanks for reading our book and posing an excellent question!

There are two elements to distinguish:
1) behaviour parameterisation
2) expressing a behaviour inline succinctly, so it can be passed ("behaviour as data")


Our text describes behaviour parameterisation as a technique that enables you to avoid copy-paste and make your code more flexible.
It goes under the category of advanced technique that you describe. The strategy design pattern is an example of that. Such techniques are generally reserved for experienced developers.

The good news is that the Java API has been updated to support more of this behaviour parameterisation (e.g. methods like 'filter'). So in a sense most of the time you will simply 'use it' (as a normal developer) rather than 'create it' (as an experienced developer).

Ok, you say, that's great but what about using it -- this means being able to 'pass' behaviours. How does Java express this?
Currently, the way to do this prior to Java 8 (as we explain in chapter 2) is to use anonymous classes. The problem is that anonymous classes are considered very verbose and reduce the readability of your code. They are also painful to use for normal developers. As a result, they are not very popular so developers are not so keen to 'pass' behaviour. Consequently, not many developers want to use behaviour parameterisation either!! (Indeed, copy-and-paste seems far simpler to most developers.)

So what about lambdas? Lambdas add no new expressive power to Java.
They offer a more concise & simple syntax to pass new behaviour (which previously we could only clumsily express as an inner class). As a result, their syntactic simplicity encourages normal developers to pass behaviour and make use of behaviour parameterisation -- rather than cut-and-paste.
In other words, lambda expressions should succeed in doing what anonymous classes have failed to do!

Does this help clarify the motivation?
Ulle (2) [Avatar] Offline
#3
Re: Motivating Lamdas
Hi Raoul-Gabriel,

Thank you for the clarification. Let my repeat how I understood your reply:

1) We are talking about behaviour parameterisation.
2) Roughly speaking, there are two different kinds of developers: "normal" and "experienced" ones.
3) Normal developers who want to use behaviour parameterisation have not much support from the Java API prior to Java 8.
4) Experienced Developers do have support through inner classes, design patterns and the like.
5) Lambda expressions, through their syntactic simplicity, give normal developers the possibility to *use* behaviour parameterisation (and avoiding inferior programming techniques like copy/paste) and experienced developers the possibility to use it less verbose and complicated.

If this reflects what you posted then yes, it has clarified the motivation.

Thank you and regards,
Ulrich
raoul-gabriel.urma (37) [Avatar] Offline
#4
Re: Motivating Lamdas
Hi Ulrich,

That's correct! I would add in 3) "not much *simple* support" smilie
By making lambdas a concise construct to represent & pass behaviours, it should encourage all programmers to adopt behaviour parameterisation more often.

Thanks and please let us know any other feedback you may have (e.g. chapters 4/5 about streams that we pushed recently)

Raoul