wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Proper naming of Packages/Classes. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Proper naming of Packages/Classes." Watch "Proper naming of Packages/Classes." New topic
Author

Proper naming of Packages/Classes.

Alex Petsche
Ranch Hand

Joined: Oct 29, 2012
Posts: 42

So far I've been naming my packages and classes close to the same name (as netbeans auto generates the main class based off of the package name), but I've seen different ways that are supposed to be the industry standard. I've been naming the package as what it was doing as in "Calculator", but I've heard that package names are supposed to have a backwards web address followed by what the package is. I did note though that they said to use a company URL or an email. Is this so that the code can easily be referenced as to where it came from and should I start this now so that I'm used to it when I get a job as a programmer? Lastly I've seen that classes can be names anything, but it is good practice to name the class as what it is for. Recently I've taken to the habit of naming them ch6_P386_N24_10_30_2012. It has the chapter of the book_page number_problem number_and then month_day_year. Since it's for my class and I don't plan on reusing it I've been naming it that to easily check off problems we are assigned to do, but I'm wondering if I should name them something else so that when I code something professionally it will be easier for others to understand. For example those that don't have my book the title wouldn't be relevant to them.

In a nutshell would this be correct for a package and class names?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60766
    
  65

Firstly, please learn to use paragraphs. Large chunks of unbroken text are very hard to read.

Alex Petsche wrote:but I've heard that package names are supposed to have a backwards web address followed by what the package is

This convention helps to minimize the possibility of naming collisions. For example, I own bibeault.org. No one else does. So if I prefix my packages with org.bibeault, no one else should ever create a package that starts with that prefix, as no one else owns that domain.

Also, get used to using all lowercase for package names. Using mixed case is a sure sign of a novice.

I did note though that they said to use a company URL or an email. Is this so that the code can easily be referenced as to where it came from

Again, to avoid collisions.

should I start this now so that I'm used to it when I get a job as a programmer?

Yes. Using proper conventions from the start means fewer bad habits to unlearn later.

Lastly I've seen that classes can be names anything, but it is good practice to name the class as what it is for. Recently I've taken to the habit of naming them ch6_P386_N24_10_30_2012.

Eeesh!

Class names should start with a capital, not contain underscores, and use camel case. Usually they are nouns that name what the model. Cat, Dog, SevereWeatherData, CustomerCredentials, TimeZone, RadarService, and so on.

Kudos for asking. Following the naming conventions may seem like a small thing at first, but it's something all competent professionals do, and not following the conventions makes code surprisingly hard to read.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Alex Petsche
Ranch Hand

Joined: Oct 29, 2012
Posts: 42

Thanks for the reply! I've always heard about standards for naming, but I never heard why they were used other than my own speculation.

I'll try to remember what you've posted and start using that, so it'll be an easier transition or not one at all when I know more and am getting paid for to code!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Alex Petsche wrote:Thanks for the reply! I've always heard about standards for naming, but I never heard why they were used other than my own speculation.


When you're dealing with dozens or hundreds of packages, and hundreds or thousands of classes, if there's not some kind of logical structure to how they're arranged and named, you're going to be in a world of hurt trying to find anything, or figure out what it does, or what else it relates to. Even with good naming it's hard enough.

Also, just to clear up some terminology for you:
I've heard that package names are supposed to have a backwards web address followed by what the package is. I did note though that they said to use a company URL or an email


It's a domain name, not a "web address" or URL or email.

homer@springfieldnuclear.com is an email address.
http://www.springfieldnuclear.com is a URL.
springfieldnuclear.com is the domain in both of the above.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37945
    
  22
There is a Java Tutorials Section about package names. They use somewhat different conventions from other names.
Alex Petsche
Ranch Hand

Joined: Oct 29, 2012
Posts: 42

Thanks for the link it explained some more on how they name packages, but what should I do since I don't own a domain? I listed I have an e-mail, but that would be a subdomain of gmail.com if I understand that right. Can I use that or do I need to use a domain to correctly name a package?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60766
    
  65

According to GoDaddy.com alexpetsche.me is not in use.

So me.alexpetsche seems safe enough.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Alex Petsche wrote:Thanks for the link it explained some more on how they name packages, but what should I do since I don't own a domain? I listed I have an e-mail, but that would be a subdomain of gmail.com if I understand that right. Can I use that or do I need to use a domain to correctly name a package?


If you're just writing code for yourself and friends or family, anything is fine. Just pick something that makes some logical sense and has some reasonable association to you, even if it's not official. Java doesn't actually care about domain names per se. It's just a recommended convention, because A) That keeps package names unique, so there won't be conflict when you get packages from multiple vendors, and B) It makes it easy to see where a given class came from by its fully qualified name.

If you plan to actually distribute your sources or classes beyond your immediate personal circle, you'll want to make sure to use something that you have a legal right to (your personal name is probably still a good bet) and that's not likely to clash with something already in use.
Alex Petsche
Ranch Hand

Joined: Oct 29, 2012
Posts: 42

Ok thanks for the advice. I probably won't be distributing my code for business purposes for a while or outside my "personal circle", but I was curious as to what I would do if I didn't have a domain name. From your comment " (your personal name is probably still a good bet)" I could probably use petsche.alex.packageName or would I still need a domain as Bear Bibeault suggested alexpetsche.me? I guess I'm asking (in the general case) would my name be unique enough in theory so that I could use it?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

"Uncle Bob" Martin likes to say that your package structure should give you a sense of the architecture of the application. For example, if you were creating an accounting system, your top-level packages should have names that make it clear that you're dealing with an accounting system, e.g. names like "assets", "receivables", and "payables". I use packages to group together things that have more or less the same rate of change. Things like interface definitions go in one package. Implementations of those interfaces will go in different package(s). One design principle to keep in mind is to have dependencies that flow from less stable to more stable, not vice versa. That is, packages that are more stable should not depend on packages that are less stable. Violating this principle results in more pronounced ripple effect when a change is made.

Think of your house as an application. The different rooms in your house would be the different packages in your application. Each room contains objects with related functionality. A bedroom has mirrors, beds, closets, blankets. A kitchen would have a stove, a refrigerator, a sink, a table, chairs, drawers, utensils, etc. So in the same way, packages are a way to organize related objects in your application along the lines of usage and rates of change. So, in your example, does it make sense to have a Calculator class reside in a calculator package?

Remember, you don't have to settle on anything once you've done it. Software should be soft; you should be able to change things and move things around as you develop. I often just put things in one convenient place at first and then observe what happens when changes are made. When I see a lot of undesirable side-effects, I reorganize and isolate to eliminate or minimize the side-effects. This takes a lot of practice and careful study and reflection on the things you do.


Junilu - [How to Ask Questions] [How to Answer Questions]
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Alex Petsche wrote:Ok thanks for the advice. I probably won't be distributing my code for business purposes for a while or outside my "personal circle", but I was curious as to what I would do if I didn't have a domain name. From your comment " (your personal name is probably still a good bet)" I could probably use petsche.alex.packageName or would I still need a domain as Bear Bibeault suggested alexpetsche.me? I guess I'm asking (in the general case) would my name be unique enough in theory so that I could use it?


You don't need to own a domain. Java doesn't care. If and when you go into business, you will probably purchase a domain as a matter of course. It's pretty uncommon for a business--especially one that produces software--to not have it's own domain name. Until that time, let common sense guide you. For the top of your package structure, as I said, use something that's unlikely to cause clashes with another company/person/entity, and that makes some logical sense. For the structure below that, Junilu gave you some good advice. Think layers and groups of classes and packages that work together to provide some broader coherent function or feature. Look at existing package hierarchies in the core API and whatever third party libraries you're using for examples.
Alex Petsche
Ranch Hand

Joined: Oct 29, 2012
Posts: 42

Thanks for the replies it makes a lot more since now when I see other "professional" examples of classes and such. I'll keep this in mind and apply it as I can to my code. Thanks for the help!
 
Consider Paul's rocket mass heater.
 
subject: Proper naming of Packages/Classes.
 
Similar Threads
ArrayList problems
I'm having problems putting my own classes into packages
Problems with import/package statements
package naming and imports question
FBNS: Package structure review