Naming in Programming

What I learned for choose a good name in coding 🧑.

Dinuka Kasun Medis
2 min readAug 15, 2020
Image source : https://jegxdwep1p-flywheel.netdna-ssl.com/wp-content/uploads/2019/08/Naming.jpg

First one is you have to choose a name thoughtfully. If you need to add a comment to describe the name, it means you are fail and you used a bad name.

ex:

int i = 5;//starting number of the seriesint startingNumberOfSeries = 5; 

In here first variable i is not a good one, software developer need a extra comment to describe this name.

And also if developer needs to go through the code and understand the meaning of that name then it became a bad name.

And also you have to pick a pronounceable name.

ex:

getYYYY();

So how do you going to read this? “get why why why why” or “get yeeee” or any other funny way 👼. So please do not use this kind of names. As your experience you know this method is use for get the year so why can not name it directly?

getYear();

It is simple and easy to read. So every time make sure to use a readable name.

And do not use any encodings with your names. Do you know the encoding? Hungarian notation is an example for this. In this some kind of notation we used to write a name as follows.

Hungarian notation

Always remember classes and variables are nouns and methods are verbs. But do not use silly words for names 👶.

But Wait!

Some times we can break the rules👨‍✈️.Let’s look at following code.

for(int i; i<10 ; i++)
{
System.out.print(i);
}

In this simple code we only use i to print and it has small scope. We can easily understand what is i and what we do with it. So like this type of scope (means just 2 or 4 lines scope) you can use a small, simple name; It means you can break the rules in a extra small scope.

Public method/class should have a short name and Private method names should write with more description(long names)

Public variable names should write with more description(long names) and private variable (in a short scope) should be a short name.

So those are the few things I learned for naming. You can read Tim Ottinger’s paper (Ottinger’s Rules ) for more details.

--

--

Dinuka Kasun Medis

Hej, jag heter Dinuka Kasun. Välkommen till min profil. Arbeta som mjukvaruingenjör. Och nu för tiden lär jag mig svenska. 🤓🇱🇰 🇸🇪