Naming classes like "com.facebook.FacebookClient" vs "com.facebook.Client"
I'm looking 开发者_JAVA技巧for opinions or if there is an agreed way of doing this, regarding to naming namespaced classes.
E.g.:
com.facebook.FacebookClient
vs
com.facebook.Client
Or
javax.script.ScriptEngine;
vs
javax.script.Engine;
I'm currently prefer the first name in each example but the extra word seems a bit wasteful.
Using Actionscript as an example, I'd say FacebookClient over Client. For this reason:
import com.facebook.Client;
import com.twitter.Client;
You'd have to refer to the class by it's full package to create an instance in the same class:
new com.facebook.Client();
If it was FacebookClient, I could have both
new FacebookClient();
new TwitterClient();
Plus Client would be annoying to me when code completion pops up. An extra click to select the correct Client ;)
I generally go with something like:
[company name].[project name].[functional area].[category]
i.e.
com.dave.megaproject.dataaccesslayer.postcodelookup
or in the .net world:
namespace DaveFirm.MegaProject.DataAccessLayer.PostCodeLookup
Does the facebook domain have other kinds of clients? Does the javax.script domain have other kinds of engines? Are they likely to want to in future? If so, you need something to differentiate them. Otherwise it might be wasted space.
Of course, the future is almost never certain.
The only thing I have to add is if you were to use using
directives in C#. If you had two namespaces with the same method names like client
then there would be a confict/confusion.
In a language where class name and filename are one-to-one, there's an argument to be made for namespacing the classname, regardless of the package. I lot of people locate files by classname, either by opening the file named <class>.java
or by use of a "Quick Open" in their IDE. The extra bit of mental hurdling to decide which Client.java
you need can be enough of an annoyance that you appreciate the extra naming.
And, it makes things easier to read and it's not like in any modern editor (e.g. vi or greater) you ever need to type it out more than once (sometimes not even once).
I'll go with the FacebookClient style extra-word every time just because the more descriptive the smallest form of the name is, the easier it is to find references to that name in multifile searches.
精彩评论