Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Indentation (4 spaces vs 8 spaces)

This thread is locked; no one can reply to it. rss feed Print
Indentation (4 spaces vs 8 spaces)
OICW
Member #4,069
November 2003
avatar

I prefer 2 spaces. Soft indentation at all costs, doesn't matter if the number of spaces should be higher, but tabs are evil. Because tab character is represented by various number of spaces across the editors. So I set tab in IDE to insert 2 or 4 spaces instead.

And I know what I'm talking about, I opened .cs file generated in MSVC by two people in kwrite and what I got was a mess. Code wasn't aligned, because indentation was all messed up - sometimes spaces, sometimes tabs.

[My website][CppReference][Pixelate][Allegators worldwide][Who's online]
"Final Fantasy XIV, I feel that anything I could say will be repeating myself, so I'm just gonna express my feelings with a strangled noise from the back of my throat. Graaarghhhh..." - Yahtzee
"Uhm... this is a.cc. Did you honestly think this thread WOULDN'T be derailed and ruined?" - BAF
"You can discuss it, you can dislike it, you can disagree with it, but that's all what you can do with it"

Matthew Leverton
Supreme Loser
January 1999
avatar

Audric
Member #907
January 2001

Tab characters are always ambiguous, some software show them as N hard spaces, some align on columns multiple of N.
I'm working on large existing code (1.5Mb of C) with a few other coders, and as long as nobody puts a tab character we're all fine.
When a tab does appear, it's easy to find and fix.
Googlecode doesn't handle tab characters, the code loses all indentation when viewed in the site.

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

Googlecode doesn't handle tab characters, the code loses all indentation when viewed in the site.

Why don't I believe this? Oh, because it's completely untrue ::)

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Audric
Member #907
January 2001

Well, on Firefox 2 the tabs disappear entirely. I don't blame the software or site, I just try to do my best with what I have (which is windows 98).
Glad to know it works on whichever browser you're using.

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

Glad to know it works on whichever browser you're using.

Firefox 3, and tabs displayed fine in Firefox 2 ::)

In fact, they work in all versions of Firefox.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Onewing
Member #6,152
August 2005
avatar

Working by myself, whatever the default tab is.
Working with anyone else, whatever the group decides.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

decepto
Member #7,102
April 2006
avatar

Interesting reading for anyone interested in the coding style used in the linux kernel.

http://www.linuxfromscratch.org/alfs/view/hacker/part2/hacker/coding-style.html

My favorite quote from it:

Quote:

...if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.

--------------------------------------------------
Boom!

Jonatan Hedborg
Member #4,886
July 2004
avatar

Quote:

...if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.

That's stupid. In many modern languages, there is a namespace, a class and then methods... That's 3 levels of indentation right there. No control structures for you!
Tabsize 8 is just HUGE, and does not (imo) make it any more readable.

bamccaig
Member #7,536
July 2006
avatar

Jonatan Hedborg
Member #4,886
July 2004
avatar

I'm not so sure.

Quote:

Tabs are 8 characters, and thus indentations are also 8 characters. There are heretic movements that try to make indentations 4 (or even 2!) characters deep, and that is akin to trying to define the value of PI to be 3.

He seems pretty religious about it :D

Audric
Member #907
January 2001

To enforce a coding style you can convince people, or you can terrorize them into it :)

OT: CGamesPlay, I was doubly mistaken about googlecode, the rendering is flawless on all FF versions, it's on ie6 (at work, I must have used it by accident) that it fails. Not on the page you tested (text/plainm no HTML), but on http://code.google.com/p/linkage/source/browse/trunk/src/EditColumnsDialog.cc which converts TAB characters from source into 8 spaces of <pre> HTML, and in http://code.google.com/p/linkage/source/detail?spec=svn545&r=512 which preserves TABs and spaces. In both pages, all indenting is lost, no matter if it's done in spaces or tabs.

Steve++
Member #1,816
January 2002

While it's true that the display of tabs depends on the viewer, tabs should not be used on a line after any non-tab characters. So they should be used strictly for leading indents. When that rule is followed, the readability of code shouldn't be affected by the number of spaces that the tab character represents for the person viewing it.

Johan Halmén
Member #1,550
September 2001

Quote:

He seems pretty religious about it

Well, there's something religious in the belief that there would be an optimal coding style, whether we're speaking about indents, is-a/has-a, global/local variables, mixing C with C++ or whatnot.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

ReyBrujo
Moderator
January 2001
avatar

I hate tabs, they are awful, especially when people combine them with spaces (for example, if tab is set at 8 and they are indenting with 4, and need to do 12, they press a tab and four spaces!).

So, I press the space bar four times and done.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Jonatan Hedborg
Member #4,886
July 2004
avatar

Quote:

or example, if tab is set at 8 and they are indenting with 4, and need to do 12, they press a tab and four spaces

What kind of idiot would do that? One tab = one level of indentation. If they have 8 that would be at 8, 16 and 24. Why would they need to do 12?

bamccaig
Member #7,536
July 2006
avatar

We've already been over this... The optimal solution is to use tabs for indentation and spaces for alignment.

teh_codez
{
[tab]teh_codez
[tab]{
[tab][tab]teh_codez
[tab][tab]teh_codez moar_codez moar_codez
[tab][tab]                     moar_codez
         ^ identation         ^ alignment
[tab][tab]teh_codez
[tab]}
[tab]teh_codez
}

No matter what editor you view that code in, it should always appear cleanly indented. You can also indent with a single key press and remove indentation (even in simple editors) with a single key press.

ReyBrujo
Moderator
January 2001
avatar

Quote:

What kind of idiot would do that? One tab = one level of indentation. If they have 8 that would be at 8, 16 and 24. Why would they need to do 12?

Many. Especially if they want 4 spaces as tab, but they never configure tab to move 4 instead of 8, so they just do it manually mixing spaces and tabs.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

X-G
Member #856
December 2000
avatar

3 spaces.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Goalie Ca
Member #2,579
July 2002
avatar

Softabs of 4 spaces in vim. Alignment comes out same in all editors. Vim's backspace doesn't break either.

-------------
Bah weep granah weep nini bong!

Matthew Leverton
Supreme Loser
January 1999
avatar

Softabs of 42 spaces in vim. Alignment comes out same in all editors. Vim's backspace doesn't break either.

Tobias Dammers
Member #2,604
August 2002
avatar

Indentation is for sissies. Real Programmers(TM) don't use line breaks, either. After all, unnecessary whitespace is wasteful.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

BAF
Member #2,981
December 2002
avatar

That linux coding style is awful. And the doc is obviously written by an opinionated douchebag too.

CGamesPlay
Member #2,559
July 2002
avatar

It was written by Linus?

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

ImLeftFooted
Member #3,935
October 2003
avatar

I like to indent the outtards instead of the innards. Since indented things stand out more the important stuff (like function names) get the most attention.

        void output(ostream &out)
        {
      for(int i = 0; i < 24; i++)
    if(i%24)
  for(int j = 0; j < i; j++)
out << i * j << endl;
        }

It also just looks prettier.



Go to: