Importance of selecting relevant names and proper naming conventions in programming.


improve-logic-in-programming




Names are everywhere, We name our variables, functions, arguments, classes, and packages. We name our source files and the directories that contain them. We name our jar files and war files and ear files.

Generally, developers do two mistakes while writing names in programming. Most developers do not select relevant names and, another mistake is not selecting proper naming conventions. There are so many reasons behind that but, writing irrelevant with no proper naming convention names in programming is not a good practice. But, if you follow the best practice it will so much help you and other developers to read and maintain code.


Why it is important?

There are lots of benefits of writing relevant names with proper naming conventions in programming.

    1. Readability of code made easy.
    2. Helps to understand the intention of code.
    3. Reduce confusion.
    4. Increase productivity of developers.
    5. Helps to save lots of time for developers.
    6. Helps to maintain code for a long duration.

On the other hand, there are also lots of disadvantages of writing irrelevant names with no proper naming conventions.

    1. The readability of code makes it hard.
    2. The intention of code is not understood easily.
    3. Increases lots of confusion.
    4. Reduce developer's productivity and increase frustration.
    5. Not help to time management, consume lots of time to the understanding of code.
    6. It makes it very hard to maintain code in long term.

First, we understand different types of naming conventions in a programming language and then see the selection of relevant names.


Naming conventions

A variable name with more than one word can be difficult to read. But, there are several ways by using them we can make more readable.


1. Camel case naming convention

In camel case naming convention each word starts with a capital letter but except the first word.

dayTemprature

Here you can see, the day word is in small letters and the temperate word first character T is a capital letter, and the rest is in the small letters.


2. Pascal case naming convention

In the pascal case naming convention, each word starts with a capital letter.

DayTemprature

Here day and temperature both words start with capital letters.


3. Snake case naming convention

In snake case naming convention each word is separated by an underscore.

day_temprature

Here you can see day and temperature words are separated by an underscore character.


Relevant names

1. Use intention revealing names

Choosing good names takes time but saves more than it takes. So take care with your names and change them when you find better ones. Everyone who reads your code (including you) will be happier if you do. If a name requires a comment, then the name does not reveal its intent.

int d;  // number of days in a month

Here name d reveals nothing. You can write variable names like this.

int daysInMonth (using camel case)
int days_in_month (using snake case)

The benefit of choosing names that reveal intent can make it much easier to understand and change code.


2. Avoid disinformation

Must avoid leaving false clues that obscure the meaning of code. We should avoid words whose entrenched meanings vary from our intended meaning. "hp", "aix", and "sco" would be poor variable names because they are the names of Unix plat-forms or variants.

Do not refer to a grouping of users as an "userList" unless it's actually a List. The word list means something specific to programmers. If the container holding the users is not actually a List, it may lead to false conclusions. So "userGroup" or "bunchOfUsers" or just plain users would be better.

Beware of using names that vary in small ways.

numberOfDataObjectsHandlingInStorage
numberOfDataObjectsPresentInStorage

See the above example both names look similar if you get possible to avoid writing these types of names then avoid them.

A truly awful example of disinformative names would be the use of lower-case L or uppercase O as variable names, especially in combination. The problem, of course, is that they look almost entirely like the constants one and zero, respectively.

int a = l;
if (O == l) {
a = O1;
} else {
l = 01;
}

3. Make meaningful distinctions

Programmers create problems for themselves. For example, because you can't use the same name to refer to two different things in the same scope, you might be tempted to change one name in an arbitrary way. Sometimes this is done by misspelling.

Programmer use "klass" just because the name "class" was used for something else. The programmer also uses (a1, a2, a3…aN), these names are disinformative instead you choose meaningful intentional names like, "source", "destination" etc.

Imagine that you have Patient class. If you have another called PatientInfo or PatientData, you have made the names different without making them mean anything different. Info and Data are indistinct noise words like "a", "an", and "the". Noise words are redundant. The word variable should never appear in a variable name. The word table should never appear in a table name.


4. Use pronounceable names

Most developers do common mistakes while giving names in programming, they use shortcut names that are hard to pronounce. Example, arr (array), obj (object), stud (student), dr (doctor), gen (generation), et (epochtime) etc.

Use pronounceable names while developing if don't sometimes it confuses you and other developers. "genymdhms" (generation date, year, month day, hour, minute, and second) Instead, you can choose "generationTimestamp" as a name.


5. Use searchable names

Single-letter names (e = 200) and numeric constants (3) have a particular problem in that they are not easy to locate across a body of text. One might easily grep for MAX_PATIENT_IN_OPD, but the number 3 could be more troublesome.

The name e is a poor choice for any variable for which a programmer might need to search. It is the most common letter in the English language and is likely to show up in every passage of text in every program.