Java Reference
In-Depth Information
Chapter 8. Refactoring, testing, and debugging
This chapter covers
How to refactor code to use lambda expressions
The impact of lambda expressions on object-oriented design patterns
Testing lambda expressions
Debugging code that uses lambda expressions and the Streams API
In the first seven chapters of this topic, you saw the expressive power of lambdas and the
Streams API. You were mainly creating new code that made use of these features. This is great if
you have to start a new Java project—you can use lambdas and streams immediately.
Unfortunately, you don't always get to start a new project from scratch. Most of the time you'll
have to deal with an existing codebase written in an older version of Java.
This is the purpose of this chapter. It presents several recipes showing how you can refactor
existing code to make use of lambda expressions and gain more readability and flexibility. In
addition, we discuss how several object-oriented design patterns including strategy, template
method, observer, chain of responsibility, and factory can be made more concise thanks to
lambda expressions. Finally, we explore how you can test and debug code that uses lambda
expressions and the Streams API.
8.1. Refactoring for improved readability and flexibility
Right from the start of this topic we've argued that lambda expressions can let you write more
concise and flexible code. It's more concise because lambda expressions let you represent a piece
of behavior in a more compact form in comparison to using anonymous classes. We also showed
in chapter 3 that method references let you write even more concise code when all you want to
do is pass an existing method as argument to another method.
Your code is more flexible because lambda expressions encourage the style of behavior
parameterization that we introduced in chapter 2 . Your code can use and execute multiple
behaviors passed as arguments to cope with requirement changes.
In this section, we bring it all together and show you simple steps you can follow to refactor code
to gain readability and flexibility, using the features you learned in the previous chapters:
lambdas, method references, and streams.
 
Search WWH ::




Custom Search