wiki/tabsversusspaces

Tabs versus spaces

https://twitter.com/jonathoda/status/737824970483617793
Had to admit to wife that tabs vs. spaces is really a thing. So embarrassing


Alignment

Spaces are great for aligning things, unless you switch to a proportional font in which case they are completely hopeless.

Tabs don't (necessarily) break when used with proportional fonts. However custom tab values do break alignment
(system defaults of 4 count as custom).

You can't align things longer than 7 characters, which is doubly bad when you take into account that aligning text into columns becomes more desirable the more variable the sizes of their would-be ''cells'' are. So tabs dissallow for alignment when it's most useful.

The tab key is also useless for alignment of non-zero-padded numbers, since they are a special case in that they must be aligned in a ''flush right'' manner.


Both
Tabs for indent and spaces for alignment

Yeah, no. You can't mix and match like that. You WILL get them mixed up.

Gary Bernhardt @garybernhardt wrote
https://twitter.com/garybernhardt/status/906335670800494592
"If you just use tabs for indent and spaces for alignment it'll be fine!"
"If you just free your mallocs it'll be fine!"
Literally the same!
+
Someone replied
https://twitter.com/EricChlebek/status/906336140264751104
It's fine if you use gofmt ✨

Ah, yes, the Gofmt Argument. Automating whitespace does, in fact, nullify the wrongness of the both approach, but it also nullifies the entire point of a debate about whitespace, so it's kind of invalid.


Consistency versus customizability

https://twitter.com/ibc_tw/status/776052508007555076
To be clear: all those using spaces instead of tabs for indentation are irredeemably wrong.
Let me see your code in the way I like.
Period.

https://twitter.com/andy_kelley/status/801140562946506752
Hard tabs cause source files to depend on metadata to be displayed correctly.
+
https://twitter.com/Wallbraker/status/801175009137786881
Not if you use tabs for indentation and spaces for format.
+
https://twitter.com/andy_kelley/status/801230081607794688
if you use a tab for indentation and then some spaces to format, how many spaces should the tab be? that's the metadata.
+
https://twitter.com/Wallbraker/status/801344330157977600
It doesn't matter, it might look different but it will still look correct. And everybody gets their preferred tab size.
+
https://twitter.com/andy_kelley/status/801463433384394753
that assumes 1) no intended alignment between differently indented lines and 2) no column limit

Some people think 8 columns tabs are too long and customize tab character size in their {editors,terminals,whatevers}. This is wrong because it makes alinment that uses tabs break. You can get around this by making the ''tabs for indent and spaces for alignment'' argument, but it is wrong, which makes this also wrong.

However, there's an argument that tabs are better, because just like they can be customized to be shorter, they can be customized to be longer, which may be desirable if you have bad eyesight.

https://cmpwn.com/@sir/104615166548400832
@neauoire the other reason which should immediately shut down the discussion is that tabs are more accessible. If you have a vision impairment then tabs allow you to customize the display of the code to read more comfortably.

Is this really a thing though? I have _shit_ eyesight and I just took a look at some Go code with my glasses off: I can't read a thing but, indent-wise, the structure of the code is pretty clear to me. Like, I can't imagine a context in which 8 columns isn't enough indent but you can still read characters.

Maybe I'm wrong though. Do you (or does someone you know) set your tabs to be longer than 8 columns? Please, let me know!


Elastic tabstops

Elastic Tabstops are an attempt to make the tab character actually good at alignment.

http://nickgravgaard.com/elastic-tabstops/
Rather than saying that a tab character places the text that follows it at the next Nth column, we should say that a tab character is a delimiter between table cells in a manner more reminiscent of how they're used in tab separated value (TSV) files. When used for text or code I call this format "Tab Separated Columns". Seen in this light, we can see that space aligned files are analogous to the old fixed width data files, and we all know the advantages that delimited files have over those. For one thing, you can use sed or other tools to substitute strings in files and everything will still line up when you load them in the editor. Another advantage is that proportional fonts can now be used (in itself not a new idea - see Smalltalk, Oberon and Plan 9's Acme).

If we'd made computers work this way from the start we'd live in a tabs-for-everything utopia. It would also be really useful outside of a programming context: hassle-free, spreadsheet-quality alignment at every text input.

https://twitter.com/BatmanAoD/status/1154093004870676480
If I had a time machine I'd make sure the definition of the "tab" character in ASCII required the use of elastic tabstops

Sadly it is hopelessly unworkable for backwards compatibility reasons. Unless everyone adopts it at the same time code will look broken for whoever doesn't adopt it.


File size

Tabs lead to smaller file sizes than spaces. It escapes me why anyone thinks this is even vaguely relevant among so many way more meaningful arguments from both sides. Maybe no one really does, maybe it's an illusion created by people typing it to grow their pros&cons lists.


Freedom versus safety

Spaces allow for wrong indentation, where bad indentation means mistakenly putting Nٍ±1 space indents in an N space codebase, or something of the sort. You get the idea. Wrong indentation is still possible with tabs --if a space slips through-- but likely way less common.

TODO: Tabs don't allow for arbitrary indentation, like in lisps.

Okay, so let's make tabs the global default and have lisps still use spaces. Yeah, no. This doesn't necesarily rule out tabs altogether, but at least for holy war purposes, There Can Be Only One.


Invisible characters are special

/media/tabsversusspaceshotlinebling.jpeg
https://twitter.com/sysarmy/status/1214517062749609989 credit

https://twitter.com/eevee/status/1258176886422691841
...
"here is how to correctly mix these invisible characters, a thing space people literally never have to try to do, it's in the name"
+
https://twitter.com/eevee/status/1258180312422440960
look it's not hard. tabs for indentation, spaces for alignment, form feeds between function arguments, soft hyphens between array elements, paragraph separators between methods. i love to encode metadata with invisible characters instead of doing programming

Andrew Kelley @andy_kelley wrote
https://twitter.com/andy_kelley/status/875934630926528512
talking about this video
https://www.youtube.com/watch?v=En8Ubs2k1O8
tabs v spaces: "it's not about which one is better. It's about which one can we get rid of. And we can't get rid of spaces." A+
+
TODO: replace with quote from video.


Semanticness

Tabs ''mean indentation'' in a way that spaces don't.

http://xahlee.info/UnixResource_dir/writ/tabs_vs_spaces.html
the semantics of tab is what indenting is about, while, using spaces to align code is a hack.

https://twitter.com/nikitonsky/status/497668345723101187
Tabs were invented for tabular data. Source code is not tabular, so check and mate, go indent with spaces.

https://twitter.com/sebmck/status/1261831532496384001
Honestly if you find the accessibility argument weak, and many do, consider that the tab character is literally for indentation. "Hard tabs" is such a bad term, it should just be "tabs". "Soft tabs" should be called "spaces pretending to be a tab".

Gary Bernhardt @garybernhardt wrote
https://twitter.com/garybernhardt/status/218026131725623296
"The accidental lingering aftereffects of typewriters' mechanical limitations are integral to the experience of programming."
— Tabs.

TODO:
Who is correct here?
Was the tab key invented before the invention of indenting code?


***


Break glass in case of spaces

How many? The hill I will die on is that 2 spaces is indisputably nowhere near enough indent to easily scan the structure of code. 4 is workable, but still not ideal. 8 happens to be the sweet spot.

Regardless, make it easy for developers to comply with your projects tab length.


Break glass in case of tabs

Don't use them for alignment, and configure your editor to help you spot mixed up invisible characters. On vim my current setting is
        set list listchars=tab:▒░,trail:▕
which makes code look like this:
        for {
        ▒░░░░░░░if done {
        ▒░░░░░░░▒░░░░░░░break
        ▒░░░░░░░}
        }
but with much dimmer tabs, because of my colorscheme.


***

https://gitlab.com/BartG95/dot/-/blob/c79d086a6b43dde214efb8352ac20390bce65a0b/editorconfig/.editorconfig#L99
https://softwareengineering.stackexchange.com/questions/57/tabs-versus-spaces-what-is-the-proper-indentation-character-for-everything-in-e
https://www.jwz.org/doc/tabs-vs-spaces.html