Java Programming Conventions (Part I)

If you ask me ‘What Programming is?’, I will reply something like ‘It’s the process of creating art’. We always say ‘Code is poetry’ and I regard poetry as greatest form of art.

OK, that’s just me, expressing my artistic views. We programmer always like to present our self like artist, creator of something and that something is software.

Previously, I posted about clean coding and is related to this post(What’s clean coding & not), which I hope will be lot of help. Now, I gonna discuss about programming conventions for Java. But this convention is applicable to many, not just Java, so hoop on.

To start, I will go for basic intro :

  • Software is developed by team of programmer. Good communication is must, which means understand each other and their codes.
  • Most of software life period goes for maintenance and support.
  • Code convention increases readability and efficiency of software.

I hope you got the faintest idea of what I’m saying. If not, no worries, just carry reading on.

1. Package Naming

The package name should contain domain name and must be in lower case.
Do :
Don’t : application.ui
The initial package name representing the domain name must be in lower case.

2. Variable Naming

Variable naming use what we call camel-back notation. Start naming with lowercase and use uppercase as need.
Do : myVariable, eastDirection
The variables name must start from lower case (Camel back notation). It easier to read and understand.
Names representing constants (final variables) must be all uppercase using underscore to separate words.
Do : void connect(Database database);
Don’t : void connect(Database db);
Generic variables should have the same name as their type.

3. Method Naming

This is identical to variable names, but methods in Java are already distinguishable from variables by their specific form.
Do : getFirstName() , audioVolumeController()
Do : importXmlSource()
Don’t : importXMLSource()
For Abbreviations and acronyms, using all uppercase for the base name will give conflicts.

4. Get/Set

The get/set term be used where an attribute is accessed directly.
getName(), setFirstName()
It clearly define what this method will do

5. Variable Name Length

While declaring variables the length of it depends upon the type. Like, if it is temporary, then we use short name.
int i, j
A programmer reading such variables should be able to assume that its value is not used outside a few lines of code.

6. I don’t need to tell this but…

All names should be written in English.
The most of programming is done in English as it is internationally recognized. Program written in local language are understood only to local programmer and other who don’t know that language won’t get a clue.

7. Name of object should be implicit

To simply put this is quite common mistake a most of us often do make.
Do : rectangle.getArea();
Don’t : rectangle.getReactangleArea();
Later don’t make sense and it’ll result in confusion.

8. Variable declaration of element type

I find this particular matter dependent on the individual programmers. If I have a text field then how to declare variable for it.
Do : nameTextField, sortingJTable
Don’t : textFieldName, jTableSorting
Appending type names to variable as suffix helps to increase readability. It provides the user an immediate clue of the type of the variable and thereby the available resources of the object. Which is difficult to read and understand. Difference is eminent and clear.


Why not add sensible comments to your code?
I found commenting can be real problem when programmers don’t do it actually. They are like the summary or excerpt defining what the following method do and what parameters it take. Please, comment. Not just for other sakes, for you also. You don’t want pulling your hairs some few years back when you don’t understand you own codes.

Leave a comment

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.