Java

Camel Casing Rule

Jul 7, 2020

Camel Casing Rule

This should be exactly the first thing you should come across when you start to learn the coding. There is a difference between Rules and Convention.

If Rules are broken, you will get a compile time error and believe me, with my years of experience I am saying that getting error is a good thing, it can actually help you to sort out the issue before even getting started.

But when Conventions are broken they can actually bring down a whole project on it's head , not for the working but for the internal management team.

Let me make you understand in this way, you are driving on a city highway, you stopped because you saw the red light, WHAT WILL YOU DO WHEN THE LIGHT TURNS GREEN?

I know the answer is quite simple, you will start driving as your light turn green, but sub consciously, your brain gave you an affirmation that the other guy out there must also know the signal rules and as your light turns green their must have turned red and they know that they need to stop on the Red.

Same way when we start coding we all share a single pattern rule for naming the components of the classes and it has to be followed by every developer as a courtesy of maintaining code readability among the developers.

 

Apart from the basic rules which you need to follow ( you can go through my blog post for Java Rules) , you need to follow something called as camelCasing Rule.

camelCasing Rule

Even though it is called as Rule, it is actually an convention and it can skipped without getting an error. The rule states the following

"When a class, method or a variable is combination of two or more words and if the combination is not found to be adjacent in the dictionary, we write first letter of second word and so on in Upper Case."

Rule is very simple, it says if you want to name your class or method or variable and it happens to be a two or three words combination we will write first letter of second word onward in Upper Case, the rule doesn't state about the first character as the ruling on the first letter is already given by Java Rules, which states a class should always start with an Upper Case letter and a method or variable should start with lower case letter. Hence the rule actually help us to start from second word and so on.

 

How does it help?

If you look at the camel properly you can see that it's hump (it is the inspiration behind the name of the rule) actually help it stand out and identifies it's body. Same happen when we use the camelCasing Rule for our classes and methods.

camelCasing for class

For example I need to name a class ' uniformresoucelocator' and 'databaseconnector'.

As you can see that in it's actual format reading the name of the class becomes a bit difficult. Now let us apply the rules to it. So the very first rule you need to apply on this two classes will be a generic class rule, i.e it's first letter should be written in Upper Case.

 

Applying Class Rule First on both the classes

class Uniformresourcelocator
class Databaseconnector

After this now let's apply the camelCasing Rule. In case of first class Uniform and Resource are two different word in dictionary, hence as per the rule R in resource will be now written in Upper Case and same way Resource and Locator are two different words so applying the same rule, L of locator will written in Upper Case.

In case of second example, Database and Connector are two different words and hence C in Connector will be written in Upper Case. So now the final output after applying Rules and Conventions will look like.

class UniformResourceLocator
class DatabaseConnector

Just by looking at it, you can see how readable it becomes.

camelCasing for methods and variables.

Now I need to name the method 'loaddataforemployee' and 'substring' as per the rule. As both of them are methods, hence they will remain same in their initial format as methods and variable names start with lower case letter

Applying Method Rule First on both the methods

public void loaddataforemployee()
public void substring()

Now once we start applying the camel casing rule on the first method, load and data and not adjacent words hence D in data will be now in Upper Case, same way Data and For are two different words hence F in for will become Upper Case and For and Employee are different, hence E in Employee become Upper Case.

In case of second method sub and string are actually two different words in dictionary but their combination is also found, hence s in string will remain in lower case.

public void loadDataForEmployee()
public void substring()

More of the reason to follow it.

Java library follows it, just pick up any library and see the names of class, methods and variables. Following is the snap of the class summary of java.util package

Java library follows it, just pick up any library and see the names of class, methods and variables. Following is the snap of the class summary of java.util package

Related Posts

Table Of Contents

;