Java Coding Nerd. This Blog will feature content-rich articles, tutorials and own opinions on variuos programming related topcis.

Clean Code

Clean Code with Java: Choose meaningful Names



While coding we name quite a few things. In fact a lot of them: Classes, Methods, Variables, Packages and so on. To have Clean Code you have to give those things speaking words.
But how do you know you have choosen good names for you vaiables etc? Imagine you  take your noch techie friend and sit him right in front of your code. When he is able to know what your code is doing,  you did a good job with choosing the right names. Remember he should not know how you are doing it, just what.

Clean Code: Guidline for choosing the right names

We will learn what it means to choose the right names for your code.

Use Intention revealing Names

Have a look at this little code snippet

int i;

Often developers wright code in this style because they know what they want to do with the variable. But think about what happens when another dev uses your class. Right it will be absolute horror for them. Or think about you again. Right now you know what to do with the variable, but what if you have to touch you code in  a year again?

Try to always write code like this

int numberOfPickelsInTheJar;
long timeElapsedInMilliseconds;
String NameOfTheAmericanPresident;

Avoid Disinformation

Let us start with an example of disinformation

int[] accountList;
String nameOfTheLastBarVisited;
String nameOfTheLastClubVisited;

Look at accountList it is clearly an array, but the name could lead to confusion because it could make others thing this is an List. So you better should call it something like accounts.  The two Strings on the other hand look just to similiar, it is easy to mix them up after a long day. So eventhough the name itsel is valid, in this context it is not. Try giving it names that make it more distinguishable like:
ClubNameLastVisited and BarNameLastVisited.

int l = 5;

int a = l;

System.out.println("Will it print");


HORRIBLE! Can you spot the potential problem? One could easily misread int a and think that is always 1 but indeed it is 5. Never ever name a variable just l or O!

Class Names

The name of a class should never be verb. Also avoid words like “Info” “Data” “Manager”. The name should have noun or noun phrase like Animal, StartPage, Person.

Method Names

Method names should have a verb like “deleteAccounts, saveSettings.  It is also important that you follow the javabean standart:

  • Accessing Methods : getXXXX
  • Mutating Methods : setXXX
  • Predicate Methids : isXXXX

Pick One Word per Concept

If you mean the same thing never use different wording.

String nameOfMyAnimal;
//Do some work
String nameOfMyPet;

This just adds complexity and confusion to your code. Why should you have two different names for the Same thing? Don´t do this.

If want a deeper look in this  topic i reccomend you the Book “Clean Code” from  Robert C. Martin

Spread the love

Leave a Reply

Theme by Anders Norén