DEV Community

Cover image for Why Indentation is more important than coding?
Sanjay Singh Rajpoot
Sanjay Singh Rajpoot

Posted on • Edited on

Why Indentation is more important than coding?

As a beginner, I always thought that working of code is more important than how well it's indented. After write code for about a year now I realised how important it is to write good code. Good code here not only means indentation but how your code looks to other people. This will include how you name a variable, where you add comments and more. Let's begin with Indentation.

What is Indentation?

In computer programming languages, indentation is used to format program source code to improve readability. Indentation is generally only of use to programmers; compilers and interpreters rarely care how much whitespace is present in between programming statements. However, certain programming languages rely on the use of indentation to demarcate programming structure, often using a variation of the off-side rule. The Haskell, Occam, and Python programming languages rely on an indentation in this way.

Let's look at a pseudo-code :

main() {
variable declaration;
for loop {
another for loop {
yet another for loop {
some work to be done;
another work;
}
again some work;
}
damn some more work;
}
}
Enter fullscreen mode Exit fullscreen mode

Seems horrible? Huh? Thinking that who really code like this in real life? And it hurts. The more you see the more it hurts. Now let's have look at his indented brother :

main() {
    variable declaration;
    for loop {
        another for loop {
            yet another for loop {
                some work to be done;
                another work;
            }
        again some work;
        }
    damn some more work;
    }
}
Enter fullscreen mode Exit fullscreen mode

When we look at this code, we can clearly see that inside our main(), we have a nested for loop. It certainly is more readable, and understandable than his illiterate/ mismanaged brother. So now you got what I am talking about.

How to use good indentation in your code?

1. Spacing

Spacing forms the most important part of code indentation and formatting. It also makes the code more readable if properly maintained. So we should follow proper spacing throughout our coding and it should be consistent.

Few tips :

All array names should be immediately followed by a left square bracket.

// NOT Recommended
    arr [0];  

// Recommended
    arr[0];
Enter fullscreen mode Exit fullscreen mode

All binary operators should maintain space on either side of the operator.

// NOT Recommended
    a=b-c;          
    a = b-c;        
    a=b - c;        

// Recommended
    a = b - c;      // NOT Recommended
    z = 6*x + 9*y;         

// Recommended
    z = 6 * x + 9 * y;     
    z = (7 * x) + (9 * y);
Enter fullscreen mode Exit fullscreen mode

All casts must be written without any space

// NOT Recommended
    (ClassA) m.get(3);  
    ( ClassA )m.get(3); 

// Recommended
    (ClassA)m.get(3);
Enter fullscreen mode Exit fullscreen mode

2. Maximum Line Length

Try to fix a maximum line length for your code. The maximum line length should not exceed 120 characters. If it needs to be increased then put it in a different line. The reason behind it is due to the capability of editors and printing facilities. In general, normal editors and printers can handle 120 characters comfortably. So if it exceeds the limit then it’s become a problem to handle for the code editor itself. Try to get the lines of code to a near equal length. This will make the code look symmetric and organised.

3. Self Documenting Code:

Many a times we have heard that each and every piece of code should be documented properly so that it is easily understandable. And we start writing details of the method on top of it. But this is not the best practice. Because the code might be changed in future and the programmer forgot to change the documentation. So the best practice is to write the code in a way so that it can explain itself without any comment.

// NOT recommended
    if ( (a == Good) && ( (b == better) || (b == best) ) )// Recommended
    boolean isbetterbest = ( (b == better) || (b == best) );
    if ( (a == Good) && isbetterbest )
Enter fullscreen mode Exit fullscreen mode

4. Using Comments

Try to explain your logic in simple words for other people to read it comfortably. With the help of comments, you will have fewer queries regarding your code and logic. One-line comments added in the above snippets are the best examples to convey to the reader what your code is about.

Some rock-solid reasons to use indentation.

  • It shows levels of nesting, nested if statements, for loops, etc :

As shown above, indenting your code tell you when and what is nested inside what. Confusing? For example, you’ve asked WAP that takes the input of all elements of a 2D array. So simplest logic says, you’ll use a for/ while loop and inside that another for/while loop. So writing the code w/ proper indentation helps you in identifying the nesting level.

  • It shows scope :

It’s clear that if everything is indented you know the scope of variables. As in the above example, if the code’s indented, you can easily understand which variables are available where. And it also shows that where a specific block is starting and where it’s ending.

  • Anyone reading your code can tell whats executing inside of what :

Obviously, you’ll not use your own code in an isolated chamber for the lifetime, don’t say you will. You’ll obviously share your code (until you’re Windows/ Apple fanboy), and others will read it and may want to modify it. And in order to be able to modify, one must be able to understand the flow of execution of the program, which is possible only if the code is correctly formatted, commented and indented.

  • Easier to read :

I don’t think I need to explain this point. If you still want an explanation, read the above two examples again.

Conclusion

From the above points, it is very clear that while writing even a 20 lines program, indentation saves a lot of time that one might waste in reading and understanding again. It also increases readability to others and well as the programmer. We have also understood the importance of writing good code with the best indentation and formatting practices. We need to remember that a proper indentation rule must be followed while coding so that the program is easily readable and maintainable in future. So we can conclude that indentation and formatting is an important part of programming practice and developers should follow It from the starting of their programming career.

Nameste 🙏

Top comments (6)

Collapse
 
cjsmocjsmo profile image
Charlie J Smotherman

Write some python code, you will understand the importance of proper indentation very quickly.

Collapse
 
andreidascalu profile image
Andrei Dascalu

Or Go, or yaml. :)

Collapse
 
sanjaysinghrajpoot profile image
Sanjay Singh Rajpoot

100% true

Collapse
 
babalolajnr profile image
Abdulqudduus Babalola

This is a nice article and all but if you use a decent IDE or editor, it will handle indentation well for you.
I personally do not indent my code myself in VsCode, it does that automatically for me so that I can focus on the actual code.

Collapse
 
alanparadis profile image
Alan Paradis

Sometimes even if the code is not enough commented a good indentation save the readability of the code, I give it as much importance as commenting

Collapse
 
cherrydt profile image
David Trapp

Ironically, the "again some work" and "damn some more work" isn't indented correctly (needs to go one layer deeper).