Introduction
Most of us tend to stop writing code the moment it starts working. But if you want to collaborate and share code with people or just plan to reference your code 3 months later, then writing readable code is super important. Readable code helps you save a lot of time and understand the codebase a lot quicker.
In this article, we will discuss some of the best practices in the C language that you should follow to write super-efficient code. They include:
Meaningful Names
When you create a variable, you need to give it a meaningful name. When you are writing code, it's easy to think that you will always remember the purpose of the variable you are creating using a single-letter name. But that's not the case!
Suppose, you have created a variable that stores your height in inches. Now, you can name it in two ways:
The first option is a short name, such as "h". At that moment, you will remember what the variable is doing. However, three months later, it will be very hard for you to remember what the variable actually stores. So, that's not the ideal case.
The second option is a descriptive name, such as "My_Height". In this case, even six months later, you will remember the purpose of the variable just by seeing its name.
Hence, to improve code readability, writing meaningful names is a must.
Meaningful Comments
We all write comments in our code. However, we sometimes write comments that are too obvious. Take a look at the following example:
// Add two numbers 5 & 3
int result = 5 + 3
The above comment is too obvious. It can be seen that two numbers are being added. There's no need for a comment here.
Now, look at the below example:
// Calculate the surface area of a sphere (4πr^2) with a radius of 5
int result = 4 * 3.14 * (5 * 5)
The comment written in the above example is useful in this case, as it is difficult to understand what's happening by looking at the equation. Hence, the comment here is clarifying the functionality of the equation.
That's why writing meaningful comments is one of the most important best practices in C programming.
Consistent Indentation
We all indent our code. But is it consistent? Look at the following example:
for (int i = 1; i <= 5; i++){
for (int j = 1; j <= 5; j++)
{
printf("%d, %d", i, j);
}
}
In the above example, we have used indentation, but it's not consistent at all. Hence, we are having a little difficulty reading the code.
Fun Fact: The larger the codebase gets, the more difficulty you will have in understanding the code.
Now, let's take a look at the optimized version of the above example:
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
printf("%d, %d\n", i, j);
}
}
Looks so much better, right? It's easily readable as well.
So try to make your indentation as consistent as possible.
Avoid Magic Numbers
Another best practice that you should follow is to use variables instead of magic numbers, i.e., hardcoded values in your code. If we do something like this:
while(height < 6)
{
// Do something
}
It's hard to detect what the above number signifies.
Now, look at the following example:
int minimum_height = 6;
while(height < minimum_height)
{
// Do something
}
Here, we can easily understand that someone's height is being compared with the minimum height required.
That's why using variables instead of hardcoded values is important.
Capitalize Macro Names
It's a best practice to capitalize all the macro names that are used in your C program. It helps you separate them from regular variables.
So, instead of writing this:
#define pi 3.14
You should go for:
#define PI 3.14
Conclusion
That's it! We have seen above some super important best practices to follow to write good code. Use these methods and make your code much more readable!
Top comments (0)