
Swift Style: Are you a 4-denter? - ingve
http://ericasadun.com/2016/03/31/swift-style-are-you-a-4-denter/
======
Rockslide
So clearly, there are people who favor 2 and people who prefer 4. Solution:
Indent with tabs, configure your editor, and everyone is happy. I don't get
why people still promote the use of spaces.

~~~
CJefferson
The problem is that when some line inevitable has space-based indent (which is
basically impossible to see), then indentation goes crazy.

Of course, the same thing can happen with spaces, but it generally easier to
fix. You can have a "no tab anywhere" policy, and easily enforce it. You can't
have a "no space anywhere", or even a "no spaces at the start of lines", for
example:

    
    
        while(1) {
            if (a == b && b == c && d == e &&
                f == g && h == i) {
                ....
    

We need to tab-indent the 'if', but do tab+4 spaces the 'f == g'. That's very
hard to automatically check and enforce

~~~
Stratoscope
That's only a problem if you use column alignment, as in your example. But
column alignment isn't necessary in coding. It's quite possible - and easy -
to write perfectly readable and maintainable code using indentation only and
no column alignment.

Defining my terms a bit: By "indentation" I mean whitespace at the beginning
of a line without any dependency on character widths. Extra whitespace in the
middle of a line is "column alignment", and so is whitespace at the beginning
of a line when the number of spaces depends not only on an indentation level,
but also on matching other text in the previous line.

In your example, the "if" line uses indentation only, but the line beginning
with "f == g" uses a mix of indentation (to match the indentation of the "if"
line) and column alignment (the extra spaces to line the "f" up with the "a"
above).

One good way to think about it is to imagine (or actually test) what the code
would look like in a proportional font. If it still looks properly formatted,
you're probably just using indentation. If the formatting gets messed up (e.g.
the "a" and "f" no longer line up), you're using column alignment.

Avoiding column alignment has many benefits. If you ban it from your code, so
many formatting issues go away completely.

I've written about this on HN a few times, so I'll leave the details to one of
my previous posts:

[https://news.ycombinator.com/item?id=10206860](https://news.ycombinator.com/item?id=10206860)

------
Mithaldu
As i get older i opt for 4 spaces over 2 for a simple reason: My eyes are not
getting better and reading 2 spaces is actually _harder_ even though i'm only
a little short-sighted.

------
pklausler
3 spaces seemed to be the norm for block-structured languages (Pascal, PL/I,
Cybil, PLUS, etc.) back in the "card image" days before tabs appeared in C
source files. I think it worked because chains of END would line up in a nice
diagonal.

I got used to 2 spaces in C++ in a big shop overrun with Style Guide
fundamentalists and really long API names, but it's just not enough to convey
logical nesting at a distance.

I've seen code, can't remember where, that used 4 space indentation for loops
and 2 spaces for conditionals, so that indentation depth more accurately
reflected runtime cost.

------
joeblau
I use 4 because that's how Xcode set it up. I used to use 2, but when you're
working in a joint code-base without a linter (almost anything on GitHub) 4
spaces is the standard.

------
mooseburger
4 is more legible. Besides, that's what's recommended in the Python style
guide, and Python is my sweet lover.

[https://www.python.org/dev/peps/pep-0008/#indentation](https://www.python.org/dev/peps/pep-0008/#indentation)

------
kazagistar
I prefer tabs for indentation and spaces for alignment, but sadly, everyone
seems to hate this. Either way, I adapt to the norm/standard of the
language/project in question, like a sensible developer.

~~~
charlesism
If everyone hates it, you've probably made the correct choice (for solo
projects, at least).

With spaces, you not only repeat yourself needlessly, but you also lose the
semantic difference between indentation and token separation (which is why
converting spaces to tabs always leaves you with a few incorrect sections,
whereas tabs to spaces does not).

But, again, it's tech, so the least sensible answer is usually the most
popular. I wonder if it just makes some people feel manlier to write cryptic-
looking camelCase with a crapload of redundant space characters.

~~~
mixedCase
>If everyone hates it, you've probably made the correct choice

That's why I'm a Visual Basic Rockstar Ninja.

~~~
charlesism
Did you read my last sentence? The whole problem is that, despite consciously
knowing better, programmers have a perverse urge, in the depths of their
psyches, to make everything seem as difficult as possible. I can't think of
any other reason why we still use camelCase, for example.

I think most devs prefer spaces to tabs (yes, literally typing in redundant
spaces, while also losing some semantic content) Maybe it's because you always
get consistent spacing that way, but maybe - just maybe - it's because of a
puritanical urge to make programming harder.

Everyone in technology is aware of how important it is to keep things clear
and simple, but deep in the soul, a lot of us just want to be Neo, typing
bizarre hieroglyphs that only we ourselves are clever enough to decipher.

~~~
mixedCase
> I can't think of any other reason why we still use camelCase, for example.

I know you said that on purpose ;)

In any case, I am also a proponent of tabs for indentation, spaces for
alignment, and in fact only recently did I succumb to using spaces for Python
projects, for the sake of consistency with the rest of the ecosystem. I was
merely pointing out how "many people use it, therefore it's probably bad",
just like "many people use it, therefore it must be good" are never good
arguments.

~~~
charlesism
Well, it's never black and white, so, strictly speaking, what I wrote was
wrong. But I still like my exaggerated version. It sounds a lot more dramatic.

------
breuleux
> They found that indentation level had a statistically significant effect on
> comprehension, and optimal indentation fell somewhere between 2 and 4
> spaces.

There's your answer. 3.

------
monocasa
Tabs for indentation, spaces for alignment. Then everyone can setup their
editor for whatever they prefer. Crazy, right?

------
shawkinaw
The only language I sometimes use 2 spaces for is HTML, since you can get so
damn deeply nested. But even then I usually use 4 because it's so much easier
to see the structure.

------
alttab
4 for Java, 2 for Ruby and javscript. Essentially, 4 for anything in an IDE,
and 2 for VIM :)

------
julie1
8 in C if the lead says 8 because in C you want people to limit the cyclotymic
complexity of their code. C is hard to read.

4 in C when I code for fun or the lead says 4.

In fact 4 is a pretty decent visual aid and not to big for my use of 80
car/lines 25lines max per functions/method that I respect religiously and
loosely) since 1998. I think being able to see the whole code without
scrolling on a VT is kind of preparing yourself for the worst case (debuging
in prod with vi/emacs or notepad) where attention is the key point under
stress. Prepare yourself for the worst, the worst may happen.

In python, well, I pray that the lead does not mix tabs and space. Because,
what a beautiful idea to have made indistinguishable characters significant.
That is one of my huge beef with python as well as the inability to control
the scope of variables (hence preventing strictures).

So I have strong opinions, but the lead wins when the lead is a benevolent
dictator that proved to be right very often (like linus, theo, guido...).

The one who lead the project to its success makes the rules and as long as the
project is going a way I like, I follow the guidelines happily.

~~~
d0lph
"Now, some people will claim that having 8-character indentations makes the
code move too far to the right, and makes it hard to read on a 80-character
terminal screen. The answer to that is that if you need more than 3 levels of
indentation, you're screwed anyway, and should fix your program." \--Linus
Torvalds

