Java Naming Conventions

Java naming conventions are recommendations, rather than rules.  Naming conventions normally come as part of project standards.  These are designed to ensure that the code is written in a consistent way across different areas of the project and also even within the same class of method.

After a certain time a piece of code will have been written, and then maintained by several different people.  If everyone writes in a different way this code is going to become more and more difficult to read.

Think of it like a book that has been written by different people with different styles and you’re never quite sure who has written the next sentence.

It would be very difficult to read, as each style will interrupt your flow.  Java code is the same.  Remember, you only write it once, but it will be read plenty of times by you and many others.  It always pays to take the extra step to make it readable.

If the rules are broken the program won’t compile; the Java compiler will complain.  Howevere, if the conventions are broken the program will still compile and run but it will be left to your boss and your colleagues to do the complaining.

This is a very important point to know as a new developer.  It’s one of the small things that are easy to ignore, but following the standards closely will help everyone around you and get you noticed as a careful developer who listens to requirements.

This is one of those hidden rules that in the long term are going to directly affect your bank balance.  Writing code that is easy to read because it follows conventions will mark you out as someone who cares and someone who can be trusted.  It is really in your best interests to follow best practices and follow the coding conventions wisely.

Follow House Style

House style will change from project to project.  When you join a new project it is very easy to slip into your style and that normally tends to be the style you used on your last project.

Make sure you read the coding conventions and pay careful attention to the areas where they are different to the way you are used to.  Note these differences, so you can refer to them as necessary.

Ensure you use the new house style, not your style.

If there are no coding conventions, be proactive: suggest creating some.  It will help everyone in the long run and mark you out as someone who is proactive, and that’s never a bad thing.

Sun have a perfectly good set of coding conventions.  This is a very good starting point for your coding conventions if you need them.  You can use them just “as is” or document where you would like to deviate from these standards.

Naming Guidelines

Here are some further guidelines that will help you to produce good names for your variables.  They are not standards or conventions, just some guidelines to help you write readable code.

Try and follow these and you will be taking the first step towards self-documenting code, the perfect position where someone new can read your code and easily understand what is happening, without even the need for many comments.

Reveal Intention

Use several words to describe the intent of the variable.  Make it obvious what data the variable holds.

For example, what is more helpful?

Integer distance;

or

Integer distanceTravelledToday;

Ensure Distinction is Clear

If there are 2 variables holding similar data, ensure the name describes the differences between the 2 so it is obvious which is which.

Again, which is better?

Integer airport1, airport2;

or

Integer departureAirport, arrivalAirport;

Ensure you can pronounce the name

Don’t try and shorten the name to save space.  Some people remove the vowels.  This just makes it harder to talk to your colleagues about.

With the autocomplete feature in Eclipse it is rare to ever need to type the whole variable name.  Usually the first few characters will do, so don’t sacrifice readability for typing effort.

Again, which is easier?

Integer depApt, arrApt;

or

Integer departureAirport and arrivalAirport?

Hungarian Notation

If you’re old enough to know what Hungarian notation is, then make sure you aren’t using it.  If you don’t know what it is, don’t worry, you’re very unlike to be using it unknowingly, but if you want to know more, try Googling it.

Whatever, don’t use it.  It just hinders the autocomplete feature within Eclipse.

Use One Name for a Concept

Within the team agree a single name to be used for each concept and ensure that everyone sticks to it.

plane, aeroplane, aircraft, airplane.

Are all these referring to the same concept?  Sounds like it.  Use the same name, then.  If not, it is time for some more meaningful names.

A Name is not Forever!

Final point to note:  Don’t obsess too much about getting the name just right to start with.  The refactoring tool in Eclipse allows the names to be easily changed at a later date.  This also means that if you have some bad names already in your project then it’s easy to improve these now.

This article has been taken from Day 6 – Naming Variables of our Free 21 Day Email Course.

{ 2 comments… read them below or add one }

Elvis January 22, 2011 at 1:41 am

Thank you for the tutorial on java naming conventions, is very clear and straight forward. I can’t wait for the next post…..

admin January 22, 2011 at 3:22 pm

Hi Elvis,

I am glad you found this straight forward, it is an important idea to understand.

If you would like some more posts please join our free Java Tutorial For Beginners. There is a form at the top right hand side of this page.

Thanks

Paul

Leave a Comment

Feedback Form