# Most Used Coding Styles (Just a Few Basic Items)

## Recommended Posts

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.

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by Telastyn1. 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.

##### Share on other sites
Quote:
 Original post by PalidineA 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 43) 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(), ...

##### Share on other sites
Quote:
Original post by Rydinare
Quote:
 Original post by Telastyn1. 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.

##### Share on other sites
Quote:
 Original post by RydinareThere'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.

##### Share on other sites
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?

##### Share on other sites
Quote:
 Original post by Telastyn1. 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 BoderIf 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.

##### Share on other sites
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]

##### Share on other sites
Except for html, I try to indent using a tab set to 4 spaces. When I'm using html, I just use 2 spaces for each indent.

##### Share on other sites
Ok, there's been enough interesting responses, I suppose I can post the results of where we are.

Currently, the tech lead of the project is mandating a style. Of particular note is that the style is preferred by the minority of people in the development group, including heavily contrasting with another project he's supposed to be cooperating with.

I liked the comment of using tabs for indenting and spaces for alignment. I do the same thing. Oddly, the tech lead prefers the opposite. He uses 2 spaces for indentation and an 8-space tab for alignment (Why would someone who preaches spaces use tabs for alignment anyway?). This causes some ugly/oddly spaced code. He argues that this is the most common style in the world (utter BS, right?)

We already agreed on consistency: existing modules use existing style; new modules use creator's preference. Now he's also mandating editor configuration. I tend to use 4 space tabs, as from what I can tell, it is the most common style. He's mandating 8 space tabs, which makes no sense.

Quote:
 Original post by BoderHis 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]

A better question is why are you lining up comments from different levels of indentations? Am the only one who doesn't think it makes any sense to do so?

##### Share on other sites
This stuff is the basis for many holy wars. The only people who really gain from "Holy wars" are the gunsmiths.

##### Share on other sites
Quote:
 Original post by speciesUnknownThis stuff is the basis for many holy wars. The only people who really gain from "Holy wars" are the gunsmiths.

Absolutely. I'm merely trying to gain some perspective. The tech lead is an absolute micromanager (and he's not even my manager!). He has the classic "nothing is good enough" syndrome. This post was mostly for informative purposes (and slightly rantative).

If I had it my way, the ideal way would be that everyone uses whatever style they like as long as what was checked in is mostly consistent with the documented coding standards (there are none documented, in this case). Even if there were cases where there were slight inconsistencies, who cares? There's bigger fish to fry.

But some people just can't compromise.

##### Share on other sites
Quote:
 Original post by RydinareI liked the comment of using tabs for indenting and spaces for alignment. I do the same thing. Oddly, the tech lead prefers the opposite. He uses 2 spaces for indentation and an 8-space tab for alignment (Why would someone who preaches spaces use tabs for alignment anyway?). This causes some ugly/oddly spaced code. He argues that this is the most common style in the world (utter BS, right?)

Utter BS, yes.

##### Share on other sites
Okay, my new answer (my original one, before I did some serious overthinking) is

tabs,4,spaces

##### Share on other sites
Quote:
 Original post by BoderOkay, my new answer (my original one, before I did some serious overthinking) istabs,4,spaces

Can you elaborate?

##### Share on other sites
I dunno. I just ask the IDE to indent things for me (emacs: M-x indent-region, VS: C-k C-f) and it uses whatever is necessary (although I suspect they all use spaces, with two-space for emacs and four-space for VS).

##### Share on other sites
Quote:
 Original post by ToohrVykI dunno. I just ask the IDE to indent things for me (emacs: M-x indent-region, VS: C-k C-f) and it uses whatever is necessary (although I suspect they all use spaces, with two-space for emacs and four-space for VS).

Yes. Well, one thing is that there's at least four editors being used (vim, Emacs, Eclipse, Visual Studio). The fact that people are choosing to use vim and Emacs as their primary editor at this point almost seems ridiculous, though.

##### Share on other sites
Quote:
 Original post by RydinareThe fact that people are choosing to use vim and Emacs as their primary editor at this point almost seems ridiculous, though.

I use emacs because it's more flexible than the other editors:
• It handles OCaml. This includes a decent toplevel interaction system which I've seen in no other IDE. It also provides correct syntax highlighting: C-based IDEs generally fail to recognize nested comments, type parameters ('a is not the beginning of a character literal) or even highlight types.
• It handles LaTeX. It's auto-configured upon install, it's free, and it provides a reasonable amount of built-in WYSIWYG with minimal effort. Few editors I've seen handle LaTeX this well, and most that do are unusable for anything else and cost money.
• It handles XML and HTML, and also performs automatic validation of code. While not as advanced as a dedicated HTML editor, it's enough for my needs and doesn't require me to have several editors running at the same time.
• It handles C, C++, PHP, Java correctly without requiring anything more than apt-get install whatever-mode.
• It handles script and makefile editing.
• It's entirely keyboard-controlled by default and in a reasonably standard way across computers. This may sound useless, but it's actually great to be able to keep your hands on-keyboard at all times.

Until I can find a single editor that can handle all the above on my workstation, I will use emacs as my primary editor, and only resort to secondary editors (such as Visual Studio) when I have extremely specific work to do.

##### Share on other sites
Quote:
Original post by ToohrVyk
Quote:
 Original post by RydinareThe fact that people are choosing to use vim and Emacs as their primary editor at this point almost seems ridiculous, though.

I use emacs because it's more flexible than the other editors:
• It handles OCaml. This includes a decent toplevel interaction system which I've seen in no other IDE. It also provides correct syntax highlighting: C-based IDEs generally fail to recognize nested comments, type parameters ('a is not the beginning of a character literal) or even highlight types.
• It handles LaTeX. It's auto-configured upon install, it's free, and it provides a reasonable amount of built-in WYSIWYG with minimal effort. Few editors I've seen handle LaTeX this well, and most that do are unusable for anything else and cost money.
• It handles XML and HTML, and also performs automatic validation of code. While not as advanced as a dedicated HTML editor, it's enough for my needs and doesn't require me to have several editors running at the same time.
• It handles C, C++, PHP, Java correctly without requiring anything more than apt-get install whatever-mode.
• It handles script and makefile editing.
• It's entirely keyboard-controlled by default and in a reasonably standard way across computers. This may sound useless, but it's actually great to be able to keep your hands on-keyboard at all times.

Until I can find a single editor that can handle all the above on my workstation, I will use emacs as my primary editor, and only resort to secondary editors (such as Visual Studio) when I have extremely specific work to do.

Interesting. Just for reference, other editors handle most of those. I have no idea about OCaml or Latex. XML handles just fine in Visual Studio. When I do Java, I use Eclipse, which has a bunch of nice tools (refactoring, automatic compilation, etc...) that go along with it. That's part of the IDE experience, is you get more than just a text editor.

Anyway, while I understand that you can gain a familiarity by sticking with a single editor, I think you might also find that specialized editors for a particular task work better than a single do-it-all solution for every type of thing you're editing.

Anyway, since the only things of that list we're currently using are C++, XML and Python, I'm not sure my other team members could use the same reasoning.

##### Share on other sites
Quote:
 Original post by RydinareJust for reference, other editors handle most of those.

I can't help but notice that you are missing my entire point and arguing for something else right now than you did in your previous post. If you remember your initial statement, you might notice that it was related to the choice of a primary text editor, namely the fact that emacs or vim are ridiculous for that purpose.

I certainly agree that, for the purpose of developing C++ code, a specialized IDE such as Visual Studio is a clearly better alternative, as it provides a cleanly integrated graphical interface for debugging and a streamlined build system designed specifically for C++. However, using Visual Studio as my primary editor will not allow me to write OCaml, LaTeX, PHP or Java code with reasonable ease, which is actually quite unacceptable. This makes Visual Studio a good choice for a specialized text editor (as it handles C++ and C# code quite well), but a lousy choice for a primary text editor.

And so, my point is that most existing editors are good specialized editors, but none of them qualify as a good primary editor, because they can't handle the breadth of everything I do.

Quote:
 Anyway, while I understand that you can gain a familiarity by sticking with a single editor, I think you might also find that specialized editors for a particular task work better than a single do-it-all solution for every type of thing you're editing.

This is not an argument against using Foo or Bar as a primary editor. This is an argument against the very existence of a primary editor, as opposed to a toolbox of specialized editors. I personally tend to only consider using a specialized editor when the project is large enough to gain something from it, and I routinely use Visual Studio for large C++ projects.

On the other hand, you must notice that in essence, Visual Studio is actually an unspecialized editor with specialized "plugins" for C# and C++, and Eclipse also works based on a "plugin" system which can support Java, C++, OCaml, and others. The same goes for emacs, which is an unspecialized editor with specialized "plugins" for C, C++, Java, PHP, XML, OCaml, LaTeX... Ultimately, the quality of a tool is a combination of that tool's general usability, combined with the efficiency of its specialized plugins.

##### Share on other sites
I just use the tabs as spaces option in VS to keep any people that are picky about such things happy. Otherwise there was the possibility that someone assumed that a tab had so many spaces and someone else just put in tabs and that it won't be represented the same across different editors/setups.

If it is just me editing the file then if I have tabs in the code it doesn't really matter as I will keep it uniform.

##### Share on other sites
Quote:
Original post by ToohrVyk
Quote:
 Original post by RydinareJust for reference, other editors handle most of those.

I can't help but notice that you are missing my entire point and arguing for something else right now than you did in your previous post. If you remember your initial statement, you might notice that it was related to the choice of a primary text editor, namely the fact that emacs or vim are ridiculous for that purpose.

I certainly agree that, for the purpose of developing C++ code, a specialized IDE such as Visual Studio is a clearly better alternative, as it provides a cleanly integrated graphical interface for debugging and a streamlined build system designed specifically for C++. However, using Visual Studio as my primary editor will not allow me to write OCaml, LaTeX, PHP or Java code with reasonable ease, which is actually quite unacceptable. This makes Visual Studio a good choice for a specialized text editor (as it handles C++ and C# code quite well), but a lousy choice for a primary text editor.

And so, my point is that most existing editors are good specialized editors, but none of them qualify as a good primary editor, because they can't handle the breadth of everything I do.

Quote:
 Anyway, while I understand that you can gain a familiarity by sticking with a single editor, I think you might also find that specialized editors for a particular task work better than a single do-it-all solution for every type of thing you're editing.

This is not an argument against using Foo or Bar as a primary editor. This is an argument against the very existence of a primary editor, as opposed to a toolbox of specialized editors. I personally tend to only consider using a specialized editor when the project is large enough to gain something from it, and I routinely use Visual Studio for large C++ projects.

On the other hand, you must notice that in essence, Visual Studio is actually an unspecialized editor with specialized "plugins" for C# and C++, and Eclipse also works based on a "plugin" system which can support Java, C++, OCaml, and others. The same goes for emacs, which is an unspecialized editor with specialized "plugins" for C, C++, Java, PHP, XML, OCaml, LaTeX... Ultimately, the quality of a tool is a combination of that tool's general usability, combined with the efficiency of its specialized plugins.

I see where the confusion is. I meant "primary editor for C++". I apologize for the ambiguity of my statement. From what I'm talking about here, I was only referring to their C++ coding. I don't care if they use vi, emacs, notepad, etc... for editing non-C++ items, as it hasn't been an issue to date.

Aside from the terminology confusion, I believe we're generally in agreement.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628356
• Total Posts
2982253

• 10
• 9
• 13
• 24
• 11