Results 1 to 2 of 2

Thread: Why you should use Indentation? Share/Save - My123World.Com!

  1. #1
    Garage Member
    Join Date
    Aug 2012
    Location
    India
    Posts
    97
    Blog Entries
    1

    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 cumpolsury 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 :
    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;
    }
    }
    Seems horrible? Huh? Thinking that who really code like this in real life? Trust me, I've been watching at least 40 persons 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 his indented brother :

    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;
    	}
    }
    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 benefitted. Comments, suggestions and rectifications are invited. Your comments motivate me to work better, and better.

    Hope this helped you,
    Cheers.
    Last edited by c0dist; 09-05-2012 at 07:38 AM. Reason: proof reading
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who
    keeps learning stays young. The greatest thing in life is to keep your mind young.
    - Henry Ford

  2. #2
    each new quote from a person is like a new paragraph, so indent each one when the conversation switches from one person to the other.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •