We are having this discussion in our team about code conventions for Java for:
interface
:Foo
orIFoo
orFooInterface
?abstract
:Foo
orAbstractFoo
?Enums
:Foo
orFooEnum
?
I'm basically trying to put my personal preferences aside :) so reasons to back up one or other convention are very welcome.
9 Answers 9
In Java: Foo
, AbstractFoo
and Foo
- although AbstractFoo
could just be Foo
.
Evidence:
java.util.List
(interface)java.util.AbstractList
(abstract class)java.util.Formatter.BigDecimalLayoutForm
(enum)
For the interface part, see the Naming Conventions section of the Java Coding Conventions document. It doesn't talk about enums and abstract classes though.
-
1Interfaces, as far as I am aware, have a JavaBeans naming convention that dictates that they should be named as an adjective, usually ending in
able
. So ifFoo
were an action the interface should be namedFooable
.Rudi Kershaw– Rudi Kershaw2013年08月12日 10:36:38 +00:00Commented Aug 12, 2013 at 10:36 -
@RudiKershaw: Not always... and my experience is that that has decreased over time, as well. Counter-examples include
CharSequence
, all the collections, the various DOM interfaces.Jon Skeet– Jon Skeet2013年08月12日 10:49:16 +00:00Commented Aug 12, 2013 at 10:49 -
The JavaBeans conventions are only truly applicable for code written for plugin reusable software components, so a lot of the conventions seem to be largely ignored for standard purposes. But I like to use the
Fooable
, naming because it just helps me differentiate from normal classes at a glance.Rudi Kershaw– Rudi Kershaw2013年08月12日 10:52:49 +00:00Commented Aug 12, 2013 at 10:52 -
Sad! Sun was sold to Oracle. :(user1459961– user14599612014年05月30日 15:42:06 +00:00Commented May 30, 2014 at 15:42
From my blog:
- Foo - The interface ultimately defines the concept, so it should have the best name.
- AbstractFoo - An abstract implementation intended to be used as the base of a hierarchy of classes.
- BaseFoo - An implementation intended to be used as the base of a hierarchy of classes, where the base class could be used on its own if necessary.
- DefaultFoo - A "default" implementation that would be appropriate for the majority of typical use cases.
- SimpleFoo - A "simple" implementation with no unexpected functionality, perhaps as an example or as a mock. A simple POJO would be a good "simple" implementation.
- {Descriptive}Foo - Other implementations should describe what makes them unique.
The blog also discusses reasons against some of the other names.
interfaces: Foo
Reason: Your code must not need to know that they are dealing with an interface. Writing 'IFoo' does just that. Instead, Foo makes it clear that 'Foo' is generic, and the object behind it may be a 'NumFoo' or a 'StrFoo'. The code really need not care.
abstract classes: AbstractFoo
Reason: your code is never going to use this class directly. You will always subclass this class to make any classes that are used by other code. So it must be amply clear to a programmer that the class is an abstract one. And what better way to name it Abstract! Places where you need to use references of type AbstractFoo, you should reconsider using an interface instead. (Ofcourse, this is not possible in C++)
Enums: FooType or FooEnum. Personally, FooType is better because Type relates more easily to the "real world" that Enum does.
Cheers!
No special conventions.
Having special naming conventions for these kinds of classes is basically a form of Hungarian notation (the bad kind): the information it gives you is already present in the syntax and is usually made easily available by IDEs e.g. when you hover over the name. Putting it into the name itself is pointless and ugly.
Class names should simply describe the class's role as well as possible. This can lead to "natural" naming conventions - a very good example is the Java convention of naming interfaces with an -able suffix (Iterable, Comparable) - but I don't want to imagine the result if it were universally enforced and List, Map, etc. had to follow it.
-
1but what if you have many close-by-functionality-classes? the conventions free you to not waste meaningful names. for Foo classes branch you just use IFoo, AbstractFoo, Foo, ExtendedFoo and happy, while without, what names will you choose? Foo, Foo2, Foo3, SuperFoo?Imaskar– Imaskar2009年07月01日 10:53:04 +00:00Commented Jul 1, 2009 at 10:53
-
1Not having conventions doesn't mean you're not allowed to name classes like that, only that you don't have to use such names everywhere. But in your example, I'd have Foo as the interface, AbstractFoo is necessary since interfaces and classes share the same namespace, but the concrete classes should have distinctive names that describes their concrete aspect.Michael Borgwardt– Michael Borgwardt2009年07月01日 11:07:39 +00:00Commented Jul 1, 2009 at 11:07
-
2An example from Java: Map is the interface, and then there's AbstractMap, HashMap, TreeMap, LinkedHashMap, etc...Michael Borgwardt– Michael Borgwardt2009年07月01日 11:08:41 +00:00Commented Jul 1, 2009 at 11:08
My convention:
- interface: Foo
- abstract: it depends FooAdaptor or AbstractFoo or BaseFoo
- enum: Foo or Foos
I really dislike using I in interface names or even FooInterface:
interface FooInterface {
is like writing:
class FooClass {
or even:
abstract class AbstractFooClass {
it is simply prolix.
-
Class names are generally supposed to be singular nouns. Thus, Foos probably isn't a good name for an enum, (although Foo is).James– James2009年06月17日 14:41:48 +00:00Commented Jun 17, 2009 at 14:41
-
@James You can strike the "probably" through in your last sentence.Torsten– Torsten2014年07月16日 08:11:09 +00:00Commented Jul 16, 2014 at 8:11
My convention:
- Interface:
Foo
; - Abstract:
AbstractFoo
; - Enum: usually
Foo
but in some circumstancesFooType
.
IFoo
is very .Net, not Java. FooInterface
I've never seen used.
-
IFoo is also used throughout the Eclipse codebase, which is very Java, but they could very well have picked it up from .NET.Peter Dolberg– Peter Dolberg2009年06月17日 14:23:11 +00:00Commented Jun 17, 2009 at 14:23
-
1Although I pointed out that it's used in Eclipse, I'm not recommending it. In fact, I prefer Foo to IFoo.Peter Dolberg– Peter Dolberg2009年06月17日 14:26:12 +00:00Commented Jun 17, 2009 at 14:26
-
Also, even though Eclipse was written in Java, it does not mean they used a Java style.Cephalopod– Cephalopod2011年08月17日 11:48:29 +00:00Commented Aug 17, 2011 at 11:48
Regarding the interfaces I personaly like:
Fooable
-
Fooable is to foo (with) it, where 'foo' is a verb.Adam Hošek– Adam Hošek2014年08月01日 12:21:35 +00:00Commented Aug 1, 2014 at 12:21
About interfaces:
I prefer IFoo because it’s a talking name, telling you it is an inferface right away. Second, for modules etc. where you do an interface for just one class, the class often has the same name as the interface. Then you can use Foo extends IFoo. Otherwise, well, you’d have to find a name. Or use FooInterface or whatever ...
java.util.list as stated uses Foo. This is no problem as classes with different concepts implement it, thus already suggesting a different name (ArrayList, LinkedList ...). I’m not quite sure if I really would prefer IList there. Dunno ... :P
-
1In most situations, it is not relevant whether a type is a class or an interface; but your class and interface have the same name, that's probably a sign of bad design.Cephalopod– Cephalopod2011年08月17日 11:46:35 +00:00Commented Aug 17, 2011 at 11:46
Here's convetion used in my DEV team in ION.
Interface
interface IMyInterface
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
abstract class MyAbstract
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
enum EMyEnumeration
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
-
Is this in ION Trading Inc.? the maker of MarketView?endless– endless2013年07月09日 13:27:09 +00:00Commented Jul 9, 2013 at 13:27
-
1sounds like just the kind of company i don't want to work for :-)Jonathan E. Landrum– Jonathan E. Landrum2020年05月07日 01:35:50 +00:00Commented May 7, 2020 at 1:35