RSS2.0

I moved to Debiprasad.net. Now I am blogging there, so please visit Debiprasad.net and keep me reading. love, Debiprasad

Ask questions related to online shopping in India

Where to buy online in India?

wheretobuyonline.in

Why should we indent our code?

Why should we indent our code?

When I was studying in my 1st semester of B.Tech, I was writing a C program. That time I was not aware of indentation, however i have seen this in some other students program. My program had one error, which I was unable to fix. When I asked our lab demonstrator regarding this, first he started indenting my code. I asked, "Why are you doing so? To look code beautiful or this will resolve the bug?". He smiled and replied "Of course this will look beautiful as well as help you to understand and easily debug your code." Then I was coding 20 or 30 lines of code (LOC). Now when I am coding thousands lines of code, I understand why should we indent and proper indention.

To explain this simple but most useful concept, I decided to use HTML as example language, as most of the people know basics of HTML and everybody say that this language is the easiest one.

Take a look at this non-indented code:


In the above piece of code it is difficult to navigate to any part of code, how ever this contains 12 lines. Think, when you need to find end of some tag in 4000 lines of code, how can you find? In some other languages there some control feature as if condition. If you need to find out where the if condition is ending in 4000 lines of codes? Remember before reaching to the end of if loop you can encounter some other if loops started withing this. So without proper indentation it is very difficult to find out what we need.

The above piece of code can be written with proper indentation as this:


Now you can figure out how easy it to navigate though your code. To indent your code you can use tab or some spaces. (It is recommended to use 4 spaces instead of tab. You can configure your text editor to print 4 spaces when you press tab.)
Again one question comes into my mind. Why people hesitate follow proper indentation? The answer is simple: due to laziness. But they unable to realize that, what they are coding today may need to be modified tomorrow. If they think they save some seconds by not using indentation, may need to spend some hours to find out where to modify. So start following proper indentation right now.

Why should we indent our code?SocialTwist Tell-a-Friend TwitThis Reading: Why should we indent our code?Post Link to Twitter

0 comments:

My Shared Items in Google Reader

My Blogroll

Followers

Want to see/show anything here? Contact me :)