Chapter 6Being Lazy

Progress is made by lazy men looking for easier ways to do things.

Robert A. Heinlein

In Java we often execute code eagerly. The arguments are evaluated right at the time of method calls, for example. There’s a good reason for that; eager code is easy to write and to reason about. But delaying commitments until the last responsible moment is a good agile practice. When executing code, we can gain in performance by being just a little lazy. Eager is simple, but lazy is efficient. Now we can easily be lazy and make things simple and efficient.

If we use heavyweight objects in a program, we want to postpone creating them. Certainly we’d delay running any expensive computations until we really need them. Paradoxically, laziness ...

Get Functional Programming in Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.