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 :
- 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 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:
- 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 :
- Anyone reading your code can tell whats executing inside of what :
- It shows scope :
- Easier to read :
- Better programming sense :
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