pmarreck

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

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.

  1. 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.
  2. 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.
  3. 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 diff and 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”.

10
Post #9
hst337

hst337

Finally, a topic worth a discussion.

sodapopcan

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.

Where Next?

Popular in Announcing Top

tmbb
I’ve published the first version of my Makeup library. It’s a syntax highlighter for Elixir in the spirit of Pygments, Currently it highl...
New
josevalim
Hi everyone, We would like to announce that Plataformatec is working on a new MySQL driver called MyXQL. Our goal is to eventually integ...
New
devonestes
Introducing assertions, the library that helps you write really great test assertions! GitHub: https://github.com/devonestes/assertions ...
New
gabrielpoca
Hello everyone! I want to share with you something that I’m really proud of: https://stillstatic.io/ Still is a static site builder for...
New
mspanc
I am pleased to announce an initial release of the Membrane Framework - an Elixir-based framework with special focus on processing multim...
New
tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
oltarasenko
Dear Elixir community, After a year of development, bug fixes, and improvements, we are proudly ready to share the release of Crawly 0.1...
New
ahamez
Hi everyone, I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
markmark206
simple_feature_flags is a tiny package that lets you turn features on or off based on which environment (e.g. localhost, staging, product...
New
wfgilman
I’ve cleaned up and open sourced three financial libraries I was using for my company. They are bindings for the APIs of these three comp...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29305 241
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52238 488
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New

We're in Beta

About us Mission Statement