Sitemap
Nirali Sahoo

Nirali Sahoo

A philomath being a maven ;)

Neat Code (Proper Indentation)

3 min readMar 25, 2024

--

Introduction

Indentation, in its definition, “is an empty space at the beginning of a line to signal the start of a new paragraph”. Now, why is this so important that I decided to make a blog post on this? That’s because, no matter how silly it sounds, it’s one of the most important concepts of programming that almost everyone screws up with.

Get Nirali Sahoo’s stories in your inbox

Join Medium for free to get updates from this writer.

Note that I mentioned proper indentation and not simply indentation. Proper indentation, for me, means a code with proper spacing, allignment and certain rules.

Importance:

  • Super helpful for debugging : Maintaining proper space can help to find errors easily.
  • Increased Readability : Other programmers, or even the future you gets to understand the code more properly.

These aren’t all but well, I guess that’s enough for the boring theory for now.

Note:

There is no absolute law for how indentation works (unless, of course, you work with Python). All the things written here are my own personal views.

Basic Rules

  1. Proper spacing : A cramped up code is really confusing and decreases its readability. Though it may be sacrificed in certain cases like in microprocessors it is still essential in most of the situations or programs.
  2. Nesting and allignment : The nesting should be done in a way which makes it easy to find the beginning and the ending of loops or condition statements.
  3. Stick to your indentation style : Now, this is the most important one. Changing your style repeatedly in a single program not only makes it look haphazard but also makes it confusing and can even destroy the interest of the reader.

Congo! We are done with the boring theory part. So, the next section describes a small example covering all the rules above.

Example

Note:

Now, writing examples in all the laguages is next to impossible so I will be using C but don’t worry, you don’t need to be a C programmer to get the basic idea.

Without proper indentation:

#include <stdio>
#include <stdlib>

int main() {
char random_char; int random_num1;
int random_num2;
scanf("%c%d%d", &random_char,&random_num1,&random_num2);

//Note the change in placement style of the braces.
if(random_num1>0)
{
if(random_num2>0) {
printf("%c", random_char);
}
}
return EXIT_SUCCESS;
}

With proper indentation:

#include <stdio>
#include <stdlib>

int main() {
/*Preferred: Variables of one data type in one line
(or even better, each variable on a different line but some people don't
prefer that).
*/
//Random tip: Initialising each variable helps in finding bugs easily.
char random_char = '\0';
int random_num1 = 0, random_num2 = 0;

//Preferred: Proper spacing between two different operations (here, defining variables and taking input).
//Also, note the spaces between the different variables.
scanf("%c %d %d", &random_char, &random_num1, &random_num2);

//Note the proper spacing between the nested conditions which makes finding the opening and closing braces easier.
//Also note the spaces between the operators and the operands.
if(random_num1 > 0) {
if(random_num2 > 0) {
printf("%c", random_char);
}
}

return EXIT_SUCCESS;
}

So, we are finally done with the thory of proper indentation! Again, all of these were my own personal views. The final choice of how to indent your code lies on you. So I hope this article was of use to you.

Originally published at http://nizzblogs.wordpress.com on March 25, 2024.

--

--

Nirali Sahoo

Written by Nirali Sahoo

A philomath being a maven ;)