formatting

Pages: 1234
I notice a lot of people format their code like this. 8 space indents, bracket below the control statement.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
  
        double x;
        
        cin >> x;

        if ( x != 3 );
        {       
                x -= 1/x;
                
                while (x <= 5)
                {
                        x += 0.001;
                {
        {

        return 0;
}

I usually format my code like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
    
    double x;
    
    cin >> x;

    if ( x != 3 ){    
        x -= 1/x;
        while (x <= 5)    
            x += 0.002
    }

    return 0;
}


4 space indents, bracket after the control statement, no unnecessary brackets, no line spacing inside blocks.

Does it matter really which way you format? Is there are particular standard style I should get used to?
Last edited on
The only things that are important are that your style is legible and consistent. It might even change while you learn and see other people's styles. When I started out, I wrote code like this:
1
2
3
4
5
6
7
8
int main()
{
    if (expression)
    {
        statement;
    }
    return 0;
}

then changed to
1
2
3
4
5
6
int main() {
    if (expression) {
        statement;
    }
    return 0;
}

and now I write like this:
1
2
3
4
5
6
7
int main()
{
	if (expression) {
		statement;
	}
	return 0;
}
closed account (zb0S216C)
Personally, I feel 8-space tabs are too much; 4 is the usual size.

iseeplusplus wrote:
"Does it matter really which way you format?"

In some cases, yeah'p. For instance:

1
2
3
4
5
6
7
8
9
10
11
// This:
if(/*something*/)
    X = Y;
    Y = Z;

// ...is not the same as:
if(/*something*/)
{
    X = Y;
    Y = Z;
}

iseeplusplus wrote:
"Is there are particular standard style I should get used to? "

Nope; use a style that you're comfortable with. However, if you're working with a team, keep the style consistent. Otherwise, go nuts.

Wazzak
Last edited on
Tip: Don't indent your code, you'll make mistakes.
Leave that matter to your editor.
There's also the question of whether to use spaces or actual tabs.
For instance, on this site, a tab expands to 8 columns.
But in your editor you can set it to anything, including 4.
Anything 4 spaces or bigger is okay, although I consider an enemy anyone who uses a tab size that isn't 8 columns. Also, odd numbers of spaces are just plain wrong.

I personally think tabs are better since the specific purpose of the tab is for indentation. Generally, tabs automatically align to the next column whose number is a multiple of eight (8, 16, 24, etc.), which is better for aligning code (which is the entire point of indentation). The only problem with tabs is that you can't always align things perfectly.

Finally, I am against using more than 80 characters on a line. I'll go over a bit if it looks better, but I can't stand code lines that take up significantly more than 80 characters. If I split a line, I'll indent it until it aligns with something or gets as close to the 80th column without going over.
I consider an enemy anyone who uses a tab size that isn't 8 columns.

Do you also take every second step with the same foot?
Eight columns is simply wasteful for source code, in my opinion.

What really grinds my gears is when people mix 8 column tabs and spaces.
(Which happens to be the default mode in Kate, KWrite, and KDevelop.)
Catfish2 wrote:
Do you also take every second step with the same foot?

I tend to put one foot in front of the other: left, right, left, right, so yes. Unless you mean left, left, right, right, in which case, of course not.

I don't particularly care what people use on their own text editors as long as it renders correctly when it's on mine - if they use 4-space tabs it's usually okay, unless they (for whatever reason) then use two tab characters per indent (8 spaces per indent, which on a sane person's text editor would be 16 spaces).

The worst thing is when someone puts one of those "-*-" control lines at the top of the file and changes your editors settings. If I contribute code to someone else's project, I make sure to emulate their style as much as I can, but if I'm editing their code for my own use, I don't want them overriding my editor's settings.

Even though I'm officially against it, I sometimes mix tabs and spaces:
1
2
3
4
		/* Without tabs & spaces, the asterisks wouldn't line up!
		 */
		std::cout << "Some text which almost fills up 80 columns."
			  << std::endl;

For that reason, I've actually been considering switching to tab-with-spaces recently. I don't want to, but mixing tabs and spaces feels wrong.
closed account (1yR4jE8b)
Your example with the comment stars lining up is exactly why I *only* use spaces. Mixing tabs and spaces is horrible, especially when working with other people that use different tab column-sizes -- nothing ever lines up correctly.
Your example with the comment stars lining up is exactly why I *only* use spaces. Mixing tabs and spaces is horrible, especially when working with other people that use different tab column-sizes -- nothing ever lines up correctly.


This has irritated me n the past to the point where I decided to stop using tabs. But it's too much extra typing using spaces, so now I use tabs, and then use the editors detabify feature later.
Last edited on
But it's too much extra typing using spaces


95% of IDEs / text editors (and 100% of the ones worth using) have a feature to insert spaces on tab. So it shouldn't be any extra typing.
When you need to move along the line and use backspace and stuff, it gets annoying to have to press the arrow or backspace key eight times per indent level. I guess editors could solve the issue by having the arrow and backspace keys align to tab-stops, but by that point you're basically jumping through hoops to keep the automatic features that tabs give you, and may as well just say "F*ck the hoops, let's use tabs".

Also, tabs don't use up more space - au contraire, they use less: one tab is a single character ('\t') while an equivalent amount of spaces is 8 characters. Assuming an average number of 2 indents per line and an average output of 100 LOC per programmer per day, if every programmer used spaces instead of tabs, that would be 1600 bytes extra per programmer per day. If we estimate the total number of programmers in the world to be 6 million, that's a total of 8.94 GiB extra per day, or 3.2 TiB per year. That's a total of about 20,000 kg worth of hard drive assuming an average density of 625 Gib (gigabits) per square inch. If the average hard drive has a mass of 600 grams, then the extra data is equivalent to 16,000 hard drives running year-round. Assuming an average power consumption of 6 W, in a year, each hard drive is using 1.5 GJ of energy. In total, all 16,000 hard drives are using 6 TJ (6 tera-Joules) of energy per year. The obvious conclusion is that if you're a programmer who uses spaces instead of tabs, you're DESTROYING THE PLANET.

[edit] My maths was waaay off:
8 spaces * 2 indents per line * 100 LOC * 6,000,000 programmers * (60 * 60 * 24 * 365.25) seconds = 3x1017 Bytes = 269 PiB per year
[(3x1017 Bytes) / (625 / 8)x109 B] / 600x10-3 kg = 6,462,996 hard drives per year
6,462,996 hard drives * 6 W * (60 * 60 * 24 * 365.25) seconds = 1015 or 1 PJ (Peta Joules). With an estimated human consumption of 6 TW or roughly 2x1020 Joules, or 189,346 PJ per year, programmers who use spaces are contributing a total of 0.00053%, or roughly 10-12% each.

I've done a lot of rounding there but it should be roughly correct.
Last edited on
closed account (3hM2Nwbp)
chrisname speaks the truth.
*slow clap*

That was beautiful.
The obvious conclusion is that if you're a programmer who uses spaces instead of tabs, you're DESTROYING THE PLANET.


Lolz! I'd sig this but...
it gets annoying to have to press the arrow or backspace key eight times per indent level


You need to learn how to use the Ctrl key.

Also 8 spaces per tab? That's waaay too much. Anything over 6 is complete insanity.


that would be 1600 bytes extra per programmer per day.


Your efforts to make that sound significant are comical. =)
I will try this "Ctrl key" you speak of (p.s. only 4 and 8 spaces per indent are allowed (no exceptions: void only_4_or_8_spaces_per_indent() throw();)).

I got the idea from http://peterbraden.co.uk/article/space-infidel and then I decided to do the same but in a slightly more round-about way.
I would agree that tabs would be better if everything displayed them the same way. But that isn't the case.

He side-steps that point with this:
And seriously, if your editor can't display tabs with your preference of spaces then your editor sucks. I don't care how l33t it makes you.


Which is meaningless because:

1) It's not just your editor. Copy/pasting code to websites (like this forum, which does not allow you to configure how tabs are displayed), emails (where you have no control over how the recipient displays their tabs), etc, or pretty much any other situation where you're having other people look at your code.


2) It screws up single-line pretty print:

With spaces:
1
2
3
4
5
            /* Branches */
        case 0x10:  Branch( (mState.fNZ & 0x180) == 0 );    break;  /* BPL */
        case 0x30:  Branch( (mState.fNZ & 0x180) != 0 );    break;  /* BMI */
        case 0x50:  Branch(  mState.fV           == 0 );    break;  /* BVC */
        case 0x70:  Branch(  mState.fV           != 0 );    break;  /* BVS */


With tabs:
1
2
3
4
5
			/* Branches */
		case 0x10:	Branch( (mState.fNZ & 0x180) == 0 );	break;	/* BPL */
		case 0x30:	Branch( (mState.fNZ & 0x180) != 0 );	break;	/* BMI */
		case 0x50:	Branch(  mState.fV			 == 0 );	break;	/* BVC */
		case 0x70:	Branch(  mState.fV			 != 0 );	break;	/* BVS */



Which I suppose is avoidable if you only use tabs at the start of the line. But then you have to remember to never ever actually press the tab key. What's worse, if you forget and accidentally hit tab instead of 4xSpace you won't notice so you won't be able to fix it and your code will look stupid to other people.

So in that sense, since you can't actually press tab here, using tabs causes MORE typing, not less. Since you have to manually input the spaces. And is more error prone. And is stupider.


Spaces ftw.
Last edited on
I wasn't necessarily agreeing with him, I just liked his reductio ad absurdum argument at the end.

I generally only use tabs at the start of a line; if I want to align other things, I use spaces. I've never experienced this scenario:
Disch wrote:
if you forget and accidentally hit tab instead of 4xSpace you won't notice so you won't be able to fix it
In visual studio you can tabify or detabify selected lines. In visual studio go to edit-> advanced-> ...

In notepad++ edit -> Blank Operations-> ...

Leave in tab form, then when you want to share it, detab it first. That way they're sure to see it exactly as you wrote it.
Last edited on
Pages: 1234