Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface naming in Java [closed]

Most OO languages prefix their interface names with a capital I, why does Java not do this? What was the rationale for not following this convention?

To demonstrate what I mean, if I wanted to have a User interface and a User implementation I'd have two choices in Java:

  1. Class = User, Interface = UserInterface
  2. Class = UserImpl, Interface = User

Where in most languages:

Class = User, Interface = IUser

Now, you might argue that you could always pick a most descriptive name for the user implementation and the problem goes away, but Java's pushing a POJO approach to things and most IOC containers use DynamicProxies extensively. These two things together mean that you'll have lots of interfaces with a single POJO implementation.

So, I guess my question boils down to: "Is it worth following the broader Interface naming convention especially in light of where Java Frameworks seem to be heading?"

like image 412
Allain Lalonde Avatar asked Feb 12 '09 15:02

Allain Lalonde


People also ask

What is the naming convention for interface in Java?

Interface names should be capitalized like class names. Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter.

How do you name an interface?

16.4 Interface Naming. Each network interface has a name. This usually consists of a few letters that relate to the type of interface, which may be followed by a number if there is more than one interface of that type. Examples might be lo (the loopback interface) and eth0 (the first Ethernet interface).

Should class names be capitalized Java?

In Java, class names generally should be nouns, in title case with the first letter of each separate word capitalized.

Should constants be capitalized in Java?

The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_"). (ANSI constants should be avoided, for ease of debugging.)


1 Answers

I prefer not to use a prefix on interfaces:

  • The prefix hurts readability.

  • Using interfaces in clients is the standard best way to program, so interfaces names should be as short and pleasant as possible. Implementing classes should be uglier to discourage their use.

  • When changing from an abstract class to an interface a coding convention with prefix I implies renaming all the occurrences of the class --- not good!

like image 128
starblue Avatar answered Sep 20 '22 16:09

starblue