Java Reference
In-Depth Information
This “how” style of programming is an excellent match for classic object-oriented programming,
sometimes called imperative programming, because it has instructions that mimic the low-level
vocabulary of a computer (for example, assignment, conditional branching, and loops), as
shown in this code:
Transaction mostExpensive = transactions.get(0);
if(mostExpensive == null)
throw new IllegalArgumentException("Empty list of transactions")
for(Transaction t: transactions.subList(1, transactions.size())){
if(t.getValue() > mostExpensive.getValue()){
mostExpensive = t;
}
}
The other way centers instead on what's to be done. You saw in chapters 4 and 5 that using the
Streams API you could specify this query as follows:
Optional<Transaction> mostExpensive =
transactions.stream()
.max(comparing(Transaction::getValue));
The fine detail of how this query is implemented is left to the library. We refer to this idea as
internal iteration . The great advantage is that your query reads like the problem statement, and
because of that it's clear to understand immediately in comparison to trying to understand what
a sequence of commands does.
This “what” style is often called declarative programming. You give rules saying what you want,
and you expect the system to decide how to achieve it. It's great because it reads closer to the
problem statement.
13.1.3. Why functional programming?
Functional programming exemplifies this idea of declarative programming (“just say what you
want, using expressions that don't interact, and for which the system can choose the
implementation”) and side-effect-free computation explained previously. As we discussed, these
two ideas can help you implement and maintain systems more easily.
Note that certain language features such as composing operations and passing behaviors, which
we presented in chapter 3 using lambda expressions, are required to help read and write code in
 
Search WWH ::




Custom Search