84

The coding standards for the code hosted in drupal.org suggest to use two spaces to indent the code; other sites suggest to use tabs to indent the code.

What is the proper indentation character for everything, and in every situation? Please explain the answer you give.

Adam Lear
  • 31,939
  • 8
  • 101
  • 125
apaderno
  • 4,014
  • 8
  • 42
  • 53
  • [Read this first](http://www.jwz.org/doc/tabs-vs-spaces.html) – finnw Sep 03 '10 at 07:43
  • 13
    IDE default FTW - just press the tab key and see what happens – user281377 Dec 14 '10 at 11:12
  • Whatever you choose, it must be used by the whole team but not when one uses tabs others spaces. – Disciple Aug 24 '11 at 09:54
  • 1
    I tried to edit this to correct the implication that tabs/spaces are the only way to indent code. The edit was removed, but hopefully this comment will remain: Instead of indentation characters, why not use the left-margin instead? - this can be fully automated by the editor and word processors have done this for decades. – pgfearo Sep 28 '11 at 21:50
  • 4
    I really don't think there should be an "accepted" answer here. The fact the the answer with fewer votes is actually first is testament to that: why should the asker get a special power (i.e. being able to put their favorite answer first) just because they asked the question? – Dean Harding Sep 29 '11 at 12:53
  • Remember that an accepted answer is not an answer that is universally accepted from all the users; it's the answer that is accepted from the OP, who chooses the answer that helped him more. Only the OP can know which answer helped him more than other answers; even in the case of two questions that basically say the same thing, there is still an answer that can help the OP more than the other. – apaderno Sep 29 '11 at 13:01
  • [http://www.emacswiki.org/emacs-en/TabsvsSpaces](http://i.stack.imgur.com/wD5I4.png) – Keith Thompson Oct 16 '11 at 07:07
  • @Shabbyrobe: While I agree some should refrain from harshly downvoting, I think it's a powerful tool in S.E., and **a good thing** (in my opinion). There's a gap between indifference and disagreement, and the latter should be clearly voiced. If you put yourself out there w/ a statement w/o a strong basis, expect to collect a lot of these. If you do have a strong basis, you'll get the occasional disagreement (**hopefully with some explanation, which is what is often lacking**). If you **can't** back it up, then the question is possibly too open-ended, biased or subjective and shall be flagged. – haylem Feb 28 '12 at 17:19
  • @Shabbyrobe: also, it gives a strong incentive for people to delete their own answers when they are of poor quality (which isn't done often enough: if a non-winning answer is devoid of any added-value in a thread, it should simply be deleted). – haylem Feb 28 '12 at 17:21

22 Answers22

248

Tabs

Now, of course, consistency matters more than either one, and a good IDE makes the differences negligible. That said, the point of this thread is to be a holy war, so:

I prefer tabs:

  • They're a character specifically meant for indentation
  • They allow developers with different preferences in indentation size to change how it's the code looks without changing the code (separation of data and presentation for the proverbial win!)
  • It's impossible to half-indent something with tabs. So when you copy code from some website that used 3 spaces into your 4-space indented file, you don't have to deal with misalignment.
Geoffrey
  • 130
  • 2
  • 12
Fishtoaster
  • 25,909
  • 15
  • 111
  • 154
  • 12
    What about copying that 3-space indented code into your tab-indented file, which you have set your tabs to 3 spaces? When you send your code to your compatriots they wonder why that function has such bad indentation! – Wayne Werner Sep 01 '10 at 20:16
  • 27
    Tabs are not originally meant for indentation, they are meant for *tabulation*, and do a dreadful job at it... – Timwi Sep 02 '10 at 19:15
  • 14
    @Timwi: [Wikipedia](http://en.wikipedia.org/wiki/Tab_key) seems to say that they are meant for moving the cursor to the next of a set of fixed columns. Doesn't sound like tabulation was the primary function. Also, I was referring to the post-typewriter era. – Fishtoaster Sep 02 '10 at 19:31
  • 5
    I don’t see how that contradicts what I said. I did say they do a dreadful job at tabulation, but that was definitely the intended use. Why else would they be called this? – Timwi Sep 02 '10 at 19:49
  • 40
    Plus a lot of programmers spend a significant amount of time writing html, css, etc, were the fact that a file with 4 spaces instead of tabs is 5-15% larger is actually significant. *Example: this page you are on right now loaded at least 10% slower than it should have because the SE team used spaces*. Check for yourself. – MGOwen Dec 14 '10 at 01:46
  • 23
    Another benefit of this approach - fewer keypresses when using arrow keys for left-right navigation. – Nicole May 18 '11 at 17:20
  • One thing I like emacs is I can hit tab key multiple times without destroy the indentation. – Codism Sep 29 '11 at 19:58
  • 1
    @Renesis: Actually, you want to skip indentation. So it doesn't matter if there are spaces or tabs, you want your editor to jump to first occurrence text (either automatically, with ctrl+ arrow or other shortcut). – MaR Nov 11 '11 at 12:42
  • 12
    @MGOwen Actually this page is served with gzip compression, so there is no difference. – Josh K Feb 18 '12 at 18:58
  • 1
    @WayneWerner the problem is with the code that used spaces for indentation and would be just as problematic if the person cut and pasted 3-space code into 4-space code. – Jordan Reiter Aug 27 '12 at 15:12
  • @JordanReiter, Not so - with my example there is no visual difference to one coder. Copying 3-space code into a 4-space code *immediately* looks wrong without some type of whitespace visualization (and if there isn't, I suggest that programmer needs to start using a monospace font ;) – Wayne Werner Aug 27 '12 at 16:43
  • Oh right, got it. Of course, the reverse is also true. If you cut and paste in code that is tab-indented and didn't know, unless your editor auto-expands them to spaces you could run into the same problem. Although I'll add for me the real headache is reformatting indentation from, say, 3-space to 4-space, especially when the coder *also* uses spaces to line up variables, parentheses, etc. – Jordan Reiter Aug 27 '12 at 17:57
  • 2
    +1 for separation of data and presentation. If you like the look of spaces, format your presenter. Tabs are universalizable, spaces are not. – Kyeotic Jan 06 '13 at 20:58
  • The problem with IDEs is that different people use different IDEs, often within the same company. For example, with iOS programming some people could be using Xcode, others emacs and others TextMate. If you're doing cross-platform programming it only gets more complicated. Best to stick with spaces. – ThomasW May 16 '13 at 02:44
  • Also, you often end up viewing the text with a different tool: github, cat, diff, whatever. It ends up being too difficult to have all those different tools using the same tab conventions. Spaces work better. – ThomasW May 16 '13 at 02:57
  • @ThomasW True, different editors might have different default tab widths but an editor without a setting for it isn't an editor I want to use. And if a team is setting a standard for itself regarding tabs vs spaces, why not tabs with a set width? (Although that seems pretty heavy handed considering everyone can use their own preference when using tabs.) – spaaarky21 Jun 05 '13 at 18:58
  • @WayneWerner If you paste any code, you simply hit the shortcut in your IDE for whitespace formatting. This is not a point for tabs nor for spaces. – mafu Jul 16 '13 at 15:24
  • @Timwi I honestly don't care what a character was intended for. Nowadays, tabs are used for indentation in source code. The reason I'm pointing this out is because, according to the upvotes, the 'historic intention of tabs' seems to be the *main* pro point of spaces right now - that's pretty disappointing. – mafu Jul 16 '13 at 15:27
127

Spaces

A tab could be a different number of columns depending on your environment, but a space is always one column.

In terms of how many spaces (or tabs) constitutes indentation, it's more important to be consistent throughout your code than to use any specific tab stop value.

Tamara Wijsman
  • 8,259
  • 14
  • 58
  • 94
  • 42
    Your second paragraph is wrong; the answer is 4! – Daenyth Sep 24 '10 at 19:27
  • 5
    @Daenyth - perhaps if you are using a curly-brace language. Most Pascal/Delphi uses 2. – Gerry Sep 27 '10 at 01:15
  • 1
    @Gerry: No actually, python. – Daenyth Sep 27 '10 at 03:35
  • 2
    Formatting errors don't often occur anymore: almost every IDE is at tab=4spaces by default (if anyone's dumb/stubborn enough to insist on 7 spaces, let them change the setting and deal with the mess. The rest of us have programming to do). On the other hand, converting spaces to tabs in web files (html, css, etc) saves not-insignificant download time (varies of course, but you'd be suprised - this page lost about 10% just on the html alone). – MGOwen Dec 07 '10 at 01:30
  • 72
    Why should I be mandated to your 2 or 4 SPACES of indentation when a TAB can be dynamically set and indicate a single level of indentation? – Jé Queue Dec 10 '10 at 16:49
  • 2 or 4 may depend on your screen size and the type of the code. For usually extremely nested HTML 2 might help keep the width in view. –  Dec 14 '10 at 11:10
  • 3
    @MGOwen The download time argument for website coding is easily resolved nowadays using source minifying on css/javascript and whitespace removal on HTML. See mod_pagespeed to see what I mean. – Evan Plaice Jan 19 '11 at 11:12
  • 42
    "A tab could be a different number of columns depending on your environment" Yes, this is exactly the advantage. Everyone can set it to his taste and be happy. – Andrea Apr 20 '11 at 09:23
  • 1
    A space is a single column, unless you use 2 spaces to represent a column. Or 4. Or 8! – Chris S Jun 17 '11 at 09:28
  • 2
    For languages where whitespace is signficant (F#, python), it has to be spaces anyway. Suits me fine :) – Benjol Sep 29 '11 at 12:55
  • 3
    A space is only one column if you're using a fixed-width font. Thereby you are also stating that you _must_ program with fixed width fonts. – edA-qa mort-ora-y Oct 16 '11 at 12:18
  • 3
    **A tab is always one indentation so it isn't different along environments**, while with spaces you have a variable amount. *Would you rather like a document with 1 space indentations or with 8 space indentations? Oh, seems I sometimes typed some other amount of spaces by accident...* – Tamara Wijsman Nov 11 '11 at 11:55
  • 3
    Sorry, but this answer is terribly wrong! DON'T use spaces. If you share a code with other people and their code indents with spaces and yours indents with tabs, you'll have a terrible problem. Because you use tabs, your editor is configured to use tabs and if you open a spaced-indentation code you'll see a fucking piece of shit. If the code is indented with tabs you can configure the width!! You can set a tab to be 2 columns without the need to hardcode 2 spaces. I hate when the code is not tab-indented. –  Nov 17 '12 at 17:21
117

Use tabs to indent the start of the line, one tab per indent level, and let everyone pick how wide they want that to be.

Use spaces if you're lining up characters within a line, so they always line up regardless of tab size.

And find and punch all the early software authors who let this stupid thing become an issue in the first place.
(Seriously, why is this even something that's discussed? Next you'll be telling me you also want to use multiple characters for line breaks!)

Peter Boughton
  • 4,562
  • 1
  • 30
  • 27
  • 33
    Yes yes yes yes yes. One hundred percent yes. This lets everyone set their own tab width while preserving tabular formatting. If you're worried about keeping within 80 columns (as you probably should be!) then just pick an arbitrary maximum expected tab size (such as 4) and keep within the margins for tabs at that width. Honestly, is this so difficult? – Jon Purdy Sep 24 '10 at 20:23
  • Yes, but don't use spaces to line up within a line. Your colleagues may be using a proportional font so your ASCII art is going to look weird on their screens. – paperjam Dec 02 '12 at 13:45
76

Tabs

  • allow mistakes to be more noticeable, it's annoying to micro manage to spaces.
  • are consistent, they are only used for indentation, unless you are commenting wrong.
  • are customizable, you can specify in the options of the editor how wide they are.
  • are more productive, why type 4 spaces when ye can hit 1 tab.
  • take less size than multiple spaces.
  • take more space than a space.
  • take less time to go through.
  • tend to work better in IDEs.

Spaces should be used where tabs are completely useless.

Even for aligning parameters and comments, tabs still work better.

Tamara Wijsman
  • 8,259
  • 14
  • 58
  • 94
  • 24
    wrt "more productive", personally I hit TAB and let the IDE do the conversion for the appropriate number of space characters to introduce. Perhaps that you need to upgrade :p ? – Matthieu M. Dec 14 '10 at 19:14
  • 3
    -1: More noticeable mistakes: not true; consistent: hardly because of customizable: yes, but it's also disadvantage at the same time; more productive: not true, virtually all ides can convert tabs to spaces; less size: yes, but practical? no; less time: no with reasonable IDE; tend to work better: not at all if you use multiple environments and tools – MaR Nov 08 '11 at 13:30
  • @MaR: Noticable mistakes is true, the width of a tab is larger. Also... Python! Tabs are consistent as a single Tab is an indentation, while you otherwise have to define how many spaces you will use for an indentation. Customization is an advantage. You are using tabs in the first place, so it's more productive. Seems you can't even give a good reasoning why it is impractical, haters gonna hate... ;) – Tamara Wijsman Nov 11 '11 at 11:51
  • @TomWij: How is tab larger than equivalent number of spaces? Python is virtually the only language that got whitespace right to use tabs safely (but still - it offers only little advantage compared to "insert spaces"). Try also other languages, legacy environments, diffs/merges, terminals, sccs to see why tabs are still utopic "ideal". – MaR Nov 11 '11 at 12:37
  • @MaR: Where did I state 'equivalent number of spaces'? I've tried all those, tabs work perfectly in them! – Tamara Wijsman Nov 11 '11 at 13:07
41

All of the arguments for tabs are great in theory. But...

In theory, practice and theory are the same. In practice, they are not.

Yes, with tabs you can decide your indentation level. And yes, you can use a combination of tabs and spaces to align things. And in an ideal world it would

In reality, you can't see the difference between spaces and tabs, they always seem to get mixed up when moving code around, and viewing the code in another program that has tabs set to 8 columns is a nuisance.

I used to use tabs. Then I started to work as part of a team and share code. I quickly became a proponent of spaces. So while I can sympathize with the utopia of tabs, I can't imagine not using spaces.

g .
  • 707
  • 6
  • 12
  • 6
    you can get an editor like Kate which presents tabs with a 'ghost' character - I can easily see tabs vs spaces when coding. – HorusKol May 04 '11 at 06:22
  • 8
    Every editor has a "show whitespace" option now, and Visual Studio has a 'tabify' option – Chris S Jun 17 '11 at 09:31
  • 2
    Good argument. That's the reason I gave up on trying to convince people that tabs are more logical. Even programmers who should be used to abstract thinking seems to confuse tabs and spaces ("if it looks like four spaces, then it must be four spaces!"). The nail in the coffin for me was that it seems to be impossible to configure Emacs to indent only with tabs. – Martin Vilcans Jun 22 '11 at 21:10
  • 3
    +1 The tab utopia is great, but every time that I moved code between editors with different tab widths the code has been messed up and misaligned. It is too easy to silently do the wrong thing with tabs. – Mankarse Nov 08 '11 at 08:27
  • I used to use tabs too, but I was running into trouble with some IDEs that couldn't insert spaces for continuation lines and I hated to replace all those auto-inserted tabs with spaces (Visual Studio was one of them). So I just sticked with spaces. Plus spaces allow you to use half-sized indents for things like `public:` in C++ class declarations so they look prettier (especially with nested classes). – szx Jul 19 '13 at 21:02
31

Some relevant opinions that may be of interest:

Fishtoaster
  • 25,909
  • 15
  • 111
  • 154
  • 105
    Just because you're famous doesn't make you correct. – Jé Queue Dec 10 '10 at 16:52
  • @Xepoch: have you read their rationale :) ? – Matthieu M. Dec 14 '10 at 19:12
  • 5
    @Matthieu M. , yes I have read each, but still don't agree. I don't like olives either but doesn't make me wrong because others do. I find it ludicrous that people wouldn't use the tab for indentation, but this is why they call them CS holy wars... – Jé Queue Dec 14 '10 at 22:56
  • 2
    @Xepoch: yes, I suppose everyone is entitled to one's opinion :) I do think though that Guido has pointed the main issue: mixing tabs and spaces is evil, because both appear as "blank space" to the user. – Matthieu M. Dec 15 '10 at 08:06
  • @Xepoch - "I don't like olives either but doesn't make me wrong because others do" ... well, actually ;) – ocodo Sep 28 '11 at 00:04
  • 3
    @Fishtoaster: When you say that Joel says spaces, shouldn't the link be to an actual Joel quote? The link just goes to the JoelOnSoftware forum where some ppl (most likely not speaking for Joel) discuss tabs vs spaces. – Andreas Magnusson Mar 07 '12 at 11:25
  • @Xepoch Partially agreed, but don't forget, they are famous just because they are **more correct** in the same field :-) – DD_ Jun 07 '13 at 12:25
23

Tabs for the win.

20

I. absolutely. hate. 4. spaces.

Why? Mainly because I'm tired of navigating my code with a keyboard and having to constantly hit left left left left to go over one indentation. This was born out of early versions of Notepad++ and even plain Windows notepad where there was no such thing as a format button. I had so many issues when people would only use 3 when I had used 4 everywhere else among other things.

The other reason is that the tab character exist specifically for indentation, and was only later adopted for navigation. Why are we doing space space space space when a simple tab would work? Why should IDE's have to deal with 2-5 spaced code and format correctly when a simple tab and a preference option would work?

Unfortunately I'm the minority.

TheLQ
  • 13,478
  • 7
  • 55
  • 87
  • 31
    You are inefficient at keyboard code browsing. You should use CTRL+LEFT and CTRL+RIGHT to skip through whitespace and tokens. So indentation wouldn't matter. – Wizard79 Sep 01 '10 at 22:52
  • 7
    Where does this misconception come from that “tabs exist specifically for indentation”? **Tabs are called tabs (tabulators) because they were invented for tabulation, not indentation**, and they do a dreadful job at both. – Timwi Sep 02 '10 at 19:17
  • 1
    @Lorenzo Eww, CTRL. – alternative Sep 09 '10 at 00:21
  • 4
    Get a real editor – JD Frias Sep 09 '10 at 06:08
  • Computers are actually really good at counting the spaces for you. – rhettg Sep 12 '10 at 15:53
  • 7
    @Lorenzo: You are inefficient at keyboard code browsing. You should use `w` and `b` (or `e` and `ge`) to skip through whitespace and tokens :P – R. Martinho Fernandes Sep 15 '10 at 03:45
  • Don't inflict your inferior text editor on everyone who has to read your code please. Use more mature tools and you'll find that the problem vanishes. – Daenyth Sep 24 '10 at 19:30
  • 6
    For the sake of devilish advocacy: don't inflict badly-formatted code on editors that you consider inferior. Use tabs. – Jon Purdy Sep 24 '10 at 20:29
  • 4
    @Timwi, SPACES were invented to put spaces between words, not indentation either. – Jé Queue Dec 10 '10 at 16:55
  • 1
    @Xepoch: So? All I said was that ① tabs were not invented for indentation (I am specifically responding to the claim that they were); and ② they do a dreadful job at it. What spaces were invented for is irrelevant; they do a better job at indentation than tabs regardless. – Timwi Dec 16 '10 at 02:47
  • 3
    @Timwi: you say that for what spaces were invented is irrelevant, yet are you not claiming that one of the reasons tab=bad is because that indentation was not their original design? Anyway, tabs allow ME the programmer to decide how deep their indent and in most legacy editors allow me to block indent. At the end of the day, it really doesn't matter... – Jé Queue Dec 16 '10 at 03:43
  • 11
    @Timwi: sorry to necro, but the computer tab key was NOT invented for tabulation. The TYPEWRITER version of that key was, but by the time keyboards evolved for use with computers, the tab key was used for indenting more than tabulation, and the term "tab" had become synonymous with indent. Tabs for COMPUTERS are to indent, and it is only by convention that the name of the key matches that of the typewriter. Your argument is akin to claiming that the computer return key was intended to start a new line and move the cursor to the far left, because that's what it was for in a typewriter :) – Beofett Apr 12 '11 at 16:17
12

I personally like to use tabs in everything, since each developer can control the amount of indentation per tab. That way you get flexibility in display.

That being said, I usually mimic whatever coding style is in the file to start with (since I spend a lot of time doing maintenance work).

Brandon
  • 660
  • 4
  • 8
  • 3
    That breaks as soon as you start indenting for anything other than statement nesting. For example if you want to align function arguments on the same column, one per line. It's possible by mixing tabs and spaces but very tedious. – finnw Sep 03 '10 at 07:36
  • 2
    It's not tedious at all, since it means you can tab most of the way there and then just use a couple of spaces, rather than having to type dozens of spaces just to get to the right column. – Jordan Reiter Dec 12 '11 at 19:55
  • 1
    @finnw you assume everyone is using a monospaced font - in 2012, significant number of devs use proportional. If you have non-whitespace chars to the left of your first argument, there's no way to guarantee arguments on subsequent lines will align for everyone. – paperjam Dec 02 '12 at 13:53
  • You've never tried writing HAML, have you. – alt Dec 14 '12 at 09:41
  • 2
    I hate to say this, but I can't imagine programming in a variable-width font. The sheer _wrongness_ of it dwarfs brace standards, tabs vs spaces, everything...am I just getting old? (What happens when you get an error message with a line and a column number?) –  Feb 21 '13 at 04:21
10

I do not think that there is such thing as proper indentation (at least not without minor war).

Personally I like four spaces. They enable me to read code much faster and they look same in every editor - even Vi.

Josip Medved
  • 1,118
  • 7
  • 8
  • 9
    vi (or rather vim) is one of the better editors for working with tabs, As well as being able to set the tab width and automatically expand to spaces (like most editors can) you also have the `softtabstop` option and the `retab` and `retab!` commands. – finnw Sep 03 '10 at 07:32
  • Would babies really die if we just said **4 spaces, everywhere, always, no choice** - and moved on? – MGOwen Dec 07 '10 at 01:34
  • 2
    Four spaces do not look the same everywhere. More programmers now are choosing proportional fonts in their IDE for a more pleasant and productive coding experience. Four spaces can look like a pretty tiny indent in these situations. – paperjam Dec 02 '12 at 13:50
8

Spaces, because when you align comments to the right of code, or function parameter lists, or complex multiline expressions, or things of that nature, you want your beautiful work to appear right for everyone. If you use tabs and allow people to set their tabstops differently, they will break alignment for all but the simplest cases of code indenting.

Besides, it's blatantly obvious that everyone in the world should be using vim, which makes it trivial to indent, unindent, and navigate through "tab stops" even in space-indented files.

hobbs
  • 1,292
  • 10
  • 12
  • That is a really good feature of many editors -- the ability to go quickly through mounds and mounds of spaces. – Christian Mann Nov 06 '10 at 08:05
  • 18
    Sounds like you're drawing ASCII art rather than coding. – rmac Nov 06 '10 at 17:21
  • 3
    I would add: Tabs musn't be used for alignment. If you are tab user and want to align you must first type correct amount of tabs for indentation and THEN correct amount of spaces for alignment. Only then you see tab formatted code right across all users and editors. – MaR Nov 15 '11 at 15:30
6

Tabs are the natural and orthodox choice, as they are by definition used for indentation.

Unfortunately, tabs are unevenly implemented, so the only real world solution is 4 spaces.

Wizard79
  • 7,327
  • 2
  • 41
  • 76
  • 8
    Since when are **tabs (tabulators)** used for indentation “by definition”? By their very definition they were invented for **tabulation** (although they do a dreadful job at that too). – Timwi Sep 02 '10 at 19:20
  • 2
    Technically, the return key was invented to move the typewriter head to the front of the line, whereas the move to the next line was performed by rotating the roller. Later, the return key would also automatically advance the roller as well. You don't see people getting all hung up on the return/enter key though. The tab key was clearly added to computers to perform indentation. It's a digital analog of the tab stop, which I had on my old non-electric typewriter. Tab stops could be set to move the roller to any position you chose, almost identical to the tab key's current role in indentation. – Jordan Reiter Dec 12 '11 at 20:00
  • @JordanReiter RE: "You don't see people getting all hung up on the return/enter key though.": Have you never faced CR (represents moving the typewriter head to the front of the line)/LF (represents rotating the roller to the next line) issues? – Max Nanasy Aug 25 '12 at 07:06
  • 1
    Yeah, but that is really an OS issue, not a keyboard issue. No matter what computer you are on, you expect hitting either the enter or the return key to take you to the next link. You don't use one key to issue a line feed and one to issue a carriage return. – Jordan Reiter Aug 27 '12 at 15:07
4

You obviously need a combination approach.

If you're sharing code with other developers, you need to standardize, and since that's impossible (koff koff), you need to make everyone do four spaces.

Then you need an editor that's smart enough not to be stupid about that, to know that it should treat a line with four spaces at the front of it like it's indented. Any modern IDE or programmers' editor can auto-flow code with spaces instead of tabs.

Dan Ray
  • 9,106
  • 3
  • 37
  • 49
4

Why can't someone implement this:

  • code is stored in a compact format beneficial to the system in question
  • as each dev opens the code, it is formatted exactly how they want it
  • when they have finished with it, back to the compact format

Everyone is happy as they all see their 'own' format

Is that so difficult?

adolf garlic
  • 1,061
  • 7
  • 10
  • 2
    Because I don't have enough free time to fix all the existing editors and IDEs that people use, and - even if I did - convincing everyone to upgrade would be even harder. – Peter Boughton Sep 09 '10 at 18:07
  • I'm sure they could build it in to SVN or find some other way of leaving the end tools out of it. – adolf garlic Sep 09 '10 at 18:15
  • 13
    Sounds kind of like tabs. Each developer opens up the file and get the indentation width they want. – rmac Nov 06 '10 at 17:35
  • 1
    You can already set up RCS or CVS to use the `indent` program. – Jé Queue Dec 10 '10 at 16:56
  • 3
    and it's prudent to use indent to force indenting/ code foramtting standarization so that devs with funny spacing ideas don't get *blamed* / *annotated* for all those formatting changes – Tim Williscroft Dec 14 '10 at 02:56
  • Why would they get blamed for anything - each person who opens the file would see the formatting exactly how they wanted it based on their preferences. In the event that they had none, it would resort to using a default. – adolf garlic Dec 15 '10 at 13:27
  • I did implement this, but only for an XSLT IDE. The compact format is the XSLT trimmed of all formatting tabs or spaces. The XSLT is always formatted properly when opened without change because it's rendered with dynamic margins, not characters, for formatting. My answer here has more detail – pgfearo Aug 24 '11 at 17:46
  • You can set Eclipse up to reformat on save. So, you open a file, delete a random bit of whitespace, save, and everything is perfect. – Tom Anderson Feb 28 '12 at 20:46
  • Wish granted! Tabs only for indentation, spaces only for alignment. – wjl Jan 15 '13 at 00:29
3

The answer is that there can be no single proper indentation character for every situation. Formatting using characters is inflexible and can cause conflict when different styles are used within a team.

The only method to format code flawlessly and flexibly with different formatting styles is to do it virtually, that is, without any indentation characters. The only code editor I know that supports this though is the one used in the sample below:

To demonstrate virtual formatting, the screenshot below is from an XSLT editor* that uses this indentation method (there's also a short video here). Every character in the XSLT has been highlighted in yellow, for illustrative purposes, to allow the only tab or space characters in the content to be seen clearly. The code indentation is handled by the editor's rendering system adjusting the left margin (which has a white background).

enter image description here

The only leading space characters precede the Books lines, because this is literal text content, not code, these space characters must be preserved.

With virtual formatting you choose the indentation width to suit the environment and indentation style without affecting any characters in the source file. You can even set the indentation width to 0, if you need a flattened view of the code as shown below:

enter image description here

To contrast this with space character formatting, the same XSLT opened in an editor without virtual formatting is transformed by that editor's auto-formatter to this:

enter image description here

The larger blank yellow blocks in the screenshot above clearly show the space characters added by the formatter of the conventional editor. Unfortunately, these now can't be distinguished from real content so the XSLT would have to be modified to correct this issue.

Summary

XSLT is possibly an extreme case, but this principle holds true for many programming languages: Characters should be used for content and an alternative method sought when it comes to formatting.

**Disclosure: The XSLT Editor with virtual formatting was developed by my own company*

pgfearo
  • 944
  • 4
  • 11
  • 19
  • 1
    Sadly, you're probably about 50 years ahead of your time. The frankly absurd idea that programs are sequences of characters is still very deeply engrained in most programmers' minds. – Tom Anderson Feb 28 '12 at 20:48
3

Not mentioned until now: There are languages (Python, Haskell) where indentation matters. But 1 character counts as 1 character, be it a space or a tab so the indentation seen by the compiler may not be the same one you see on the screen if you use tabs.

Therefore, in languages like Haskell, spaces are a must. In Makefiles, TABS are a must. In all others, it's a matter of personal taste and nowadays not a great deal - every decent editor has a "(leading) tabs to spaces" and "(leading) spaces to tabs" command.

Tim
  • 209
  • 1
  • 2
  • 7
Ingo
  • 3,903
  • 18
  • 23
3

Spaces or Tabs - What Atwood really says is pick one thing and be consistent in your project. The only holy grail of code formatting is to make certain that its consistent so that the psychopath who maintains your code after you doesn't feel compelled to remedy the situation permanently.

That said, if you're working in Python or any other language where whitespace is an actual programming construct I can't imagine using tabs.

Noah Goodrich
  • 1,396
  • 1
  • 12
  • 17
  • 4
    I use tabs. I don't know if you've done any Python programming, but it's relative indentation that matters. If line 1 is x indents in, and line 2 is x + 1 indents in, it's considered more indents. Indents can be double spaces, 4 spaces, tabs, doesn't matter as long as you're consistent. – Macha Sep 08 '10 at 22:07
  • @Macha - Thanks for the feedback. I've never worked doing Python development but my who have have all told me they use spaces so its interesting to meet someone who uses tabs. – Noah Goodrich Sep 09 '10 at 14:26
  • 3
    I use tabs with python at work. It's terrible. – rhettg Sep 12 '10 at 15:55
3

I'm a 4 space kind of guy, tabs just aren't consistent.

Walter
  • 16,158
  • 8
  • 58
  • 95
  • 5
    How are they "not consistent"? They are always a single character representing a single level of indentation. That is the very _definition_ of a consistent indentation scheme. (And that you can personally, without affecting anyone else, visually render that any way you choose is a _plus_.) – Lightness Races in Orbit Mar 02 '12 at 14:30
3

Apparently tabs mess stuff up in Delphi so I don't use tabs in Delphi.

However I do everything else using Emacs and always use tabs because my tabs go exactly where I want them to go.

Peter Turner
  • 6,897
  • 1
  • 33
  • 57
  • Can you provide an example of this? I'm honestly curious because I've been using Delphi for 15 years and have never seen this. – GrandmasterB Sep 24 '10 at 19:32
  • 1
    http://edn.embarcadero.com/article/10280#4.3 That's just the style guide, but we've had some weird errors that I can't explain because of tabs. It may also be due to Visual Source Safe. I can't explain it, it's just part of the lore of our company. – Peter Turner Sep 24 '10 at 20:04
  • 1
    The problem is that the installation defaults for (at least) some versions of Delphi don't match usual Delphi idioms. So you end up with code that has BOTH. – Gerry Sep 27 '10 at 01:24
2

I used to use spaces, but I've been using tabs lately purely because that's what Eclipse was set to when I finally noticed. All of the other developers on my team use Eclipse, so it made sense to standardise on tabs when we realised we'd already been using them for ages and there was no reason to bother changing to spaces. I've been surprised by how much of a non-issue it has been.

Setting the displayed tab size to 3 or 5 characters in your IDE greatly simplifies distinguishing between code sections that are indented by spaces (almost always 4 these days) and those that are indented by tab.

Shabbyrobe
  • 119
  • 3
1

Plenty of arguments have been given already, but nobody mentioned where we could be headed in the future.

Tabs nor spaces!

Ideally code should be considered data, and not be stored in any specific text formatting. Any developer can apply his own preferred view. Furthermore, this view shouldn't be limited to just text, but can include tables, color pickers and mathematical formulas.

This idea isn't too far-fetched. It was JetBrain's Language Oriented Programming editor Meta Programming System (MPS) which first made me realize this resolves the entire discussion, simultaneously adding a lot of extra possibilities. (Yes this is possible with editor plugins, but working on text directly adds so many unnecessary complexities, as opposed to the approach MPS takes.)

As opposed to tabs and spaces, there are little disadvantages which can be mentioned for working directly on abstract syntax trees. All that is needed is for the technology to mature to a commercially viable product. The first signs of this are showing up. Largely built upon MPS, a commercial actionscript editor, Realaxy was created.

I'd love to see one of the big players jump on the concept of this technology and see what happens!

Steven Jeuris
  • 5,804
  • 1
  • 30
  • 52
0

Neither is better, neither is worse. The only important thing is to be consistent.

If you are a team of one, pick whatever you personally like. Consider what the default behavior of your favorite editor, but pick whatever you like.

If you are on a team, do what the team does. Period.

In my various jobs I've used two spaces, four spaces, eight spaces, tabs, spaces-and-tabs, I think I may have also used one space. I tell my editor what to do then I don't think about it again, the editor works out the details.

The only other thing is to make sure you pick a smart editor. Emacs or vi? Now that's a holy war I'm willing to fight :-)

Bryan Oakley
  • 25,192
  • 5
  • 64
  • 89