|
N
igel
Warren's and Philip Bishop's
Java in Practice: Design Styles and Idioms
for Effective Java
is a book for Java programmers who want to expand
their command of the language by adding programming idioms to their skill
set. A prerequisite is an intermediate-level knowledge of Java. This book
does not teach any Java language features, but aims to present design principles
for effective use of Java.
The
authors discuss various idioms, starting with such widely-known object-oriented
principles as encapsulation, inheritance, and polymorphism. In these first
chapters you learn, for instance, that packages are a grouping mechanism;
that inheritance of implementation can be replaced by delegation; and that
polymorphism is superior to the maintenance nightmare of manually coding
switches. Depending on your background, this may or may not be interesting.
If
you've previously been using another object-oriented programming language
such as C++ or Smalltalk, then you will have already embraced these principles.
Warren and Bishop won't tell you anything that you don't already know.
These first chapters (roughly 45 pages) are only useful to absolute beginners
in object-oriented programming. If Java is your first object-oriented language,
then read it; otherwise skip it. By the way, the authors provide recommendations
for navigating the content, and their introduction shows a list of which
chapters you should read in what order, depending on your background.
Following
that is an intriguing chapter about immutability and constness ("const-correctness"),
which covers the related issues of final variables, read-only interfaces,
and cloning. This chapter convincingly explains why immutability is an
important design issue, even if Java does not support it by means of language
constructs. Brilliant as this chapter is, however, it's disappointing.
For instance, it explains why and when implementations of clone() are needed,
but leaves you in the dark about implementing clone() in practice. The
authors show primitive examples of clone() implementations but do not delve
into this non-trivial topic. This is unfortunate. After studying this chapter,
you'll know when and why you would want to provide a clone() function,
but not how to implement one correctly.
The
subsequent chapter is devoted to exceptions. It roughly describes why exceptions
are superior to classic error indications via an error state variable.
It scratches the surface of programming with exceptions, but ultimately
is disappointing, too. There is so much more to programming with exceptions.
For instance, the authors state that one should not let exceptions propagate
out of a finally block. Well, that's true but it's also just the tip of
the iceberg. In general, you should never hide an exception. That also
applies when you let an exception propagate out of a catch block. But the
book fails to tell you what to do if you ever encounter a follow-up exception
after an initial exception.
The
authors also do not mention the real-world problem of throws-clause modifications.
In practice, it is a calamity if a throws specification must be modified.
As readers, we wished the authors had included some advice for creating
throws clauses that are halfway resilient to change.
The
chapter about loading classes and creating objects is instructive. It explains
when and how classes and objects are initialized, and mentions that finalization
in Java is unreliable. But here again, the authors just scratch the surface.
For instance, they recommend that you never call an abstract method from
within a constructor. True, but invocation of a non-final method from within
a constructor is equally hazardous. Also, they mention that there are no
guarantees in Java regarding finalization. Yet they do not explain in sufficient
detail what to do instead to achieve proper resource release. Similarly,
they mention that there are problems with the order of initialization of
class variables?and then refer you to the Java language specification for
details.
The
chapter about performance discusses principles such as eager vs. lazy evaluation.
These are not Java-specific idioms, but general-purpose principles that
hold for practically any programming language.
Homogenous
collections are discussed comparing a solution that ensures type-safety
at runtime to a type-safe solution that performs static compile-time checks.
Interesting, but more or less obvious.
Finally,
there are several chapters that discuss patterns such as callbacks, factory
and singleton, and iterators. These are interesting principles, but not
terribly Java-specific. You find these patterns in books on design patterns.
All that
Java in Practice
" adds is how they can be implemented in
Java, which is not too difficult an exercise for a programmer who is halfway
fluent in Java.
So,
what does the book buy you?
Java in Practice
is truly recommendable
reading for Java programmers who have little experience with other programming
languages. The book is well-written and reflects the teaching skills of
the authors. It is well-organized, understandable, and correct. Java programmers
with no further background in other programming languages will learn about
object-oriented principles and design patterns and how these can be expressed
using Java. For this group of readers this book is very good.
For
Java programmers with a background in C++ or Smalltalk, however, this book
does not have a lot to offer. The idioms are either widely known object-oriented
principles or commonly used design patterns that you can find described
in countless other places. The few Java-specific idioms that are in this
book are only introduced, not explored in depth. Typically, after an excellent
motivational part the discussion ceases and leaves the reader alone with
all the related practical problems. The book obviously neither strives
for comprehensiveness nor delves deeply into idioms for Java use in practice.
In fairness, we should add that it is a volume of no more than 208 pages,
and completeness cannot realistically be expected from such a flyweight.
However, a book called
Java in Practice
needs to teach you much,
much more than this book does.
Eventually,
we closed the book with mixed feelings. It's good for novices. It's readable,
well-organized, and didactically flawless. But
Java in Practice: Design
Styles and Idioms for Effective Java
does not deliver what the title
promises. It is not a stunning compendium of wisdom from the trenches.
Instead, it is a nice introduction into widely-known and truly useful design
and programming principles.
Angelika
Langer
develops and teaches classes on Java, C++, multithreading, and
internationalization. She is an internationally recognized speaker and
served on the ANSI/ISO C++ Committee from 1993 to 1998.
Klaus Kreft
is a software architect and senior consultant with 15+ years of experience
in industrial software development. He currently works for Siemens Business
Services in Germany. Langer and Kreft are authors of "Standard C++ IOStreams
and Locales" (Addison-Wesley, 2000) and are columnists for the C/C++ Users
Journal.
|