views:

1051

answers:

11

What are the different kinds of indent style in use? Are there any objective differences? How do you determine the indent style for a whole development shop?

The indent style that I use is the so-called BSD KNF style or Kernel Normal Form. It's for me the most logical way of formatting my code. For instance:

//I also prefer one tab per indent
if ($foo == 'bar') {
    something();
    elseif ($bar) {
        somethingElse();
    }
}

What style do you use and why do you prefer that particular style?

A: 

I use the Visual Studio default (Looks like Allman Style):

if(bla)
{
    blub();
    foo();
    if(OnlyOneStatement)
    {
        YesIAlwaysUseBrackets(GotHurtTooOftenNotUsingThem, EvenForSingleStatements);
    }            
    bar(yikes);
}

I also always ident by 4 spaces, but that's just because that's how i started many years ago :)

Michael Stum
+1  A: 

Since i use Resharper my default is whatever it has as default. The reason for this is when i do a cleanup resharper moves/indents/cleans up my code. I like its attitude and besides we all use it on the team and what better than to agree on something decided by an application/plugin as cool as resharper - we all have different 'prefers' but this way we never have to argue - we just do as the resharper does (if resharper jumped off a ledge, yes i would jump too :)

Per Hornshøj-Schierbeck
+1  A: 
if ( bla )
   {
   blub();
   foo();
   bar( yikes );
   }

If you look at the BNF of the syntax for statements, you'll find that the braces belong to the compound statement that follows the 'if', not to the 'if' itself.

Kevin Little
Basing something that affects the readability of your code upon the mechanics by which is it parsed is... interesting.
0124816
hmmm... "conceptual clarity", me thinks. Understanding what the tool *is*, not what you want it to be.
Kevin Little
Didn't realize that, but it makes sense... however, I have to agree with @0124816 on this. That said, what you have outlined above is my *2nd* favorite convention; put the open brace on the same line as if and you've got my favorite.
Software Monkey
This is Whitesmith's. It's my favorite style, followed by Allman (same as this, but with the braces lined up with the 'if'.)
Adam Jaskiewicz
A: 

I prefer to use two spaces, and try to avoid TABs if at all possible. Too often have I found myself with some code that uses TABs and an editor or viewer that thinks they are 8 characters - much too much IMO.

If TABs are to be used, they should be used exclusively and not mixed with spaces, to make sure the indent style remains consistent. Nice in theory - in practice, I find that two spaces just work. More than two, and lots of code falls off the edge of a narrow window.

In C, I agree with the poster to make the code reasonably compact:

if ($foo == 'bar') {
  something();
  elseif ($bar) {
    somethingElse();
  }
}

However, in languages like Delphi where a keyword is used instead of a brace (and optional for single statements), I want the keywords to line up on a vertical axis:

if foo = bar then
  something
else
  if bar then
    begin
      somethingelse;
      andanotherthing;
    end;
Allan Mertner
Always use tabs. Any modern text editor worth using will let you set the tab size and if you use tabs then anyone who looks at the code will be able to see it spaced to their preference.
17 of 26
I disagree. What if I have to look at the code in a browser where TABs are 8 characters? Or all I have available is someone else's editor? Or I need to type/cat or diff the code?Agree on a number of spaces, and stick with it, whether it's 2 or 3 or even 4.
Allan Mertner
A: 

Does BSD KNF style really look like that? That has to the most ridiculous way of laying out code ever.

Like Hojou, I go with whatever style the IDE/ beautifier or whatever tool I might have enforces. If I have free reign, I personally prefer the Allman style, but that is purely subjective. In fact the whole question is so subjective, I'm surprised this site's thought police haven't shut it yet.

David Arno
No reason to close it as long as things stay civil :)
17 of 26
The thought policeman aku clearly disagreed with you...
David Arno
A: 

if(bla)
{
    thing;
    other;
} else if(bla)
{
    thing;
    other;
} else
{
    thing;
    other;
}

I indent one tab; it works fine in the editors I use (Eclipse, TextWrangler, Smultron)

Noether
A: 

I think the brace style is very subjective but I do think that no matter what style you choose, make sure that you use tabs and not spaces to indent.

Any modern text editor worth using will let you set the tab size and so if everyone uses tabs then people can easily use personal preference for spacing.

17 of 26
Yeah, but in production UNIX systems, sometimes you're using just plain old more or cat. Having the code "fly off" the page--or wraparound is not the best way to read it.
Axeman
A: 

Linux kernel coding style makes sense.

Example:

if (x == y) {
        ..
} else if (x > y) {
        ...
} else {
        ....
}
cic
A: 

I use whatever style the language designer uses unless there is a good reason to do otherwise. I've never come across a good reason to do otherwise.

Ferruccio
+1  A: 

Personally, I have always advocated the "banner" indent style (search wikipedia for "indent style"):

method() {
    some code
    if(condition) {
        some code
        }
    else if(another condition) {
        some code
        some more code
        even more code
        }
    else {
        some code
        some more code
        even more code
        }
    }

It's always been my feeling that the banner style does the best job of visually reflecting the logical code blocks. And it's follows rigorously for languages like HTML which have closing keywords for blocks:

<table>
    <tr>
        <td> xxx </td>
        <td> xxx </td>
        <td> xxx </td>
        </tr>
    <tr>
        <td> xxx </td>
        <td> xxx </td>
        <td> xxx </td>
        </tr>
    <tr>
        <td> xxx </td>
        <td> xxx </td>
        <td> xxx </td>
        </tr>
    </table>

EDIT: I have recently posted a more detailed article on indentation on my website.

Software Monkey
A: 

A second vote for "banner". My argument is that any continuation of a line ought to be indented. For example:

   func_call(looooooooooong_argument,
             second_argument, ...

A "block" is just a continuation of a statement. So all of the block should be indented.

   if (expression) {
           command;
           command;
           }

The commands and the closing bracket are part of the "if" command. The next non-indented line is the next command.