Java Reference
In-Depth Information
Chapter 13. Thinking functionally
This chapter covers
Why functional programming?
What defines functional programming?
Declarative programming and referential transparency
Guidelines for writing functional-style Java
Iteration vs. recursion
You've seen the term functional quite frequently throughout this topic. By now, you may have
some ideas of what being functional entails. Is it about lambdas and first-class functions? Or is it
about restricting your right to mutate objects? In which case, what do you achieve from adopting
a functional style? In this chapter, we shed light on these questions. We explain what functional
programming is and introduce some of its terminology. We first examine the concepts behind
functional programming such as side effects, immutability, declarative programming, and
referential transparency and relate these to Java 8. In the next chapter, we look more closely at
functional programming techniques such as higher-order functions, currying, persistent data
structures, lazy lists, pattern matching, and combinators.
13.1. Implementing and maintaining systems
Let's start by imagining you've been asked to manage an upgrade to a large preexisting software
system, which you haven't yet seen. Should you accept this job maintaining such a software
system? A seasoned Java contractor's only slightly tongue-in-cheek maxim for deciding is “Start
by searching for the keyword synchronized; if you find it, then just say no (reflecting the
difficulty of fixing concurrency bugs); otherwise consider the structure of the system in more
detail.” We provide more detail in the next paragraphs, but first observe that, as you've seen in
previous chapters, Java 8's addition of streams allows you to exploit parallelism without
worrying about locking, provided you embrace stateless behaviors (that is, functions in your
stream-processing pipeline don't interact by one reading from or writing to a variable that's
written by another).
What else might you wish the program to look like so it's easy to work with? You'd want it to be
well structured, with an understandable class hierarchy reflecting the structure of the system;
there are even ways to estimate such structure by using software engineering metrics of coupling
 
Search WWH ::




Custom Search