 |
|
Bio: Joshua Bloch, Google's chief Java architect and a former Distinguished Engineer at Sun Microsystems, won the prestigious Jolt Award from Software Development Magazine for his book, Effective Java Programming Language Guide. At Sun, he led the design and implementation of numerous Java platform features, including JDK 5.0 language enhancements and the award-winning Java Collections Framework. He holds a Ph.D. in computer science from Carnegie-Mellon University and is most recently the author of Effective Java, Second Edition.
|
Q: You and Brian Goetz are doing a session called "Writing the Next Great Java Technology Book" (BOF-6588). What makes for a great Java book?
A: It depends. You can present important material that has never before been presented in book form, as Brian Goetz and Tim Peierls did in Java Concurrency in Practice, or you can compose a wholly original presentation of existing material that makes it accessible to a whole new group of people, as Kathy Sierra and Bert Bates did in Head First Java. You can take a large volume of information and distill it down to its essence, as Peter Sestoft did so well in Java Precisely. And the list goes on.
Q: You wrote a book, Effective Java, that developers often tell me is their favorite Java technology book. You're twice offering a session titled "More 'Effective Java'" (TS-6623), where you're talking about the latest in best practices for the Java platform. Give us a peek at this.
A: I'm concentrating on material that's new in the second edition of the book. Last year, I spent a lot of time talking about generics, so I'm going to go light on generics this year, though I do have a new generics tip that I'm look forward to sharing.
I'll present a bunch of fun stuff on enum types, and a concise guide to best practices for lazy initialization. Oh, and I plan to display a buff picture of our state's chief executive in his younger days.
A Day in the Life
Q: Describe a day in your life at Google.
A: It's a great place to work. I get to work with lots of smart people from all over the world, and there's no shortage of challenging work to do. Google understands the importance of open source and giving back to the community, so they've always been supportive of my continuing work on the Java platform. And the perks are everything they're cracked up to be.
Q: What's the best meal you've ever eaten at Google?
A: Hmm... perhaps it was the roast quail at Cafe 7 a couple of years back? Or maybe the shiro maguro sashimi at Pinxto? Some of the desserts are pretty good too.
Q: Is the food really good?
A: Yes. I'm not saying that I've never had a bad meal there, but on the whole, it's just great.
Turning "Bloch" Into a Verb
Q: If you could have "Bloch" be turned into a verb in the way that "Google" has been, what would it mean to "Bloch"?
A: Gosh, that's a tough one. Maybe "to write a good API"? Of course, it won't happen, and that's fine by me. There are only a few people in each generation that rate a word, and it's usually an adjective. You know: Newtonian, Cartesian, Shakespearean, that sort of thing.
For some reason, this makes me think of a headline I saw many years ago, when Miles Davis died, his New York Times obituary said "Miles Davis, Trumpeter, Dies; Jazz Genius, 65, Defined Cool." And I thought to myself, "Yep, that about sums it up. And no one's ever going to top that headline."
The Most Beautiful Code
Q: What's your favorite bit of code, or what's the most beautiful code you've ever laid eyes on? Code like poetry?
A: I have seen a fair amount of code that inspires that sort of response. Like real poetry, a piece of code that's beautiful to someone might be ugly to someone else. Here's a mystic code poem:
static int inverse(int val) {
t *= 2 - val * t;
t *= 2 - val * t;
t *= 2 - val * t;
t *= 2 - val * t;
return t;
}
As its name implies, this method calculates the multiplicative inverse of its odd argument, mod 2^32. In other words, for all odd integers i, i * inverse(i) == 1. It works by Newton iteration.
There's an elegant proof that the four iterations -- the unrolled loop in the method -- is sufficient for any input value. Is this the most beautiful code I've ever seen? No, but it's darn cute.
If you like this sort of thing, you should see Henry S. Warren's book Hacker's Delight (Addison-Wesley, 2003). While you're at it, check out Henry's chapter on "The Quest for an Accelerated Population Count," in Osram and Wilson's Beautiful Code (O'Reilly, 2007). And while you have the book in your hands, also read Jon Bentley's chapter on "The Most Beautiful Code I Never Wrote." Jon writes a lot of poetic code.
The Funniest Code
Q: What's the funniest code you can think of?
A: The daily WTF is full of funny code. Also Bill Pugh often sends me funny code that FindBugs uncovers. Here's a delightful example -- unretouched, I swear:
public Object getObject(java.util.Map<String,Class<?>> map)
throws SerialException
{
map = new Hashtable(map);
if (!object.equals(null)) {
return map.get(object);
} else {
throw new SerialException("The object is not set");
}
}
Q: Is code the universal language and if so, why?
A: I think music lays claim to that, with math a distant second, because it takes natural language to link mathematical notation into a proof. Code is a lot like math: A large part of what makes a program readable is well-chosen identifier names, and they're based on natural language.
Q: Which Java luminary had the most profound impact on you and in what way?
A: Probably Doug Lea. He knows so much about so many things, and I've bounced innumerable ideas off of him over the years. He always has something interesting to say. And he has an uncanny ability to extract the best performance out of the VM. Luckily, he's taught me a few of his tricks. For example, branch free code runs fast, cache effects can be deadly, and inlining heuristics have a huge effect on performance.
Q: Can you give us an example of code that you are most proud of creating and explain why?
A: The Collections framework. It's far from perfect, but it's proven itself maintainable and pleasant over the years. The aforementioned Doug Lea built many parts of java.util.concurrent atop it. And I still get letters from programmers telling me how much more pleasant it makes their jobs. It lets you write stuff like this little program, which computes all the anagrams in the file on standard input:
public class Anagram {
public static void main(String[] args) {
int minGroupSize = Integer.parseInt(args[0]);
// Read words from input and put into simulated multimap
Map<String, List<String>> anagrams =
new HashMap<String, List<String>>();
for (Scanner s = new Scanner(System.in); s.hasNext(); ) {
String word = s.next();
String alphagram = alphagram(word);
List<String> group = anagrams.get(alphagram);
if (group == null)
anagrams.put(alphagram, group = new ArrayList<String>());
group.add(word);
}
// Print all permutation groups above size threshold
for (List<String> group : anagrams.values())
if (group.size() >= minGroupSize)
System.out.println(group.size() + ": " + group);
}
private static String alphagram(String s) {
char[] chars = s.toCharArray();
Arrays.sort(chars);
return String.valueOf(chars);
}
}
Q: What do you do when you feel stumped?
A: I drink a nice hot steaming cup of coffee. Google has really good espresso machines, with beans from Barefoot Coffee Roasters. If that doesn't do it, I go for a walk. And if that doesn't do it, I call the aforementioned Doug Lea.
Q: What online resources do you use to keep up with Java technology?
A: The specs. And Google. I go wherever it takes me.
Q: Is there an intellectual discipline or fun activity that you feel makes you a better developer?
A: I think that math and writing both make you a better developer. Math requires the same intellectual rigor that programming does, and writing forces you to organize your thoughts. Both math and writing exercise the same aesthetic facility that is required for writing really good programs.
Q: What are some things you wish you'd learned in engineering school?
A: I wish I'd learned to play guitar better than I do, which isn't very well. I wish I'd learned a foreign language. And art history. Also, it would have been nice to learn something about business and finance. That said, Columbia did very well by me. But I would encourage undergraduates to take the opportunity to acquire breadth while they still have time for it. They'll have plenty of time for depth later.
For More Information
Joshua Bloch will be speaking at the following JavaOne sessions:
- More "Effective Java" (TS-6623)
- Writing the Next Great Java Technology Book (BOF-6588)
With Brian Goetz of Sun Microsystems
Effective Java Second Edition
Order this book through:
Amazon.com
DigitalGuru
|