Coding Best Practices

Coding Practices: Essential skill for developers

Writing code and writing code efficiently are two different things. A good approach to coding has been always a subject of importance among the software developers community.

Code for some specific task may vary from person to person, however not all the approaches are good even if they all tend to work the same. Writing code efficiently is an art that may comprise various things that we are going to see in this article.

Comments and Documentation :

One of the most important things that should you should learn as a developer is to write comments.

Comments are pieces of text added to the code as an explanation of the code. It seems to be a waste of time and space but it’s not.

Comments help other developers to understand your code greatly. They help you as well to understand what you did if you are going to see yours after a long period.

Many developers do not comment on their code which is a very bad practice. A good piece of code should be well documented. Not only comment has explanatory information but it can also have document-related information like author, important dates, and copyright details.

Below given a python code with some comments –

x = 1 
end = 10 
num = 7 
#This will print the multiplication table of 7 from 1 to 10 
while x < end: 
    res = 7 * x 
    print(num, "*", x, "=", res) 
    x+=1

Indentation :

Indentation is the number of spaces put at the start of a line of code. In many programming languages, indentation is used just to increase the readability but in some languages, indentation is mandatory. For example in python, indentation is a mandatory thing as blocks are created by indentation rather than by using braces (unlike other programming languages). In the above example, we have created our loop block by giving the required indentation.

The below examples show how better readability can be achieved by using indentation –

Example 1- (Without indentation)

#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter a number";
cin>>num;
if(num % 2 == 0)
{
cout<<"The number is even";
}
else
{
cout<<"The number is even";
}
return 0;
}

Example 2- (With proper indentation)

#include<iostream>
using namespace std;
int main()
{
    int num;
    cout<<"Enter a number";
    cin>>num;
    if(num % 2 == 0)
    {
        cout<<"The number is even";
    }
    else
    {
        cout<<"The number is even";
    }
    return 0;
}

You can see the second example has better readability than the first one.

Naming Convention :

Using a proper naming convention plays a vital role in programming. A developer should use variable names that relate to the scene rather than using alphabets like X, and Y. For example, if you are making a program that would take the age entered by a user to check whether the user is a teenager or not, you can use a variable named userAge rather than using something like ‘a’.

Take a note of how I used the variable usage (every letter is lowercase except A). This style of naming is called as camel case. In the camel case, the first letter is lower case and the first letter of every new word should be uppercase.

You can also use underscore to separate different words in your variables like this –

user_age

However, if we need to use temporary variables we can use even a single character.

For example –

<?php
$myArr = array("apple", "mango", "banana", "orange");
for($i = 0; $i <= 3; $i++)
{
        echo $myArr[$i];
}
?>

DRY (Don’t Repeat Yourself):

It means that the same piece of code or the code meant for a similar functionality should not be repeated again and again.

For example in a web application, the header and footer are the sections that are the same for every single web page. So. it’s a bad idea to just copy-paste the content of these to every single page. Instead of what we can do, we can make separate sections for the header and footer and include them wherever we like.

<?php
        include_once("header.php");
?>
//body content
<?php
        include_once("footer.php");
?>

Functions are another thing that is used to prevent this. For example, if we need to calculate the average of two variables multiple times in our application. Then instead of writing the code every single time, we need to calculate, we can make a function for this and call it wherever we need it.

Limit line length :

Long lines of code are a bit harder to read. It is a good practice to avoid writing long lines of code horizontally.

For example, an SQL query can be lengthy if we have used multiple joins and multiple clauses. The query instead to be written in one single line should be broken down into multiple lines.

<?php
$Query = "select student.admission_number, student.full_name as student_name, parents.full_name as father_name from student inner join parents on student.father = parents.id where student.id = $id && parents.parent_type = 1";
?>

Organizing files and folders :

You can make up a single module of your application in a single file however that would be too messy and hard to read and debug. What you can do instead, you can break up your module into various files. For example, you can make a separate file for database connection and a separate file for keeping all your functions. There should be separate folders to contain the CSS files, JavaScript files, images, uploaded files, etc.

Capitalization of SQL :

A web application commonly requires database integration. It is a good practice to write readable raw SQL queries. Even though SQL special functions and words are case insensitive, it is a good practice to make them uppercase to distinguish them from your column and table names.

<?php
$query = "select * from employees where id = 1";
?>

The above query should be like this –

<?php
$query = "SELECT * FROM employees WHERE id = 1";
?>

Code Refactoring :

Code refactoring is a process to restructure the existing code without changing its existing functionality. Code refactoring does not mean debugging or adding any new functionality. It simply means to clean up or cut down the code to make it more readable and usable.