In our previous post, we dwelled on the significance of clean coding. Now we can talk about clean coding practices.

The first and the most important clean coding practice is the naming convention. We constantly name methods, functions, classes, and parameters. We realize that it is one of the most important parts of coding when we cannot come up with a beautiful name. Sometimes, a name gives us an idea about what the code does and which information it bears, while at other times we need to follow the code or debug it to understand what the code does.

 

 

For instance, the above code includes a simple binary search algorithm. As a reminder, binary search is an optimized algorithm used to make searches in arrays contain sorted numerical elements. Rather than going through the array, it constantly divides it into two parts and compares the middle term with the searched number.

We can make this code more understandable with just a few simple naming steps. For instance, “the key”, the name of the parameter undertaken by the method, might mean many different things. A more specific name, such as numberToSearch might work better. Similarly, the logic of research might be better understood by choosing lowIndex instead of lo or highIndex instead of hi as a name.

Naming is even more significant in more comprehensive and complex codes. When you come up with a method called Calculate(), you might ask yourself what it calculates. However if you name it as calculateTax() you might have a better idea on what the method does. On the other hand, the name shouldn’t be too long, as if it tells a story. For instance, checkUserClickedBottonToSeeIfFormFilledCorrectlyAndCheckDatabaseToPersist() wouldn’t be a suitable name. We need to be balanced in trying to express what the method does.

Another point about naming is the use of a prefix() or a postfix() to help us understand the type/feature of the codes. This forms a common language and a standard among developers. For instance, variables which start with “x_” in C++ are generally constant variables, whereas in Java upper case is used to indicate constant variables. Class names always start with an upper case. In some companies, IT teams document these rules and developers endeavor to abide by them.