# Indentation Style which is best?

## Recommended Posts

which indentation style do you feel is the best? and can you recommend me the best C++ text editor?

##### Share on other sites
The best is which ever YOU like most.

As far as text editors, I prefer the Visual Studio (Visual C++) editor. You can get the Express version of Visual Studio 2005 for free. For me it's the best, but it may not be for others. Best is relative.

-SirKnight

##### Share on other sites
Personally, I like the 2 space style.

For example:
void main(){  cout << "I like this indentation style!" << endl;    if (true)  {    cout << "See? It's nice." << endl;  }}

This method let's me keep track of whether my brace brackets are lined up properly, and are complete.

As for editors, there are quite a few available. Check out the Forum FAQ

##### Share on other sites
Tabs until indent level, spaces for mid-line alignment. That way, any other people who have different tab sizes will see it how they want to see it and won't have mid-line spacing problems.

##### Share on other sites
Personally, I use BSD/Allman style indentation with 4 space wide hard tabs. And since this will probably come up sooner or later, I use .NET's naming conventions in all languages that I code in.

As for editor, I stick to VS. The vim and emacs people can go on all they like, but I don't buy any of it (and I'm familiar and comfortable with both editors). Simple and direct works fine.

##### Share on other sites
Quote:
 Original post by NypyrenTabs until indent level, spaces for mid-line alignment. That way, any other people who have different tab sizes will see it how they want to see it and won't have mid-line spacing problems.

I've never understood why the majority of people don't seem to do that.

##### Share on other sites
Quote:
Original post by Roboguy
Quote:
 Original post by NypyrenTabs until indent level, spaces for mid-line alignment. That way, any other people who have different tab sizes will see it how they want to see it and won't have mid-line spacing problems.

I've never understood why the majority of people don't seem to do that.

Can you show me an example?

##### Share on other sites
Quote:
 Original post by PromitPersonally, I use BSD/Allman style indentation with 4 space wide hard tabs. And since this will probably come up sooner or later, I use .NET's naming conventions in all languages that I code in.

Yes, that's the indent style I use, except I prefer 2 space tabs. My naming conventions are pretty much a mixture of various other conventions. I do, however, seem to favor the java naming conventions for functions.

Example: void removeObject(...);

That's only because I find it more visually appealing, though.

##### Share on other sites
Quote:
Quote:
Original post by Roboguy
Quote:
 Original post by NypyrenTabs until indent level, spaces for mid-line alignment. That way, any other people who have different tab sizes will see it how they want to see it and won't have mid-line spacing problems.

I've never understood why the majority of people don't seem to do that.

Can you show me an example?

int main() {	int array = {1,	             2,	             3,	             4};	if (/*...*/) {		// ...	}}

Note which spaces are tabs and which aren't. It will all be lined up regardless of the tab size used.

##### Share on other sites
Are the .NET naming conventions the same as the Java naming conventions?

##### Share on other sites
Quote:
 Original post by BoderAre the .NET naming conventions the same as the Java naming conventions?

I think the .NET conventions capitalize the first letter of every word.

Example: GenerateObjects() and such.
That's what I've noticed from C# and VB.NET anyways.

##### Share on other sites
Quote:
 Original post by NypyrenTabs until indent level, spaces for mid-line alignment. That way, any other people who have different tab sizes will see it how they want to see it and won't have mid-line spacing problems.

And as for an editor I would recommend MSVC 2005 Express if you are on Windows.

##### Share on other sites
Quote:
 Original post by BoderAre the .NET naming conventions the same as the Java naming conventions?

.NET Naming Guidelines

##### Share on other sites
Which ever indentation style you choose, just remember the most important thing... consistency!

##### Share on other sites
Quote:
Original post by CTar
Quote:
 Original post by NypyrenTabs until indent level, spaces for mid-line alignment. That way, any other people who have different tab sizes will see it how they want to see it and won't have mid-line spacing problems.

And as for an editor I would recommend MSVC 2005 Express if you are on Windows.

yes! get it while it's free! it's by far my favorite gui-based ide.

##### Share on other sites
Quote:
 Are the .NET naming conventions the same as the Java naming conventions?

as i read in an article a while back,

Quote:
 Don't follow Java's coding/naming conventions...after all, Sun doesn't.

Just a little bit of humor.

##### Share on other sites
My C++ indentation style. I'm still working on it, but some ideas might be useful for you.

I use headlines to easily find a position in the code when scrolling up and down. My headlines end at line 80. There are different ways of writing these headlines - the headline itself framed in horizontal lines, the chapter framed this way, or just a horizontal line on top of the headline..
//===============================================================//                                                       MyClass//===============================================================class MyClass {  //  //-------------------------------------------------------------  //                                  MyClass - member functions  //-------------------------------------------------------------  //  void dosomething() {    ...  }  //  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  //                                              MyClass::foo()  // foo is my favourite function name  void foo() {    //    ...  }};

I put my opening brace behind the function name, and try to not use it as a first character. Sometimes I do the same with large argument lists of functions or templates.

Statements like public:, or case xxx: are indented with half tab size. Typical tab size I use is 2 spaces. I always tick "replace tabs with spaces" - this is more a matter of bad experiences with tabs, I don't recommend it as the better solution.

Empty lines in a function or class are filled with comment slashes to maintain the optical flow of line beginnings. This way it's easy to find the start and end of a block. I put these 'decorated' empty lines where it seems appropriate.

I still didn't find the perfect solution for templates, and for defining inheritance. For the class template I define here, I put the ending brace of the template argument list in front of the class header, so the class template can't be confused with a usual non-template class.

//---------------------------------------------------------------//                                               MyClassTemplatetemplate<  class Base,  class T,  template<class> class TT,  bool B> class MyClassTemplate : public Base {  // public:  //  template<int Size>  T* memberfunction(    TT<T> arg1,    float arg2,    float arg3  ) {    ...    if(true) {      see_what_happens();    } else {      see_nothing();    }  }};                                            // MyClassTemplate//---------------------------------------------------------------

##### Share on other sites
Quote:
Original post by Roboguy
Quote:
Quote:
Original post by Roboguy
Quote:
 Original post by NypyrenTabs until indent level, spaces for mid-line alignment. That way, any other people who have different tab sizes will see it how they want to see it and won't have mid-line spacing problems.

I've never understood why the majority of people don't seem to do that.

Can you show me an example?

int main() {	int array = {1,	             2,	             3,	             4};	if (/*...*/) {		// ...	}}

Note which spaces are tabs and which aren't. It will all be lined up regardless of the tab size used.

cool this is the style that I use anyhow!
nice.

##### Share on other sites
Why tabs instead of just spaces? All manual about good style/coding practices recommend tabs. I've been always wondering about it? Why actually does it matter?

Isn't it a more trouble when you switch from one IDE to another?

##### Share on other sites
Quote:
 Original post by NamelessTwoWhy tabs instead of just spaces? All manual about good style/coding practices recommend tabs. I've been always wondering about it? Why actually does it matter?Isn't it a more trouble when you switch from one IDE to another?

Most people prefer tabs with 4 spaces, but lets say that Dorvo is going to look at your code. Dorve states that he prefers 2 spaces. With tabs you can change the settings of the editor to make tabs another number of spaces. Also it takes longer to delete 4 lines than it takes to delete 1 tab. I can remember two good reasons to use spaces, first to make sure the source code will print properly no matter who is printing and what their settings are. Another reason is so that you don't accidently generate bad indentation, lets say I want to create a simple constructor.
		some_class_123( type obj )				:				_something( obj)

This might look good on GDNet, with tabs of 8 lines, but try pasting it in your IDE, suddenly : and _something isn't right under type, these types of indentation errors can be hard to spot unless you change indentation once in a while.

##### Share on other sites
Quote:
 Original post by NamelessTwoWhy tabs instead of just spaces? All manual about good style/coding practices recommend tabs. I've been always wondering about it? Why actually does it matter?

Well, a couple of reasons. First, it's a little easier to make sure you're using, say, exactly one tab than it is to make sure you're using exactly eight spaces. More importantly, others can choose a different tab size in their editor if they like it better; they can't nearly as easily choose a different number of spaces.
Quote:
 Isn't it a more trouble when you switch from one IDE to another?
Not if you use the tabs/spaces thing RoboGuy talked about. Besides, there hasn't been a serious editor written in the last twenty years which didn't allow you to choose tab size.

##### Share on other sites
Quote:
 Original post by CTarDorve states that he prefers 2 spaces.

I meant that my hard tabs are 2 spaces wide. Prolly should have been clearer on that one. [smile]

##### Share on other sites
My indentation style is best.

##### Share on other sites
This is all personnal preference. There is no 'best style' everyone is different.

Personally I like keeping all my brakcets on a new line.
I have my tabs set to 4 spaces.

int SomeFunc(void){    if (this == that)    {        doSomethingElse();    }    return (someValue);}

To me that is much more readable than something like

if (this == that) {    doSomething();}

MY favorite text editor is Microsoft Visual C++, with Code::Blocks coming in at close 2nd.

##### Share on other sites
int SomeFunc(void){
if (this == that){
doSomethingElse();
}//end if

return (someValue);
}//end SomeFunc

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628368
• Total Posts
2982291

• 10
• 9
• 13
• 24
• 11