About the Reviewer
Burk Hufnagel is a Senior Software Architect at ChoicePoint Inc., a longtime member of the Atlanta Java User Group and the Atlanta chapter of the International Association of Software Architects. After 10+ years of C/C++ development, he began using Java/J2EE (now JEE) in early 2001, passed the SCJP and SCJD exams, and hasn't looked back since. His software development motto is "Write and test your code as if whoever maintains it is a violent psychopath who knows where you live."
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed.

Introduction to Groovy Part 3

In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.

Easier Custom Components with Swing Fuse

Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.

Benchmark Analysis: Guice vs Spring

Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.

The Elements of Java Style

Overview

Author(s): Allan Vermeulen, Scott W. Ambler, Greg Bumgardner, Eldon Metz, Trevor Misfeldt, Jim Shur, Patrick Thompson
Publisher: Cambridge University Press
PubDate: January 2000
Reviewer: Burk Hufnagel


One Minute Review

Pros

  • Useful for beginner & intermediate developers
  • Ideas transfer well to other programming languages
  • Small format makes it easy to carry
  • Short, easy to read chunks
  • Even without a second edition, more than 90% is still correct and useful

Cons

  • Originally published in January 2001 and has not been updated to reflect changes in Java and technology

Sections

Intent & Audience

This is a great book for the beginner or intermediate developer - experts should already know this stuff. It will help you create better, cleaner, more easily maintained code. If you work with other developers, I recommend getting several copies for the group.

The authors assume the reader understands the basics of Java and object oriented programming, but that doesn't mean a beginner won't benefit from reading this book. In fact, beginners may benefit the most because they haven't developed some of the bad habits that their more experience brethren have - which means that they don't have to unlearn their current style to follow the rules laid out in the book.

Rules? Yep, one hundred and eight rules to be exact. Rather than writing pages of text explaining their reasoning, the authors just give you a few sentences (or paragraphs) of explanation and, usually, an example showing the rule in use.

While The Elements of Java Style is not intended for someone still learning Java, it can help them too. However, the intended audience is someone who already knows Java and wants to write better code - especially if they are working with other developers. As it says in the Preface, "For a team to be effective, everyone must be able to read and understand everyone else's code."

Relevance of material

The Elements of Java Style was first published in January of 2000. Given how quickly things change in the Java universe, most books that old would be pretty useless. The Elements of Java Style, however, is still very useful today precisely because it addresses coding style rather than a particular technology or package (like AWT).

This is not to say that everything in it is still useful. In particular, I recommend removing two rules from the next edition. The first is rule number 74 (Encapsulate enumerations as classes) which was rendered obsolete when Java 5 added the enum keyword. The second is rule number 99, which recommends the use of the double-checked locking pattern. If you're interested in the explanation, search Wikipedia or the JavaWorld archives for "double-checked locking."

While writing this review I contacted one of the authors, Scott Ambler, about the possibility of an update or second edition of the book. He replied that "You're right, we do need to update" and "We've talked about doing another edition but haven't gotten around to it yet."

The Elements of Java Style proves that "Good things come in small packages." Physically, it's a small book (7 x 4.5 inches - roughly 17.8 x 11.4 cm), and weighs in at just 142 pages. However, the positive impact it can have on your work is all out of proportion to its size. That's because the ideas presented aren't limited to a single language, and the way the ideas are presented is very compact.

The Elements of Java Style isn't about the code you write, it's about the way you write. It's central premise it that your writing style either enhances or decreases the readability and understandability of the code you write.

In fact, your coding style may actually increase the chances of bugs being added because the person modifying it can't tell exactly what the code is supposed to be doing. You don't believe me? OK, try this example:

You have to modify an existing application someone else wrote because the official retirement age has just changed from 65 to 67. Which of the following lines will you change?

if(a==65) e=true;
i=l-65;

Kind of difficult to tell what should be changed and what shouldn't, isn't it? OK, what if the code looked like this:

if ( currentAge == 65 ) {
    eligibleForRetirement = true;
}

jobIndex = jobLevel - 65;

While the compiled code is equivalent (possibly identical), it's a safe bet that you found the second example far easier to understand and have no trouble knowing which line to modify. Well, The Elements of Java Style is about changing your style from the first example to the second.

While this is a pretty important topic I'm only aware of one other document that covers it, and that's Sun's freely downloadable Java Code Conventions, which briefly (24 pages in PDF format) covers Sun's recommendations for Java developers.

So why should you shell out $15.00 U.S. for The Elements of Java Style when Sun (the creators of Java) is giving away their style guide for free? Because the two guides complement each other quite nicely, that's why. Use The Elements of Java Style as the basis for your standards and fill in any blank areas with the conventions in Sun's document.

Chapter highlights

The main portion of the book contains the rules grouped into six sections:

  • General Principles contains four rules that described as "fundamental principles and practices for writing high-quality code."
  • Formatting Conventions contains four rules, which describe "approaches to formatting your code to make it more readable."
  • Naming Conventionscontains twenty-three rules presenting "the standard Java naming conventions for classes, member functions, fields, getter and setter operations, local variables, parameters to member functions, and more."
  • Documentation Conventionscontains thirty-five rules presenting "a collection of principles and guidelines for writing effective source code documentation."
  • Programming Conventionscontains thirty-seven rules presenting "a collection of tips, techniques, and programming idioms for writing superior Java code."
  • Packaging Conventions contains five rules presenting "a collection of packaging conventions for distributing your Java code."

Rating

Relevance    
Readability
Overall

Why? Well, when I was offered the opportunity to be a reviewer for JavaLobby, The Elements of Java Style was the first book that came to mind. Over the years, I've read lots of books that I would recommend to different developers, but this book is one of a few that I would recommend to all developers. Pick up a copy, give it a read, and I think you'll agree.

Resources

The Elements of Java Style
The Elements of Java Style Website