Thursday 28 March 2013

Why you should use Indentation?

Introduction:
If you've been programming from quite a time, then this article might not be of help to you, but if you've just started or planning to start then read on. I'll be discussing one of the issue that I faced few months ago when I started programming, and what I see in almost all newbies. Yes, as the title suggests, it's Indentation. A rather non-technical and not compulsory but an important and useful thing.

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 indentation in this way.
Indentation - Wikipedia, the free encyclopedia

Example:

So we can see that Indentation is nothing but formatting the source of our program (by the help of tab/ spaces) in such a way that it increases readablilty. Lets look at a pseudo-code :


  1. main() {
  2. variable declaration;
  3. for loop {
  4. another for loop {
  5. yet another for loop {
  6. some work to be done;
  7. another work;
  8. }
  9. again some work;
  10. }
  11. damn some more work;
  12. }
  13. }



Seems horrible? Huh? Thinking that who really code like this in real life? Trust me, I've been watching hell lot of people coding like this front of my eyes. And it hurts.

Warning: If you code like this, then I've some bad news for you.. :P (If you know what I mean :P)


Cure to this horrible dream lies in Indentation.
Now lets have look at indented brother of above code:



  1. main() {
  2.         variable declaration;
  3.         for loop {
  4.                 another for loop {
  5.                         yet another for loop {
  6.                                 some work to be done;
  7.                                 another work;
  8.                         }
  9.                 again some work;
  10.                 }
  11.         damn some more work;
  12.         }
  13. }



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/ mis-managed brother.

Hope I've demonstrated you the importance of Indentation.

Few good reasons to use indentation :
  • It shows levels of nesting, nested if statements , for loops, etc :
As shown above, indenting you code tell you when and what is nested inside what. Confusing? For example, you've asked to WAP that takes 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.

  • 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 life time, don't say you will. You'll obviously share you 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 flow of execution of 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 :
I don't think I need to explain this point. If you still want explanation, read the above two examples again.

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


Points Source : Why is indentation important in c progaming . I've elaborated them by myself.

Another thing to note is that as stated above, some languages like Python mostly rely on the indentation to interpret your code correctly.


Tabs or Spaces ?

It has been a matter of unsolved debate from years that whether a programmer should use tab key or whitespaces to give indentation. Tabs are generally denied because tab length of different systems may be different. As one can set tab key as 2/4/8 spaces. So if a a programmer used 4 spaces tab, and reads it on a system with 8 spaces tab, it might disturb the formatting (though I use tab only :P).
However, whitespaces are always interpreted same on every system. But typing whitespaces everytime can be a hectic work if you've more than 2 levels of nesting.


What Indent-style should I use ?

Choosing indentation style is purely a matter of personal choice. However, some programming languages have developed or are generally coded in similar indent every time, it is not a compulsion to use a particular indentation style. Generally, indentation style varies from person to person. Some common/ famous indentation styles are :
1 K&R style
2 Allman style
3 BSD KNF style
4 Whitesmiths style
5 GNU style
6 Horstmann style
7 Pico style
8 Banner style
9 Lisp style
10 Ratliff style
Source : Wikipedia


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. This in turn makes it easy to understand the flow of program and scope of block, easy to debug and easy to modify.

I've tried my best to explain the importance of indentation to you. So I hope, those of you who've just started programming, like me, will be benefited  Comments, suggestions and rectifications are invited. Your comments motivate me to work better, and better.

Cheers.

No comments:

Post a Comment