pmarreck
Mix formatter for tabs instead of spaces
Go, which I am a massive critic of, got 2 things correct, in my estimation.
The first is gofmt, which actually inspired mix format. But we didn’t borrow enough, it seems, because…
The second is preferring tabs instead of spaces. Why?
You’re going to read this, probably be convinced, but then say “well, it’s too late”. Or you’ll say “but the community”.
Well, guess what. The community is already using mix format. And since mix format is both configurable and has a plugin architecture, if you mix format any code you’ve copied with tabs, it will happily convert it right back to spaces (or whatever format that the plugins and configuration in your project specify). Which means that the only argument that can truly be made here is that whitespace indentation decisions should be made at the project level, not the language level. The fact that the compiler is whitespace-agnostic (because it would be pretty insane not to be… Unless you’re an official formatter, apparently) is proof of this.
To that end, here’s a mix format plugin to make tab indentation project-wide: An Elixir formatting module for `mix format` that converts leading spaces to tabs. · GitHub
So whether it’s 2 spaces per tab, 3 spaces per tab, 4 spaces per tab, 8 spaces per tab, or actually using tabs as they were originally designed since the compiler doesn’t care; vision-impaired people DO care, and the formatter will align it to your project’s requirements anyway… So let’s choose freedom here
Most Liked
hauleth
That is not fully true, as foo:bar is not the same as foo: bar. Elixir is whitespace sensitive language (it just to not use off-side rule).
Tabs weren’t designed for indentation. Tabulation character was designed for tabular data (it is in the name). So some people say “tabs for indentation, spaces for alignment” this is explicitly against design purpose of tab, as tab was exactly for alignment purposes (that is also why tabs are elastic, so you can adjust the tab stop to match your table data).
But going back to the argument, while I agree that for some visually impaired people tabs have more sense than spaces I still prefer spaces over tabs, because tooling around tabs is enormous pain in the arse.
- You want to enforce the maximum line length? Good luck with doing that just in editor when there are elastic tab widths. You can enforce it with formatter, but for me it will be enormously irritating during work if I would use different TS than rest of the team, as it would constantly move stuff around.
- Online tooling is super inconsistent wrt to the support of the tab stops. GitHub only recently (2-3 years ago) gained support for custom tab stops and support for defining tabs stops in the project (via
.editorconfig). Earlier it was defaulting to 8 and you either like it or you can GTFO. Other tools not always provide such flexibility. - Support for multiple tabletops in single project is poor in case of CLI/TUI tools. You want to have different TS for Makefile (8), Rust (4), and Elixir (2) files? Good luck when you will fire
git diffand you will have changes in different files.
Some may say, “well, we can fix these tools to allow that stuff”. Yes, we can, but at the same time we can fix these tools to
- Support displaying leading spaces with different width than the rest of line
- Add support to the editor to support braille reader in a way that will for example start line with
<indentation width><space>to provide a way for the user to know how deeply indented they are without shifting through N characters (no matter what these are) or just simply display the leading spaces differently from other spaces (just like editor in general often do).
So yes, this is a problem, but saying that “spaces are the problem, and the only solution is to move to tabs” is the same as saying “you are the problem, fix yourself”.
hst337
Finally, a topic worth a discussion.
sodapopcan
I don’t disagree with any of these arguments. The braille one is especially compelling. I think there are a couple of things that make this awkward for Elixir, though.
One thing you can’t do with tabs, which the Elixir formatter makes use of, is line up code with odd/random spacing. Take with for example of how multi-head anonymous functions get lined up when reaching the line length. There’s no way for the formatter to do that in a way will reliably look nice across editor configurations.
The other thing is line length. With Elixir being a dynamic language it encourages full names for variables whereas I believe Go encourages single letter variables. When you start having longer variable names, larger indentations are going to make for very long lines. Maybe I’m not thinking through it well enough, but it would require the formatter to figure out how to collapse indentation that isn’t necessarily even (as per the first paragraph) in order to figure out if the line length is all right.
Accessibility concerns are very important but it’s just so hard to please everyone. Different editor tools can help—dimming code outside the current block or highlighting ends differently, for example. I don’t like saying “just rely on your editor” though it applies here since that’s the idea behind using tabs. Editors are also capable of retabbing when opening a file and converting back before saving, though that is far from ideal.







