What is standard for Interface naming in java api
I am asked by one of colleague about the Throwable
class in java API.
As per standard, I do understand, every word ending *able is a interface in java API. There is a industry standard about using such words as Interface names. So, I unknowingly, told him about this as base interface for 开发者_JAVA技巧all the exception and error types in java world. Then he shows me the java file for this class.
My questions:
Why java people has choosen this name to be a class. I think this should have been a interface by default?
Is this a pattern to use *able words as interface?
Is there any other example of class ending with *able?
Regards.
Nouns are always used to name classes, but Throwable is an exception.
(See what I did there?)
It's very common for those '-able' names to be interfaces in Java, but there is no official convention for interface naming that I've found that suggests that '-able' names should be interface names, though typically that is the case.
Official Java naming conventions can be found here - it's pretty lean, there really aren't any restrictions for class or interface naming:
- http://www.oracle.com/technetwork/java/codeconventions-135099.html#367
As to your Throwable
question, James Gosling once answered why it's a class rather than an interface, even though the name was more fitting for an interface.
Unfortunately, the original article from Sun/Oracle's site has vanished into the internet ether, so I can only provide indirect attribution:
- http://c2.com/cgi/wiki?JavaExceptionQuestion
- http://www.ibm.com/developerworks/forums/thread.jspa?threadID=58994&tstart=45
edit: Since I continue to get upvotes to this question, I found the link to the Sun discussion via the Wayback Machine, here: http://web.archive.org/web/20071013225816/http://java.sun.com/features/2002/03/gosling.html?source=jdc_news&date=20020430
JDC: Why is Throwable not an interface? The name kind of suggests it should have been. Being able to catch for types, that is, something like try{}catch (), instead of only classes. That would make the Java programming language much more flexible.
JG: The reason that the Throwable and the rest of those guys are not interfaces is because we decided, or I decided fairly early on. I decided that I wanted to have some state associated with every exception that gets thrown. And you can't do that with interfaces; you can only do that with classes. The state that's there is basically standard. There's a message, there's a snapshot, stuff like that that's always there. and also, if you make Throwable an interface the temptation is to assign, to make any old object be a Throwable thing. It feels stylistically that throwing general objects is probably a bad idea, that the things you want to throw really ought to be things that are intended to be exceptions that really capture the nature of the exception and what went on. They're not just general data structures.
There are others such as
- Activatable
- Observable
And of course there are plenty of interfaces that don't end in -able. Some people like to prefix all their interface names with an 'I' (IAdjustable instead of Adjustable). Like code formatting wars, their isn't universal agreement. Sun has some suggestions but they are pretty vague.
Interface names should always be an adjective and if possible should end in "able" has been the trend in naming conventions within the Java community. That does not have to be followed strictly, its just a naming convention, there is nothing to stop you from naming the interface/class whatever you want.
Yes, see http://www.iwombat.com/standards/JavaStyleGuide.html#Class%20and%20Interface%20Names
Clonable
*Note, Interface names should generally be adjectives, whereas class names should generally be nouns.
Check out page 15 of this style guide document released by Sun. http://java.sun.com/docs/codeconv/CodeConventions.pdf
There is also some debate as to if it is a good style to add the letter I to interfaces. (Example: ICat, IDog, etc.), but that generally applies to other languages and not to Java per say.
In my personal opinion a convention is just a "rule of thumb", if the convention gets in the way of the readability of your code, go with a more descriptive interface/class name instead of the one that follows convention, but if your really struggling to come up with a good descriptive class name, maybe you need to think about the functionality of your class/interface again a bit more.
We are talking about naming conventions here and yes, *able is the preferred convention for naming interfaces. As I'm sure you've seen, there are always exceptions. For example, System.arraycopy
is not camel cased.
精彩评论