Search code examples
coding-styleindentation

Is there any basic rules of indenting any code


This may seem a very trivial question but I would really like to have an answer:

I always seem to indent my code in the following fashion:

if (<condition>)
{
     <some code>
}

That is, I place my braces in a fashion that makes the code appear within a block. However, as I come across other programmers and even professional code on major websites and my workplace, I observe this:

if (<condition>) {
     <some code>
}

Is there any significant difference in the two styles of indentation or is it just a matter of choice? This is my personal opinion that the latter format seems a little messier. Can anyone shed some light on this?


Solution

  • The two schemes you enumerate are Stroustrup (the creator of C++) and K&R (from the C book)

    Refer to http://en.wikipedia.org/wiki/Indent_style

    Which one is better is down to personal opinion (in my opinion); and that's outside the scope of this forum.