Clean Coding

Names are basically everywhere, one of the most complex job in the world is to name a child, don't believe me (ask your parents). When it comes to the programming language a developer also goes through a same dilemma every time they need to name a class or a method or a function or a variable. While writing a small operation like even odd or prime number we don't care much about the names , because the whole application scope is basically in front of your eyes and it is easy to determine what's what, but imagine the same scenario when we do for a very large and complex operation where the code is running from one file to lot of files. In such scenarios naming a variable as var1 and var2 or x and y will confuse you more rather than making your life easier.

 

So in this article we will try to understand the basics of naming the components as per the Clean Coding Rules.

1. Use Intention Revealing Name

- If followed properly either you will be proud of yourself or the person to whom your code was handed over to will be praying for your well being, using the names with the proper intention will make your code more readable and there will be no need for making any type of mental understanding. For example

float t; // t indicates the temperature

Here the variable t can mean a lot of things, instead I can use the same variable in the following way.

float temperature;

After reading the variable it makes it easier to understand the same , now to make it more proper let us try something like this

float temperatureInCelsius;
float temperatureInFahreniet;

So in this way you can see how it can make your code more readable. For instance let see the following method

public void loadData(List l1)
        {
            List l2=new ArrayList<>();

            for(Employee e:l1)
            {
                if(e.getSalary()>=50000)
                {
                    l2.add(e);
                }
            }
        }

If you look at the code you will get that this method is basically just looping through the data from incoming list and filtering out those whose salary is greater than or equal to 50,000. But I have certain questions

With clean coding this can be resolved like this

public void loadData(List employeeList)
        {
            final int GRADE_TWO_SALARY=50000;

            List filteredEmployeeList=new ArrayList<>();

            for(Employee e:employeeList)
            {
                if(e.getSalary()>=GRADE+TWO_SALARY)
                {
                    fliteredEmployeeList.add(e);
                }
            }
        }

In simple way just by changing the names, it makes our application more readable.

 

2. Avoid Disinformation

- When a developer reads a code they make a judgement based upon the name of the variable. So having the variable name which doesn't match up the information or leads you to some place else will fall under this category. For example

String[] nameList;

For a developer a variable name ending with list is actually an object of class List and not an array, so to avoid any disinformation , you can name the above variable as

String[] nameGroup;
String[] nameArray;

Beware of using names which vary in small ways.

String urlForCallingProductData;
String urlForCallingAllProductsData;

Both the names are more or less same and there is a high chance that you might get confused, so beware of the names which vary in small ways, we can rather make changes in the above code to accommodate one of the variable name to look a bit different , yet meaningful.

3. Make Meaningful Distinction

When a developer writes a name just to make a compiler happy, they will end up writing loads of unmeaning name and it will nothing but create complexity in the understanding of the code.

For example you are trying to load the data from one file to another and write a method like this

public void transferData(String s1, String s2)
        {

        /* Code Block for the operations

        }

Now what exactly is the meaning of s1 and s2. Rather than naming them s1 and s2 better option would have been.

public void transferData(String from, String to)
        {


        /* Code Block for the operations


        }

Using from and to actually makes the sense here, or even making use of source and destination would have been also worked.

Bottom line is use MEANINGFUL DISTINCTIONS!!

4. Use Pronounceable Names

Elon Musk and his partner Claire Grimes’ creative name for their newborn son ‘X Æ A-12’ left netizens scratching their heads. It is for sure that making use of the name which a human cannot pronounce is confusing and at the end of the day we developers are the humans and when we come across the names for the methods or the variables which we cannot pronounce properly we tend to get confused.

List datStudMedList=new ArrayList<>();
List datParMedList = new ArrayList<>();

It becomes a tedious task to pronounce the names above, instead if we just use the following

List medicalStudentsDataList=new ArrayList<>();
List medicalParentsDataList = new ArrayList<>();

The moment we are able to pronounce it, it starts to make meaning for us.

5. Use Searchable Names.

Making use of a single character variable name like e or x is a very common mistake a developer make and it makes it tough for searching it.

It is always better to make use of named constants instead of using single character variable names or numbered variables like a1, a2 and a3.

When searching for the a variable it is always better to have a named variables.

int maxLoadCapacity;
float holdingViscosity;

6. Don't make use of prefix (either of type or domain)

To make this rule implemented properly it is required that the class is not very vast and it contains only the meaningful and required set of variables and methods inside it. For example when you are developing a project for Code With Z , you shoudn't prefix the code based on domain or types, for example

int numData;

String cwzName;
String cwzAddress;

Rather you can go and write the names directly as it is human tendency to ignore the prefix and focus on the meaningful part of the data;

int data;

String name;
String address;

 

So these are some basic rules you need to work when you name any of the components in the program.

Related Posts

Table Of Contents

;