Tab-space VS 4-spaces

Pages: 12
Inspired by http://www.cplusplus.com/forum/beginner/249833/#msg1100357
(btw I had to look at the page source to get the #msg, is there an easier way?)

What do you peoples prefer using/use?
The programmer's best friend TAB-SPACE
or
The 'secretly a witch' programmer's best friend 4-SPACES

1
2
3
4
5
6
7
8
while (This Universe Exists) {
	if (I Have Free Time) {
		cry();
	}
	else {
		do_chores();
	}
} // Tab-Space 


1
2
3
4
5
6
7
8
while (This Universe Exists) {
    if (I Have Free Time) {
        cry();
    }
    else {
        do_chores();
    }
} // 4-Spaces 


They're both the same in my IDE so it's just about preference for outside yous IDEs I guess.

I changed from tab-space to 4-spaces like a minute ago, for the lols of it.

What are you waiting for? Click that reply button and write what you use!
edit: Feel free to share your formatting style in general!
Last edited on
closed account (E0p9LyTq)
3 spaces, no tabs, is what I prefer....

along with braces lining up vertically, not K&R style from C....

Let the Religious Format Wars begin!
Last edited on
Yea this thread should have been a "what's your formatting preference" in general.

-> I like K&R for braces.
-> For identifiers I use something like: name_of_boy, name_of_cat
First letter caps for class/structs and all caps for constants
-> 4-spaces because that was the default on Visual Studio
-> I particularly like pizza
-> I believe that the universe was created yesterday
-> I'm secretly a witch with two pet leopards and one tongue
HOLY WAAAR

I prefer 4-spaces for this forum because tabs look ridiculous on here.
Theoretically, I would prefer the semantic & customization benefits of tabs, but in practice I always go with spaces.
http://wiki.c2.com/?TabsVersusSpaces
http://wiki.c2.com/?SyntacticallySignificantWhitespaceConsideredHarmful

I also line up braces vertically, except for class names, for no logical reason.
Last edited on
(btw I had to look at the page source to get the #msg, is there an easier way?)
Use the little chain next to the post's date.

I only have two rules:
1. No whitespace other than newlines.
2. No more than one lexical token per line.
closed account (E0p9LyTq)
Yea this thread should have been a "what's your formatting preference" in general.

You really, really, REALLY do NOT want me to copy/paste my entire set of design/coding style documents here.
I go for 3 spaces, no tabs for indentation (though tabs are quite good for their other role - lining up columns of data).

3 spaces because
- I find it the minimum whitespace to easily visually distinguish indentation;
- it's not even, so I'm not tempted to "half-indent" as I would with 4 = 2 + 2 spaces;
- even after 4 levels of indent (function + 3 nested loops) it's still only 12 spaces across the page.

I don't like tabs (for indentation) because
- default tab spacing is different for different editors;
- accidentally mixing tabs and spaces ends up looking ridiculous; and if, like me, you're also trying to learn Python you are in for a nasty shock;
- default tab settings (typically 8 or more) are ridiculous; if you switched from tabs to spaces would you really want to write eight in a row?
- default tab settings waste vast amounts of screen space with multiple levels of indentation;
- the editor that I use has << and >> prefix commands which allow me to shift whole sections of text left or right by any specified amount; this simply wouldn't work with tabs.

I do, however, often use tabs for aligning output. Then tab spacing of 8, 10 or 12 seems quite reasonable.


Braces:
- I just find them easier to identify and check if they are matched vertically; thus,
1
2
3
4
   for ( ... )
   {
       // ...
   }



std::
- Only when absolutely necessary; I'm a scientist/engineer/academic, not a professional programmer, and the maths I'm trying to code is quite complicated enough without adding a whole load of extra symbols. Besides, Fortran doesn't use it!


But if we all used the same it would be a boring world. (@Ganado's links were quite amusing!)
Last edited on
closed account (E0p9LyTq)
@lastchance,

I am exactly the opposite of your regarding the std namespace. I ALWAYS qualify each use with std::

The C++standard introduced the concept of namespaces for a reason, I don't "go the lazy route" and circumvent things with using namespace std;.

At first it was hard to remember to type std:: but now it is virtually automatic.
FurryGuy wrote:
I am exactly the opposite of your regarding the std namespace.


Ah, you're forgiven! I'm well aware that I'm in a minority here!
Last edited on
I prefer tabs. Personally I use a tab width of 4 but I write my code so that it should work with any tab width greater than 1 *. The trick is to use tabs only for indenting scopes (and similar) and spaces if I want to align expressions within the same scope.

1
2
3
4
5
6
7
// lines = tabs, dots = spaces
if (x)
{
	std::for_each(vec.begin(), 
	⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅vec.end(), 
	⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅myFunctor);
}

This has the advantage that whoever views my code can use whatever tab width that he or she likes without having to modify the code.

What I don't like about spaces is that it's easy to accidentally end up with too few or too many spaces, and I like being able to navigate my code using the arrow keys and having to step through each space feels slow.


* The reason my code is not compatible with a tab width of 1 is because I prefer to indent my member initializer lists with tabs, so that they line up with the code in the constructor body, and at the same time put the colon on the same line as the first initializer, like this:

1
2
3
4
5
6
Class::Class()
:	mem1(),
	mem2()
{
	stm;
}

Who uses a tab width of 1 anyway?
Last edited on
helios wrote:
I only have two rules:
1. No whitespace other than newlines.
2. No more than one lexical token per line.

I
assume
this
is
a
joke
.
The trick is to use tabs only for indenting scopes (and similar) and spaces if I want to align expressions within the same scope.
Yup. IMO that's the correct way to use tabs and spaces. Tabs for structure and spaces for alignment. I wish there were smarter IDEs capable of formatting code like this automatically.

It seems the forum screwed up the formatting in your example, though.

I
assume
this
is
a
joke
.
Heh heh heh. Yep.
2-space indention and a 80-character limit is how I like to do things. Some people think the 80 character limit is a bit ridiculous, but I like it since it provides me a nice split screen view of my code. I think formatting should be consistent with the project's formatting. For example, if I'm helping out someone with their code, I'll use their formatting instead of my own.
2-space indention and a 80-character limit is how I like to do things.


Same. I used to hate it for years, but having to write so much ruby kind of forced me to write in that style. I've come to use it for everything now.
closed account (E0p9LyTq)
Some people think the 80 character limit is a bit ridiculous, but I like it since it provides me a nice split screen view of my code.

I'll normally go to 100, I use a wide sized editor widget. Occasionally I might go a couple of characters longer.

If I do split the line(s) I try to vertically align into "logical" groupings. For instance:

1
2
3
4
5
   hwnd = CreateWindow(szWinClass, szAppTitle,
                       WS_OVERLAPPEDWINDOW,
                       CW_USEDEFAULT, CW_USEDEFAULT,
                       CW_USEDEFAULT, CW_USEDEFAULT,
                       NULL, NULL, hInstance, NULL);
closed account (E0p9LyTq)
File extensions for source/header files.

1. files that contain C code: .c/.h are the file extensions

2. files that contain C++ or mixed C/C++ code: .cpp/hpp.
FurryGuy wrote:
1
2
3
4
5
   hwnd = CreateWindow(szWinClass, szAppTitle,
                       WS_OVERLAPPEDWINDOW,
                       CW_USEDEFAULT, CW_USEDEFAULT,
                       CW_USEDEFAULT, CW_USEDEFAULT,
                       NULL, NULL, hInstance, NULL);

I used to code like a lot, but just recently I've been moving towards preferring this instead:
1
2
3
4
5
6
   hwnd = CreateWindow(
      szWinClass, szAppTitle,
      WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, CW_USEDEFAULT,
      CW_USEDEFAULT, CW_USEDEFAULT,
      NULL, NULL, hInstance, NULL); // sometimes put closing  ) on separate line, depending on the situation. 

This makes it more compatible with users that would prefer tabs, and reduces the unnecessary whitespace rectangle that forms on the left-hand side. And I love whitespace. Haven't completely been reverent in this, though.

But I also don't feel strongly towards restricting the characters per line. I just do what feels right to me.

1
2
3
// I also always put a space after a comment starts and try
// to make multiple lines have close to same character count.
//unless_its_commented_out_code(); 


FurryGuy wrote:
2. files that contain C++ or mixed C/C++ code: .cpp/hpp.
Absolutely.

Also, if anyone reading this doesn't put spaces between assignment tokens (a = b), please know that I hate you :)
Last edited on
closed account (E0p9LyTq)
This makes it more compatible with users that would prefer tabs, and reduces the unnecessary whitespace rectangle that forms on the left-hand side.

Readability/understandability is the key, and I personally find your style close to being confusingly unreadable.

I code for myself, not for others.

If others don't like how I do things, tough noogies.
Indentation, 4 spaces.
I've tried 2 and 3 in the past. 2 was OK on chunky old ttys, but with smaller fonts (and poorer eyesight), it doesn't quite do enough for me in making the indentation obvious.

And 3 isn't a power of two. It's fine as is, but just try doing a tabs-to-spaces conversion. It's easy if you're trying to get to tab=4 spaces or tab=2 spaces. But 3 spaces, man, you need a proper conversion tool and not a simple text editor s/\t/ /g.

K&R style, because braces are only of benefit to the compiler.
Opening braces on a line all by themselves are just a waste of screen space.

If you can't understand the program flow simply by following the indentation, then it needs to be fixed. I do like Python, but the philosophy goes way back.

If I see badly formatted code, I don't think "wow, so creative, let me count the braces". No, it's "WTF is this sxxt, format that sucker now!".

If you need to match braces for some reason, because you messed it up, use an editor with brace matching highlighting. Life's too short to count the damn things.

Never try to reformat this :)
https://en.wikipedia.org/wiki/Whitespace_(programming_language)
closed account (E0p9LyTq)
you need a proper conversion tool and not a simple text editor

The Visual Studio IDE editor reformats code, with a lot of customizable parameters.

Copy/paste "weird" formatted code and the editor will do its magic. Or simply hightlight the code you want to format, select reformat and *poof*

There are also several addon tools for doing alignment formatting, such as lining up equal (=) signs, etc.

If I see badly formatted code, I don't think "wow, so creative, let me count the braces". No, it's "WTF is this sxxt, format that sucker now!".

That is why I don't like K&R braces, when they are vertically aligned you don't have to play the "find the braces" game.

spaces between assignment tokens (a = b)

Without added spaces between the parentheses: no ( a = b ).

And spaces after statements: if (condition), not if(condition).

NO whitespace between the statement and the semicolon (;) !!!!!!!
Pages: 12