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 book, 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

8.2. Refactoring object-oriented design patterns with lambdas

8.3. Testing lambdas

8.4. Debugging

8.5. Summary

sitemap