Hacker News new | ask | show | jobs
by Aeolun 1319 days ago
Why would you ever mix them?! Even when I started programming 15 years ago it was already accepted wisdom that that was a terrible idea.
2 comments

This is one of those things that started out as good advice, and then got turned in to an oversimplified parody of the original argument.

If you have something like:

  var (
    x     = foo
    other = bar
  )
Then clearly you should use spaces to align those "="s, no matter if you use spaces or tabs for indentation. Similarly, "hanging indents" like:

  coolFun(arg1,
          arg2)
Can only be done correctly with spaces, and it doesn't really matter if you use tabs or spaces for indentation; this will still align correct no matter the tabstop size:

  ->coolFun(arg1,
  ->        arg2)

  --->coolFun(arg1,
  --->        arg2)

  ------->coolFun(arg1,
  ------->        arg2)
If you had used tabs, changing the tab size would make it align all wrong.

The problem is when you start doing stuff like:

  if (one) {
  ------>if (two)
             bar();
  ------>else
             xxx(); 
  }
And then, depending on the tabstop size, things can start looking very misaligned and confusing; this is why Python 3 forbids mixing tabs and spaces in the same function, because it's so easy to make something appear wrong and there are no braces to clarify things.

That's what people mean with "don't mix tabs and spaces", not "if you use tabs then the space shall never appear in the file under any condition".

> If you had used tabs, changing the tab size would make it align all wrong.

This is clearly false. You said this after giving a perfect example of how it's done with tabs.

So you are saying that using a tabstop of eight to align this:

  -------->coolFun(arg1,
  -------->------->arg2)
Would still look nicely aligned with a tabstop of 4? and 2? Clearly that will not look right.
To understand tabstops properly, you need to throw away the "They always expand to N spaces." idea. If you have access to a mechanical typewriter with tabstops, then go and look at how it works (with tabs being set by protruding pins on the carriage). Tabstops in terminals in the Unix and Linux worlds work this way.

https://superuser.com/questions/710019/why-there-are-11-tabs...

If you use it as indention then "tab expands to n spaces" is actually accurate though.
You're misquoting yourself.
I don't know what your point is then, but this is turning in to a very trite conversation.
> > If you had used tabs, changing the tab size would make it align all wrong.

> This is clearly false. You said this after giving a perfect example of how it's done with tabs.

I believe the post you're replying to meant "used more tabs". That is, if you used any tabs beyond the code indentation, they would get altered when you change the indentation size and ruin the alignment.

This was indeed the misunderstanding.

When I made my original comment I thought what I was replying to was an example of "tabs to indent and spaces to align" immediately followed by a statement that the only way to use tabs is to both indent and align.

The basic idea is that you use tabs to align "blocks" (ie, multiple lines at the same indent level), and then spaces from there to align line "elements")

Aligning lines in the same block

    ____var a; // indented with a tab
    ____var b; // indented with a tab
Aligning elements of a line with the previous line

    ____var a, // indented with a tab
    ____----b, // indented with a tab, then aligned using spaces
The idea being that tabs are used where it makes sense that you could change them for preference and not have things look wonky... and then spaces are used in situations where a specific number of characters is necessary.

Another alternative is elastic tabs, where tabs are used for both of those, but are converted to an indentation/alignment number of characters semantically. I like the idea, but I've yet to see a good implementation.

Personally I'm a fan of all spaces, but that's mostly because every company I've worked at has used that.