Sign in to follow this  
Rydinare

Most Used Coding Styles (Just a Few Basic Items)

Recommended Posts

Rydinare    487
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 this post


Link to post
Share on other sites
Telastyn    3777
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 this post


Link to post
Share on other sites
Palidine    1315
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 this post


Link to post
Share on other sites
Rydinare    487
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.

Share this post


Link to post
Share on other sites
Rydinare    487
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(), ...


Share this post


Link to post
Share on other sites
Telastyn    3777
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.

Share this post


Link to post
Share on other sites
Zahlman    1682
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.

Share this post


Link to post
Share on other sites
Boder    938
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 this post


Link to post
Share on other sites
Zahlman    1682
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.

Share this post


Link to post
Share on other sites
Boder    938
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 this post


Link to post
Share on other sites
Rydinare    487
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 Boder
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]


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 this post


Link to post
Share on other sites
Rydinare    487
Quote:
Original post by speciesUnknown
This 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 this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by Rydinare
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?)


Utter BS, yes.

Share this post


Link to post
Share on other sites
Rydinare    487
Quote:
Original post by Boder
Okay, my new answer (my original one, before I did some serious overthinking) is

tabs,4,spaces


Can you elaborate?

Share this post


Link to post
Share on other sites
ToohrVyk    1596
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 this post


Link to post
Share on other sites
Rydinare    487
Quote:
Original post by ToohrVyk
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).


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 this post


Link to post
Share on other sites
ToohrVyk    1596
Quote:
Original post by Rydinare
The 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 this post


Link to post
Share on other sites
Rydinare    487
Quote:
Original post by ToohrVyk
Quote:
Original post by Rydinare
The 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 this post


Link to post
Share on other sites
ToohrVyk    1596
Quote:
Original post by Rydinare
Just 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 this post


Link to post
Share on other sites
Metalstorm    150
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 this post


Link to post
Share on other sites
Rydinare    487
Quote:
Original post by ToohrVyk
Quote:
Original post by Rydinare
Just 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this