Search

Here's why should start indenting your code better

Updated: Dec 25, 2020



Heads Up : if you've been programming from quite a time, then this article might not be of help to you


Introduction

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 Indentation in this way.


An Example


Seems horrible? Huh? Thinking that who really code like this in real life? If you've been watching at least 40 people coding like this in front of your eyes. And it hurts.


The cure to this horrible dream lies in Indentation. Now lets have look at the well structured intended code :



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.

Hope the importance of Indentation is clear to you after this demonstration.


Here are 5 reasons for you to start indenting your code!

  • It shows levels of nesting : As shown above, indenting your code tell you when and what is nested inside what. Confusing? For example, you've asked to Write a Program that takes the input of all elements of a 2D array. So simplest logic says, use a for/while loop and inside that another for/while loop. So writing the code without proper Indentation helps you in identifying the nesting level.

  • Anyone reading your code can tell whats executing inside of what : Obviously, We'll not use your own code in an isolated chamber for the lifetime. We have to share your code, 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 code is correctly formatted, commented and indented.

  • 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.

  • Easier to read : For the explanation of this point, read the above two examples again.

  • Better programming sense : Writing a nicely indented code clearly shows that you have a clear sense of what you're writing, and you certainly know how to respect programming.


37 views

Recent Posts

See All