Flickr - Nicholas T - Finality

Neil’s first contributions to the company codebase were to be tested in the fires of a late afternoon code review. Donavan, a veteran of Java since 1.1, was asked to sit in.

It began as a thankfully uneventful affair — but then Donavan noticed that Neil had removed the finally from an existing try/catch/finally block.

“Why’d you do that?” he asked.

“Well, because a finally block is indeterministic,” Neil explained.

Donavan frowned, smoothing out the startled What?! in his throat into a calmer, “What do you mean?”

“You never know when it’s going to execute,” Neil elaborated. “It may never execute. It also causes severe performance and portability problems. It’s not good practice to use.”

“I’ve… never heard of that,” Donavan replied, patiently stowing his skepticism for the moment. “I can only think of two reasons why a finally block might not run: one, the thread executing the try/catch is halted or killed. Two, the JVM crashes or exits in the try or catch block.”

“I’ve read about this before. I know what I’m talking about,” Neil huffed, folding his arms.

“Where’d you read this?” Donavan asked gamely.

“I think it came from a book called Effective Java,” Neil replied.

“I have that book,” Donavan replied. “This is news to me.”

Neil tugged at his collar. “Well, I — I think you’ll find the new code performs much better, and is safer.”

“Did you run any performance tests against it?” Donavan asked.

Neil’s face grew steadily redder, until he wrapped around the spectrum and approached violet. “I have fifteen years of Java experience!”

The experience card? With Donavan? This guy was new, all right.

“Whoa! You don’t have to get defensive about this.” Donavan put up his hands. “I just want to know why you did this, and what your source was. I’m not going to propagate this information by saying ‘Some guy told me this was the case.’ I’d like to read it for myself.”

“I know it was Effective Java,” Neil replied. “Look it up, if you want!”

The rest of the code review proceeded without fanfare. Upon returning to his cube, Donovan googled for information on when and why not to use finally in Java. He found nothing enlightening.

Then he searched for “Effective Java finally.” There, in an online version of the book, Chapter Two made reference to avoiding finalizers, stating the exact same facts Neil had attributed to the hapless finally block.

There it dawned on Donavan: Neil had either confused the terms, or he believed that a finally block was actually itself a finalizer.

Donavan jumped up and checked Neil’s cube, only to find it empty. So he sent an email with the citation. I can see how the terms can be confusing, he added at the end, hoping to soften the blow.

After clicking the Send button, Donavan sat back and folded his arms with a thoughtful frown, pondering the long-term effects of such an innocent slip. In Neil’s alleged 15 years of experience, how many finally blocks had he delete-keyed into oblivion?

Fortunately, Neil wasn’t so set in his ways that he couldn’t correct course when proven wrong. He and Donavan had a good laugh about it the next day. And once Neil finally understood finally, they could finally program happily ever after.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!