Lazy evaluation (called short-circuit evaluation in compiled languages) is a strategy some programming languages use to save work for the last minute or avoid unnecessary work altogether. For example, suppose we had a conditional like this:
Suppose itIsFriday was false. Because Java short-circuits evaluation, it wouldn't bother checking the value of itIsRaining—it knows that either way the result of our && will be false, so we won't print the invitation to board game night.
We can use this to our advantage. For example, suppose we have a check like this:
What happens if 'Becky' isn't in our friends hash map? In Java, we'll get a NullPointerException (Python would similarly raise a KeyError, but Ruby and JavaScript would just give us a null object).
Instead, we could first confirm that 'Becky' and I are still on good terms:
This way, if 'Becky' isn't in friends, Java will skip the second check about Becky being free and avoid throwing the NullPointerException!
This is all hypothetical, of course. It's not like things with Becky are weird or anything. We're totally cool. She's still in my friends hash map for sure and I hope I'm still in hers and Becky if you're reading this I just want you to know you're still in my friends hash map.
We can also take a lazy approach in system design. For example, suppose we had a class for tracking temperatures:
Suppose we wanted to add a feature for getting the the highest temperature we've seen so far. We could "eagerly" keep the max up to date whenever we insert a new temperature:
Or we could lazily (or "just in time") calculate the max whenever it's requested:
The best choice depends on how often you expect to run getMax!
Becky, I haven't hosted another board game night since the incident. I know we both said things we didn't really mean and anyway Becky just if you're reading this please know that I've been cake free for 3 whole days now and it's hard but I'm doing it for you PLEASE Becky. Please.
Interview coming up?
Get the free 7-day email crash course. You'll learn how to think algorithmically, so you can break down tricky coding interview questions.
No prior computer science training necessary—we'll get you up to speed quickly, skipping all the overly academic stuff.
No spam. One-click unsubscribe whenever.
You're in! Head over to your email inbox right now to read day one!