Naming in programming

Ankora
Ankora
Published: 07.04.2022

Naming in programming is one thing that everyone kind of understands, does it every day many times but doesn't give it too much thought or plan for the long term.

It is similar to playing chess for most people. You improve as you play more games, have strategies, and don't make the same mistakes as before, but you still do not fully understand the game.

There are two reasons for this: naming should be relatively natural to us, and people don't read literature about best practices. After all, we mention the names of things every day; we even give our pets or newborn children names at times, so it should come naturally to us? Wrong.

-- Phil Karlton

 

As Phil says, correct naming is an arduous process that much thought should go into.

Simple examples of insufficient naming are:

int a;

int asdasdasd;

function doThis();

Beginners usually aren't trained yet to think about the importance of a proper name. The first one tells us only that it is a letter in the English alphabet but not much more, the second one doesn't tell us anything, and the third one informs us that something will happen.

But as it goes while learning to program for many people, we pick up information as time goes, through trial and error, and improve bit by bit.

Let's look at some additional examples:

int count;

float money;

function convertMoney(float a, string b);

This would be an example of someone who improved and started naming variables and functions more appropriately. The first one tells us that it counts something, but the problem is we don't know what. It would be ok if it is part of a class and it is immediately known what it represents. For the second, we would guess that it means the amount of money, and for the third, a first guess would be that it converts money between currencies, maybe, but it wouldn't have to be. These names give us a bit more information, but still, we have to think and check to be sure.

Let's look at some better examples:

int appleCount;

float amountOfMoneyUserWallet;

function convertAmountToCurrency(float amount, string currency);

These examples give us much more insight into what is behind the names. For the first one, we know that this represents a count of apples; the second one tells us it is the amount of money in the user's wallet. The third one tells us exactly what it does; it takes an amount and the currency type and converts it to a different currency.

But an essential thing to note is that we shouldn't make the names as long as possible, like naming a variable "amountOfMoneyUserWalletThatWillBeUsedToPayForWater".

A name should be as short as possible as long as it carries a meaning that can be understood quickly. Long names are also completely ok as long as the purpose is easy to understand and the name is not confusing. The understanding does not only come from the variable's name, but it should also come from the environment the variable is in. The function, class, or different structure gives the variable the context while the variable is there to fill the gap.

Most common naming conventions

Naming does not only apply to variables, functions, or class names. Class names are of penultimate importance in CSS for HTML element styling. Some of the approaches are tailwind or bootstrap to remove some of the custom classes that people write, but we end up with very long lines of code. Another method is using naming conventions. An exciting one is the BEM convention which has a straightforward set of rules for writing classes.

Proper naming does not only apply to CSS classes, variables, and functions. There are also entire methodologies for naming and constructing entire software system parts. Domain-driven design is one approach where we model the system according to the business domain. We would name our classes to reflect business needs. For example, such names would be 'Supplier' or 'Customer'.

Final words

The book "Clean code" mentions the 5S principles introduced in Japan. They represent a focus on quality, not production, and they can be applied to software perfectly. The first is Seiri or organization in English, to know where your things are. Naming is one of the elements.

To sum it up, don't skip out on proper naming.

Let's work together

You made it this far! Thank you for reading this article. We hope you found it useful. Please feel free to share it with your peers if you feel like supporting us.

Work with us See Ankora Software Profile
0