T
his
tiny little paperback is modeled after its classic counterparts?Strunk
& White's
The
Elements of Style
(on English writing style) and Plauger's
The
Elements of Programming Style
(on programming style). What these three
books have in common is a list of rules, each with a brief explanation
and examples of correct use. The idea is that adhering to this basic set
of rules improves readability and understandability of the resulting text
or program and thus improves the result's quality.
Another
common trait is that they are indeed "little"?that is, short, concise,
with a strong focus on the essential, and necessary. Avoid any distractions;
get to the point.
The
Elements of Java Style
provides rules for programming in Java. The
authors list 108 rules for improving Java programs, which cover:
-
General
principles
, such as "Adhere to the Principle of Least Astonishment."
-
Formatting
conventions
, such as "Break up long lines," "Don't use hard tabs,"
etc.
-
Naming
conventions
for packages, types, methods, variables, and constants.
Here you find common-sense rules like "Use meaningful names," as well as
the recommendation to stick to the naming conventions demonstrated by the
JDK. These rules have become common practice in the Java community and
there is no good reason to deviate from these conventions.
-
Documentation
conventions
for comment style and use of Javadoc tags, but also recommendations
for content such as "Document synchronization semantics" and "Document
preconditions, postconditions, and invariant conditions."
-
Programming
conventions
, the most exciting part of the book, which passes on common
wisdom that stems from object-oriented programming, such as "The Liskov-Substitution
Principle" or "The Open-Closed Principle," as well as Java-specific rules
such as "Use euqls(), not ==, to test for equality" or "Do not call nonfinal
methods from within a constructor." It covers various aspects of Java programming
including exceptions, concurrency, and synchronization.
-
Packaging
conventions
, such as "The Acyclic Dependencies Principle."
By and
large there is little to argue about.
The Elements of Java Style
is perfect in what it tries to achieve. Each rule is sensible, hardly any
are debatable, and there is no excuse for ignoring any of them. Sure, there
is room and demand for countless further rules, especially in the area
of programming conventions. One would want to learn more about proper use
of Java language features, but such a broader scope is not intended here.
One of Strunk & White's rules is "Omit needless words." It makes perfect
sense to focus on essential and non-controversial rules. And that's exactly
what the authors did: they found the least common denominator.
Where
is the grain of salt, then? Well,
The Elements of Java Style
should
not be misconstrued as a being a style guide in the sense that it only
suggests one certain style and everybody else should feel free to choose
another, more personal, more company-specific style. That's not the intent.
This little book is more a compendium of common wisdom that has been distilled
from years of programming practice. Much of it will sound like common sense
to the experienced. Nevertheless, a little book like this is a good way
to pass on the distilled "common sense" to novices to whom the rules do
not yet sound familiar. On the other hand, this book is definitely not
the right source for simply learning Java. The rules are intended to remind
you of what you should already know and keep an eye on. As a reminder and
a sort of checklist, it is equally useful to experienced programmers as
well as to reviewers and quality assurance groups.
In
sum, this book covers base knowledge required of every Java programmer.
As a historical anecdote,
The Elements of Java Style
was inspired
by the Rogue Wave C++ Guide, a technical report at
Rogue
Wave Software
, a provider of C++ libraries. The quality of their C++
code is vital to the company's success and the C++ Guide is mandatory reading
for every C++ programmer at RWS.
The Elements of Java Style
has
the potential to do the same for Java programmers.
Angelika
Langer
develops and teaches classes on advanced C++, STL, multithreading,
internationalization, and Java. She served on the ANSI/ISO C++ Committee
from 1993 to 1998.
Klaus Kreft
is a software architect and consultant
with 15+ years of experience in industrial software development. He works
for Siemens Business Services in Germany. Langer and Kreft are authors
of "Standard C++ IOStreams and Locales: Advanced Programmer's Guide and
Reference" (Addison-Wesley, 1999) and are columnists for
C++
Report
magazine.
|