Source Code Indent Styles

 

Professional programmers really take care of readabilty and appearance of their source code. Source code Indentation is the thing which we’ve been following from high school programming classes to distinguish the blocks in your source code. If you read programs written by different people you can see the way they used braces and Intended their source code

Kernighan & Ritchie’s (K&R) style and Eric Allman’s Allman style are the most popular among programmers. I was accidently came to this topic when I was reading Object Oriented Design & Patterns by Cay Horstmann

K&R Style

[sourcecode language='cpp']
int main(int argc, char *argv[])
{

while (x == y) {
something();
somethingelse();
if (some_error)
do_correct();
else
continue_as_usual();
}
finalthing();

}
[/sourcecode]

Allman’s Style

[sourcecode language='cpp']
while(x == y)
{
something();
somethingelse();
}
finalthing();
[/sourcecode]


You can see lot more indent styles from Wikipedia. I’m following Allmans’s style as I found it’s more readble. We can easily identify start and end of blocks easily.
Update: Updated post title – @ 3:53 PM IST

 
This entry was posted in Uncategorized. Bookmark the permalink.