Reputation: 17795
We are having this discussion in our team about code conventions for Java for:
interface
: Foo
or IFoo
or FooInterface
?
abstract
: Foo
or AbstractFoo
?
Enums
: Foo
or FooEnum
?
I'm basically trying to put my personal preferences aside :) so reasons to back up one or other convention are very welcome.
Upvotes: 32
Views: 23291
Reputation: 13
Here's convetion used in my DEV team in ION.
Interface
interface IMyInterface
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
abstract class MyAbstract
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
enum EMyEnumeration
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Upvotes: -2
Reputation: 63365
From my blog:
The blog also discusses reasons against some of the other names.
Upvotes: 22
Reputation: 9217
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
Upvotes: 1
Reputation: 346260
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.
Upvotes: 5
Reputation: 26583
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!
Upvotes: 19
Reputation: 116304
My convention:
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.
Upvotes: 3
Reputation: 625007
My convention:
Foo
;AbstractFoo
;Foo
but in some circumstances FooType
.IFoo
is very .Net, not Java. FooInterface
I've never seen used.
Upvotes: 2
Reputation: 1499800
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.
Upvotes: 35