Naming conventions for Interface + Implementation classes?

Discussion in 'Java' started by Robin Wenger, Feb 3, 2011.

  1. Robin Wenger

    Robin Wenger Guest

    As I noticed there are two different naming conventions for an Interface+Implementation pair:

    1.) MyClass (=Interface) + MyClassImpl (=Implementation)

    or

    2.) IMyClass (=Interface) + MyClass (=Implementation)

    The first one is (AFAIK) from Sun Who defined originally the second one?

    What is recommended (for which scenario)?

    Robin
     
    Robin Wenger, Feb 3, 2011
    #1
    1. Advertising

  2. Robin Wenger

    Paul Cager Guest

    Re: Follow-up: [Was] Re: Naming conventions for Interface +Implementation classes?

    On Feb 3, 3:59 pm, Patricia Shanahan <> wrote:
    > I'm responding to an article that was posted to
    > comp.lang.java.programmer, but with follow-ups set only to
    > comp.lang.java.help. This article is cross-posted to the two newsgroups.

    ....
    > This sort of thing has happened often enough recently that there has to
    > be some underlying cause. Maybe something can be done to fix it.


    Is it something to do with arcor-online.net? That seems to be a common
    factor, though it could just be coincidence.
     
    Paul Cager, Feb 3, 2011
    #2
    1. Advertising

  3. Robin Wenger

    Lew Guest

    Peter Duniho wrote:
    > Reposting to include c.l.j.p because I didn't notice the original
    > message had idiotically been set for follow-ups only to c.l.j.h.
    >


    Robin Wenger has had pushback on the cross-posting issue several times
    before, but apparently has decided not to comply with the multiple
    requests to clean up their act.

    Robin Wenger wrote:
    >> As I noticed there are two different naming conventions for an Interface+Implementation pair:

    >


    As Pete noted, neither is really conventional.

    >> 1.)  MyClass (=Interface) + MyClassImpl (=Implementation)

    >
    >> or

    >
    >> 2.)  IMyClass (=Interface) + MyClass (=Implementation)

    >
    >> The first one is (AFAIK) from Sun  Who defined originally the second one?

    >


    Idiots.

    >> What is recommended (for which scenario)?

    >


    The second one, never. The use of 'Impl' for an implementing class is
    sometimes used pedagogically when the topic is interfaces and their
    implementations. In production code, both of those so-called
    "conventions" violate the principle of names that make sense in the
    problem domain.

    Peter Duniho wrote:
    > I don't think you'll find anyone using the word "Class" in an interface
    > name, nor the suffice "Impl" in the name of a class that implements an
    > interface.
    >


    Unless they use "Impl" in a discussion to emphasize coding principles,
    but generally Pete has the right of it.

    > So on the face of the it, the question doesn't make much sense.
    >
    > In Java, what I often see with respect to interfaces and implementations
    > are names like "MyInterface" and "MySpecificKindOfMyInterface".  E.g.
    > List vs ArrayList, Set vs HashSet, etc.
    >
    > That said, if you're mainly asking about whether to use a leading 'I' on
    > interface names or not, hard-core Java purists are unlikely to find that
    > kosher.  It's a convention followed in .NET and in the Windows API for
    > COM interfaces (and possibly Delphi, and other languages…), but I've
    > never seen it in any "official" Java code.
    >


    The Sun conventions suggest the use of names that are sensible in the
    problem domain.
    "Class names should be nouns, in mixed case with the first letter of
    each internal word capitalized. Try to keep your class names simple
    and descriptive. Use whole words-avoid acronyms and abbreviations
    (unless the abbreviation is much more widely used than the long form,
    such as URL or HTML)."
    <http://www.oracle.com/technetwork/java/
    codeconventions-135099.html#367>
    There's nothing in there about using "Class" (TERRIBLE idea!) or
    "Impl" as part of a type name, and the examples there don't show such
    a thing.

    Naming a type (forget class or interface - think "type") with
    implementation details ("I" for interface, "Impl" for class) violates
    the O-O principle to hide implementation and is a stupid idea. It
    benefits nothing, it imposes extra work if you change implementation
    (one of the motivators for black-boxing implementation) to avoid
    misleading nomenclature, and it bespeaks a lack of critical-thinking
    capacity.

    --
    Lew
     
    Lew, Feb 3, 2011
    #3
  4. Robin Wenger

    Tom Anderson Guest

    On Thu, 3 Feb 2011, Peter Duniho wrote:

    > On 2/3/11 5:14 PM, Robin Wenger wrote:
    >
    >> As I noticed there are two different naming conventions for an
    >> Interface+Implementation pair:
    >>
    >> 1.) MyClass (=Interface) + MyClassImpl (=Implementation)
    >> or
    >> 2.) IMyClass (=Interface) + MyClass (=Implementation)
    >>
    >> The first one is (AFAIK) from Sun Who defined originally the second one?


    I see it a lot in Microsoft land, so perhaps it originated there - they're
    certainly fond of pseudo-Hungarian notation elsewhere, and this is
    similar. I recall Delphi also had prefix letters (TSomeComponent and so
    on); i don't know if this is related.

    >> What is recommended (for which scenario)?

    >
    > I don't think you'll find anyone using the word "Class" in an interface
    > name,


    I assume that MyClass is a metasyntactic variable here.

    > nor the suffice "Impl" in the name of a class that implements an
    > interface.


    You're kidding me, right? Try this on for size:

    $ cd $JAVA_HOME
    $ find . -type f -name \*.jar -print0 | xargs -0 -n1 jar tf | egrep "Impl.class$" | sort -u

    With 1.6.0_22, that nets me 1423 hits. Now, not all of these are classes
    which follow the pattern ${InterfaceName}Impl, but enough are to disprove
    your belief.

    In fact, let's see a bunch:

    $ cd $JAVA_HOME
    $ export CLASSPATH=$(find . -name \*.jar | tr '\n' :)
    $ find . -type f -name \*.jar -print0 | xargs -0 -n1 jar tf \
    | egrep "Impl.class$" | fgrep -v '$' | cut -d . -f 1 | tr / . \
    | sort -u \
    | while read c; do
    iface=$(javap $c 2>/dev/null | egrep -o "implements [a-zA-Z0-9.]*" | cut -d ' ' -f 2);
    if [[ $iface == ${c%%Impl} ]]; then
    echo "$c implements $iface";
    fi;
    done

    And that's a rough survey which ignores anything involving inner classes,
    implementations which are in different packages to their interfaces, any
    kind of addition to the class name, classes implementing another interface
    ahead of their namesake, etc.

    I hasten to add that i do not recommend this naming convention; i'm simply
    demonstrating that it does occur in the wild.

    > In Java, what I often see with respect to interfaces and implementations
    > are names like "MyInterface" and "MySpecificKindOfMyInterface". E.g.
    > List vs ArrayList, Set vs HashSet, etc.


    Yup. That's the right way to do it.

    tom

    --
    I have a bias because it's a STUPID FUCKING IDEA. -- Brett Sikola
     
    Tom Anderson, Feb 3, 2011
    #4
  5. Robin Wenger

    Lew Guest

    On Feb 3, 12:43 pm, Peter Duniho <> wrote:
    > On 2/4/11 1:03 AM, Lew wrote:
    >
    > > [...]
    > > Naming a type (forget class or interface - think "type") with
    > > implementation details ("I" for interface, "Impl" for class) violates
    > > the O-O principle to hide implementation and is a stupid idea.  It
    > > benefits nothing, it imposes extra work if you change implementation
    > > (one of the motivators for black-boxing implementation) to avoid
    > > misleading nomenclature, and it bespeaks a lack of critical-thinking
    > > capacity.

    >
    > See Robin?  Told you so.  :)
    >
    > Personally, given the fundamental distinction between how one can use an
    > interface and a class (and in particular, the inheritability of either),
    > I see nothing wrong with 'I' for interface, and even find it helpful.  I
    > also feel I have above-average critical-thinking capacity, though
    > obviously Lew disagrees.
    >
    > But he and others will impugn whomever they need to in order to support
    > their blind faith in Sun's conventions.  Ignore their dogma at your own
    > risk!  Heaven forbid you should actually post any code here that doesn't
    > comply.  Lew will pick it to pieces, leaving nothing but shredded ego
    > and a bit of "for" loop here, a smidgen of inner class there.
    >


    Christ, Pete!

    Well, I guess it's all right to shred the messenger if you can't
    really speak to the message.

    I have not said anything about "faith" in the conventions. The OP's
    question was explicitly about the naming conventions. How can I
    answer that without reference to the conventions documentation, hm?
    Riddle me that!

    I know you have contempt for the conventions; you said as much in your
    earlier post:
    "I do all sorts of things, naming- and formatting-wise, in Java that
    Java purists hate. :)"

    So your bias is well known.

    But the OP did ask about conventions, and the conventions are as I
    stated.

    As for their value, well, the key in Java programming really is
    types. There is a widespread practice, as you stated, of naming an
    interface vs. a class (correcting your example) as "MyType" and
    "MySpecificKindOfMyType". The type names are supposed to represent
    functionality in the domain of discourse, so 'List' is the general
    contract for a certain collection type and 'ArrayList' is a subtype
    with specific properties. The names indicate the problem-domain
    (collections) characteristics.

    I'm not going to get into a snarky religious war with you, Pete.
    Flame away to your heart's content. I presented the case against
    Hungarian-style notational warts (they violate information hiding).
    If you want to present the case in their favor, that provides balance
    for those making a critical decision. If they want to base that
    decision on your statement that I'm picky, then they are using
    irrelevant criteria, so why even proffer such?

    Maybe it's that lack of critical thinking ...

    --
    Lew
     
    Lew, Feb 3, 2011
    #5
  6. Robin Wenger

    Arne Vajhøj Guest

    On 03-02-2011 04:14, Robin Wenger wrote:
    > As I noticed there are two different naming conventions for an Interface+Implementation pair:
    >
    > 1.) MyClass (=Interface) + MyClassImpl (=Implementation)
    >
    > or
    >
    > 2.) IMyClass (=Interface) + MyClass (=Implementation)
    >
    > The first one is (AFAIK) from Sun Who defined originally the second one?
    >
    > What is recommended (for which scenario)?


    The second is part of the common hungarian naming
    convention used in Windows C/C++ programming. Especially
    in COM programming the IThingys are everywhere. .NET
    ditched hungarian naming convention in general but kept
    it for interfaces. Possibly to make it easier for
    interop with COM. But very much a MS convention. It
    is very rare to see it in the Java world (even though I
    think there is somewhere it is used).

    The first is the convention introduced by SUN for Java RMI.
    Some people have suggested it being used for Java
    in general, but that idea has never caught on. It is used
    for RMI and not for anything else in the Java world. I
    don't think I have seen it used outside of Java.

    The Java coding convention:
    http://www.oracle.com/technetwork/java/codeconventions-135099.html#367
    just specify plain nouns in mixed case for both classes
    and interfaces.

    I think it is relative common for an interface Foo to
    have implementing classes be either FooBar or BarFoo where
    Bar somehow indicates something about the implementation.

    Arne
     
    Arne Vajhøj, Feb 3, 2011
    #6
  7. On 03-02-2011 05:25, Peter Duniho wrote:
    > On 2/3/11 5:14 PM, Robin Wenger wrote:
    >> As I noticed there are two different naming conventions for an
    >> Interface+Implementation pair:
    >>
    >> 1.) MyClass (=Interface) + MyClassImpl (=Implementation)
    >>
    >> or
    >>
    >> 2.) IMyClass (=Interface) + MyClass (=Implementation)
    >>
    >> The first one is (AFAIK) from Sun Who defined originally the second one?
    >>
    >> What is recommended (for which scenario)?

    >
    > I don't think you'll find anyone using the word "Class" in an interface
    > name, nor the suffice "Impl" in the name of a class that implements an
    > interface.


    It is very common for RMI.

    Arne
     
    Arne Vajhøj, Feb 3, 2011
    #7
  8. Robin Wenger

    Arne Vajhøj Guest

    On 03-02-2011 12:03, Lew wrote:
    > Peter Duniho wrote:
    > Robin Wenger wrote:
    >>> As I noticed there are two different naming conventions for an Interface+Implementation pair:

    >
    > As Pete noted, neither is really conventional.
    >
    >>> 1.) MyClass (=Interface) + MyClassImpl (=Implementation)

    >>
    >>> or

    >>
    >>> 2.) IMyClass (=Interface) + MyClass (=Implementation)

    >>
    >>> The first one is (AFAIK) from Sun Who defined originally the second one?

    >
    > Idiots.
    >
    >>> What is recommended (for which scenario)?

    >
    > The second one, never. The use of 'Impl' for an implementing class is
    > sometimes used pedagogically when the topic is interfaces and their
    > implementations. In production code, both of those so-called
    > "conventions" violate the principle of names that make sense in the
    > problem domain.
    >
    > Peter Duniho wrote:
    >> I don't think you'll find anyone using the word "Class" in an interface
    >> name, nor the suffice "Impl" in the name of a class that implements an
    >> interface.

    >
    > Unless they use "Impl" in a discussion to emphasize coding principles,
    > but generally Pete has the right of it.


    Not doing much RMI coding??

    :)

    Arne
     
    Arne Vajhøj, Feb 3, 2011
    #8
  9. Robin Wenger

    Esmond Pitt Guest

    On 3/02/2011 9:25 PM, Peter Duniho wrote:
    > I don't think you'll find anyone using the word "Class" in an interface
    > name, nor the suffice "Impl" in the name of a class that implements an
    > interface.


    MyRemoteImpl is widely used in RMI systems, which are not themselves
    widely used ;-). For example sun.rmi.server.RegistryImpl. I never liked it.
     
    Esmond Pitt, Feb 3, 2011
    #9
  10. Robin Wenger

    Lew Guest

    Peter Duniho wrote:
    >>> I don't think you'll find anyone using the word "Class" in an interface
    >>> name, nor the suffice "Impl" in the name of a class that implements an
    >>> interface.


    Lew wrote:
    >> Unless they use "Impl" in a discussion to emphasize coding principles,
    >> but generally Pete has the right of it.


    Arne Vajhøj wrote:
    > Not doing much RMI coding??
    >
    > :)


    I said "generallY"! RMI is a special case and its domain of discourse is
    programming to begin with.

    --
    Lew
    Ceci n'est pas une fenêtre.
    ..___________.
    |###] | [###|
    |##/ | *\##|
    |#/ * | \#|
    |#----|----#|
    || | * ||
    |o * | o|
    |_____|_____|
    |===========|
     
    Lew, Feb 3, 2011
    #10
  11. "Arne Vajhøj" <> wrote in message
    news:4d4b199b$0$23755$...
    > On 03-02-2011 04:14, Robin Wenger wrote:
    >> As I noticed there are two different naming conventions for an
    >> Interface+Implementation pair:
    >>
    >> 1.) MyClass (=Interface) + MyClassImpl (=Implementation)
    >>
    >> or
    >>
    >> 2.) IMyClass (=Interface) + MyClass (=Implementation)
    >>
    >> The first one is (AFAIK) from Sun Who defined originally the second one?
    >>
    >> What is recommended (for which scenario)?

    >
    > The second is part of the common hungarian naming
    > convention used in Windows C/C++ programming. Especially
    > in COM programming the IThingys are everywhere. .NET
    > ditched hungarian naming convention in general but kept
    > it for interfaces. Possibly to make it easier for
    > interop with COM. But very much a MS convention. It
    > is very rare to see it in the Java world (even though I
    > think there is somewhere it is used).
    >
    > The first is the convention introduced by SUN for Java RMI.
    > Some people have suggested it being used for Java
    > in general, but that idea has never caught on. It is used
    > for RMI and not for anything else in the Java world. I
    > don't think I have seen it used outside of Java.
    >
    > The Java coding convention:
    > http://www.oracle.com/technetwork/java/codeconventions-135099.html#367
    > just specify plain nouns in mixed case for both classes
    > and interfaces.
    >
    > I think it is relative common for an interface Foo to
    > have implementing classes be either FooBar or BarFoo where
    > Bar somehow indicates something about the implementation.


    Also for an abstract base class for implementations of Foo to be called
    AbstractFoo.
     
    Mike Schilling, Feb 4, 2011
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =B=
    Replies:
    4
    Views:
    9,293
  2. Floppy Jellopy

    Namespaces and Naming conventions

    Floppy Jellopy, Jul 20, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    531
    Kevin Spencer
    Jul 21, 2005
  3. Josh  Wolf
    Replies:
    2
    Views:
    3,266
    Martin Robins
    Mar 31, 2006
  4. Jean-Paul Calderone
    Replies:
    3
    Views:
    264
    Neil Cerutti
    Aug 30, 2006
  5. Daniel Dyer

    Interface naming conventions

    Daniel Dyer, Sep 7, 2006, in forum: Java
    Replies:
    17
    Views:
    17,947
Loading...

Share This Page