E
Eric Sosman
In recent days I've encountered a tool called "Checkstyle," that
parses Java code and flags various departures from its baked-in rules.
Some of these are worthwhile: It will whine if you override equals()
without overriding hashCode(), it will shriek if a method's Javadoc
omits a @param or @throws, it will moan if a static final field isn't
ALL_CAPS, and so on. Some are less so: It insists that all method
parameters should be final, it forbids the ?: operator, it tut-tuts
at `x << 8' for using a "magic number."
But the subject of this rant is its complaint that a class is "not
designed for extension." You write a perfectly ordinary class with
methods foo() and bar(), and Checkstyle throws up its hands and gibbers
that your class is "not designed for extension." Say, what?
Well, it turns out that "not designed for extension" means that
your class has one or more methods that are not either final, abstract,
or empty. That's it, that's The Rule.
You can "design for extension" by having final methods, methods
that a subclass cannot override even if it needs to.
You can "design for extension" by having abstract methods, methods
with no implementation that a subclass must implement on its own.
Or you can "design for extension" by having methods that do
nothing at all. (A peculiar measure of productivity, it seems to me.)
BUT if you have a concrete non-final method that actually *does*
something, you have not "designed for extension." It's all in aid of
somebody's theory about programming style: You're all right as long as
you freeze your methods against overriding or ensure they're impotent.
I wonder what this Checkstyle tool would think of the concrete,
non-final, non-empty equals(), hashCode(), clone(), toString(), and
finalize() methods of ... java.lang.Object, the one class *no* Java
program can avoid extending. If java.lang.Object is "not designed for
extension," is there any hope left for the language?
Enforcing good style is difficult. I wish the purveyors of the
enforcement tools would realize it's beyond their powers to do it well.
parses Java code and flags various departures from its baked-in rules.
Some of these are worthwhile: It will whine if you override equals()
without overriding hashCode(), it will shriek if a method's Javadoc
omits a @param or @throws, it will moan if a static final field isn't
ALL_CAPS, and so on. Some are less so: It insists that all method
parameters should be final, it forbids the ?: operator, it tut-tuts
at `x << 8' for using a "magic number."
But the subject of this rant is its complaint that a class is "not
designed for extension." You write a perfectly ordinary class with
methods foo() and bar(), and Checkstyle throws up its hands and gibbers
that your class is "not designed for extension." Say, what?
Well, it turns out that "not designed for extension" means that
your class has one or more methods that are not either final, abstract,
or empty. That's it, that's The Rule.
You can "design for extension" by having final methods, methods
that a subclass cannot override even if it needs to.
You can "design for extension" by having abstract methods, methods
with no implementation that a subclass must implement on its own.
Or you can "design for extension" by having methods that do
nothing at all. (A peculiar measure of productivity, it seems to me.)
BUT if you have a concrete non-final method that actually *does*
something, you have not "designed for extension." It's all in aid of
somebody's theory about programming style: You're all right as long as
you freeze your methods against overriding or ensure they're impotent.
I wonder what this Checkstyle tool would think of the concrete,
non-final, non-empty equals(), hashCode(), clone(), toString(), and
finalize() methods of ... java.lang.Object, the one class *no* Java
program can avoid extending. If java.lang.Object is "not designed for
extension," is there any hope left for the language?
Enforcing good style is difficult. I wish the purveyors of the
enforcement tools would realize it's beyond their powers to do it well.