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."
| 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 |
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."
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.
The main portion of the book contains the rules grouped into six sections:
| 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.
The Elements of Java Style
The Elements of Java Style Website