Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Naming conventions for Interface + Implementation classes?

Reply
Thread Tools

Naming conventions for Interface + Implementation classes?

 
 
Robin Wenger
Guest
Posts: n/a
 
      02-03-2011
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

 
Reply With Quote
 
 
 
 
Paul Cager
Guest
Posts: n/a
 
      02-03-2011
On Feb 3, 3:59*pm, Patricia Shanahan <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      02-03-2011
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
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      02-03-2011
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 ****ING IDEA. -- Brett Sikola
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-03-2011
On Feb 3, 12:43*pm, Peter Duniho <(E-Mail Removed)> 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
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      02-03-2011
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/ja...35099.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




 
Reply With Quote
 
Arne Vajh├Şj
Guest
Posts: n/a
 
      02-03-2011
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
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      02-03-2011
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
 
Reply With Quote
 
Esmond Pitt
Guest
Posts: n/a
 
      02-03-2011
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.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-03-2011
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|
|_____|_____|
|===========|
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
Interface naming conventions Daniel Dyer Java 17 09-08-2006 02:33 PM
Me vs. .NET HTML Control naming conventions Josh Wolf ASP .Net 2 03-31-2006 12:37 PM
Namespaces and Naming conventions Floppy Jellopy ASP .Net 4 07-21-2005 01:36 PM
Naming conventions for ASP.NET objects? =B= ASP .Net 4 09-06-2004 09:05 AM



Advertisments