Most Used Coding Styles (Just a Few Basic Items)

Started by
22 comments, last by Rydinare 16 years, 2 months ago
There's been a claim at my job as to what the most common coding styles used in the world are, which appears to be a blatant lie. Nonetheless, without revealing which the claim is, I was curious what others thought? Just a few basic items? 1) Tabs vs. Spaces? Which is more common? 2) What's the most common usage for how many spaces a tab character is? 2, 3, 4, 5, 8? 3) Usage of tabs or spaces for alignment (as opposed to indentation)? These are really minor issues, but the tech lead is making a big deal about them, and I was just curious what really is the most common style? Thanks.
Advertisement
1. It doesn't matter. A modern IDE will allow you to reformat the code to your own style. There are technical advantages to spaces if you're burdened with an archaic IDE for some reason.

2. 4 I believe is standard for most editors, some people prefer 2, some 6 or 8. Odd numbers of spaces are... odd.

3. Again, technical advantages for spaces if you're using stone knives and bearskins.
A very minor thing; but some people just freak out about the little things. [smile]

1) I've generally only seen tabs.
2) Whatever is VisualStudio's default. otherwise 4

3) no idea what you mean by this. indentation is the use of tabs or spaces.

-me
Quote:Original post by Telastyn
1. It doesn't matter. A modern IDE will allow you to reformat the code to your own style. There are technical advantages to spaces if you're burdened with an archaic IDE for some reason.

2. 4 I believe is standard for most editors, some people prefer 2, some 6 or 8. Odd numbers of spaces are... odd.

3. Again, technical advantages for spaces if you're using stone knives and bearskins.


Are you saying that some IDEs give you astyle-like options when you're editing a file, to display it one way, but save it differently? If so, that would be great.
Quote:Original post by Palidine
A very minor thing; but some people just freak out about the little things. [smile]

1) I've generally only seen tabs.
2) Whatever is VisualStudio's default. otherwise 4

3) no idea what you mean by this. indentation is the use of tabs or spaces.

-me


On point #3, what I was referring to is differentiating indentation vs. alignment. For indentation, that's when you use a tab/x spaces to the left. Alignment is when you use character placement to line things up, say for a comment to the right. Quick example:

    identationIsToTheLeftOfThis;    // Alignment is how it lines up to the right of the semicolon    IndentedConstructor() :        indentedAndAlignedInitializerList(), ...

Quote:Original post by Rydinare
Quote:Original post by Telastyn
1. It doesn't matter. A modern IDE will allow you to reformat the code to your own style. There are technical advantages to spaces if you're burdened with an archaic IDE for some reason.

2. 4 I believe is standard for most editors, some people prefer 2, some 6 or 8. Odd numbers of spaces are... odd.

3. Again, technical advantages for spaces if you're using stone knives and bearskins.


Are you saying that some IDEs give you astyle-like options when you're editing a file, to display it one way, but save it differently? If so, that would be great.


Not to my knowledge. They simply provide options to the formatting engine. Netbeans has profiles which would easily allow you to swap preferences and reformat before saving/checkin. Visual Studio Express at least doesn't seem to. I've heard of places that ran a scheduled auto-formatter on their source control to control the rampant 'everything changed!' that diff sees when the code is simply reformatted differently everywhere.
Quote:Original post by Rydinare
There's been a claim at my job as to what the most common coding styles used in the world are, which appears to be a blatant lie. Nonetheless, without revealing which the claim is, I was curious what others thought?

Just a few basic items?

1) Tabs vs. Spaces? Which is more common?

2) What's the most common usage for how many spaces a tab character is? 2, 3, 4, 5, 8?

3) Usage of tabs or spaces for alignment (as opposed to indentation)?

These are really minor issues, but the tech lead is making a big deal about them, and I was just curious what really is the most common style?

Thanks.


As far as I am concerned, there are only two ways to get it seriously wrong:

#1.

    int foo (int bar, int baz) {  float wibble;        Object quux,              spam,                ni;        if (something) {              }     }// I.e., you don't care at all.


#2.

int foo(int bar, int baz) {    float wibble;    Object quux, spam, ni;    if (something) { // 4 spaces	if (something else) { // tab	    // tab followed by 4 spaces	}    }}// I.e., assuming that a tab represents a specific number of spaces, and then treating spaces and tabs interchangeably.


For almost anything else, you should show a little tolerance. People have their reasons. (But do aim for consistency, no matter what.)



Anyway, whatever's "most common in the world" is irrelevant (and subject to broad disagreement anyway): use what your team finds most agreeable, and stop wasting time worrying about it.
If you used tabs for indenting, you'd have to use tabs for alignment, right?

so:

tabs, 4, tabs

but I think a lot of IDEs convert tabs to spaces, so you use the tab key but it saves spaces?
Quote:Original post by Telastyn
1. It doesn't matter. A modern IDE will allow you to reformat the code to your own style. There are technical advantages to spaces if you're burdened with an archaic IDE for some reason.

2. 4 I believe is standard for most editors, some people prefer 2, some 6 or 8. Odd numbers of spaces are... odd.

3. Again, technical advantages for spaces if you're using stone knives and bearskins.


As a SK&BS (heh) user myself, you get the best advantage from tabs for indentation, but spaces for alignment. The tabs let everyone set their tab stop as they like to indent the amount they like (and eliminates arguments - unless you have wackos on your team who would seriously propose to indent by more than one tab per level!), while spaces for alignment make sure that things are aligned no matter how big the tabs are, or whether they use a "tab stop" system or are just interpreted as N spaces wide.

As for the number of spaces that a tab is interpreted as, that doesn't belong in a coding spec, because setting a value in the spec would (a) defeat the purpose of tabs (see above); and (b) seed the very dangerous (or at least irritating) meme that tabs and spaces can be treated interchangeably. That said, I've seen increasingly more people interpreting them as 3 spaces these days, and I'm sure I've seen editors in my time that assumed 5 spaces. (I like 2, personally.)

Some people outlaw tabs simply because they feel they aren't worth the effort. I really don't see that they're difficult to get right, though. Although they do cause one major annoyance for me as a forum user: you can't type them directly in the posting box x.x

Quote:Original post by Boder
If you used tabs for indenting, you'd have to use tabs for alignment, right?


Er, no. Alignment is the extra space you put after indentation, which is (some indent distance) times (level of indentation). You can set indent distance = 1 tab, tab / space = undefined, and still use spaces to line things up past the level of indentation. Which is exactly how I do it.
His example shows a right-hand comment, for "alignment" so tabs would have to be used to guarantee alignment of the left edge

  func();     // comment after five spaces  if(1)  {    boot();   // comment after three spaces  }    // now tabs=4    func();     // comment after five spaces    if(1)    {        boot();   // comment after three spaces    }


Thinking more, using tabs would probably just make it worse [sick]

This topic is closed to new replies.

Advertisement