Re: Named fields in enum constant declarations
Jim wrote:
Yet another method...Implement an Interface that defines the colors
public interface IColors {
Color LIGHT_GREEN = new Color(100,255,100);
}
Lew wrote:
This technique has a name: the Constant Interface Antipattern.
That's right, antipattern. Don't do that.
/Effective Java/, Joshua Bloch
<http://java.sun.com/docs/books/effective/> Item 19: Use interfaces only
to define types
And you don't prefix interfaces with "I" in Java, by convention.
<http://java.sun.com/docs/codeconv/index.html>
jebblue wrote:
Anti-pattern? I just tried it and the pedantic Java compiler complained
not at all.
Sigh.
Compilers don't enforce patterns or prevent antipatterns, duh.
Did you read the referenced /Effective Java/ item that talks about why it's an
antipattern?
Sun themselves warn against it in
<http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html>
... people sometimes put static members into an interface and
inherit from that interface. This is a bad idea. In fact, it's
such a bad idea that there's a name for it:
the Constant Interface Antipattern (see Effective Java ...)
Just because you can do something, doesn't mean you should. Java allows you
to write bad code. But don't.
And some places require interfaces to be prefixed with an "I".
And those places aren't following the Java conventions that have been in place
for about ten years now.
The best practice, Java-style, is to use interface names that express the type
represented by the interface, usually an adjective or attributive noun that
indicates the contract the interface enforces.
<http://java.sun.com/docs/codeconv/index.html>
They give example interface names of 'RasterDelegate' and 'Storing'. No "I"
prefix.
<http://java.sun.com/javase/6/docs/api/>
The only standard Java interfaces there that use an initial "I" to represent
the word "interface" are the CORBA interfaces, where "interface" has a
specific meaning different from the general Java meaning.
<http://java.sun.com/javaee/5/docs/api/>
Not one Java EE interface uses an initial "I" to represent the word "interface".
<http://commons.apache.org/collections/api-release/index.html>
ditto
<http://commons.apache.org/lang/api-release/index.html>
ditto
<http://commons.apache.org/logging/apidocs/index.html>
<http://commons.apache.org/math/api-1.2/index.html>
<http://commons.apache.org/net/apidocs/index.html>
<http://commons.apache.org/pool/api-1.4/index.html>
ditto, ditto, ditto and ditto.
<http://www.1t3xt.info/api/>
Nope.
<http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/index.html?overview-summary.html>
None here, either.
The "I" prefix doesn't contribute anything, so why defend it? Better would be
to move those shops away from the practice.
--
Lew