elt547

elt547

Obsession with line length and hideous formatting (am I writing elixir wrong?)

What do y’all think about the defaults of the mix formatter? I often find long lines being being turned into multiple breaks with weird indentation. The output is so often hideous and completely unreadable.

This happens almost exclusively in long function definitions with pattern matching. Since pattern matching function parameters is a core part of elixir and incidentally very wordy, it doesn’t make sense for formatters to make such a fuss over line length. It’s making me doubt whether I’m writing elixir in the intended manner.

Look at the example below. It’s almost impossible to visually separate the function definition from the first line of the function.

This version is objectively easier to read, but apparently wrong according to the flagship formatter. Is 103 columns really that sinful for a function definition?

Most Liked

LostKobrakai

LostKobrakai

To me one of the big benefits of the formatter is that it has so few options. A good chunk of elixir dependencies uses the formatter, so there’s no difference between looking at code I wrote or some third party code. Yes, to get to “one way of formatting” one has to make subjective decisions once, but when they’re taken there’s no need to discuss them further. There are some cases where the formatter doesn’t produce optimal code, but many times I’ve found ways to make it work. Given those are the 1% I’m happy to make those changes to get the benefits for the other 99%.

baldwindavid

baldwindavid

Think you mean subjective :slight_smile: I actually find the formatted version easier to read. It’s easier for me to scan vertically than horizontally and I don’t like long lines. I tend to think one can get used to just about any formatting if you look at it enough, but also agree with others that too many matches in a function head can be hard to parse.

sorentwo

sorentwo

Oban Core Team

Personally, 9 out of 10 times I see a function clause that spans multiple lines as a code smell. Ask yourself:

  • Do you need all of that pattern matching in the function head?
  • Can you match some in the body instead?
  • Can you write a private guard to reduce matching or guard clauses?
  • Can you use a case within a function (or all compiles down to a case anyhow)?

Where Next?

Popular in Discussions Top

matthias_toepp
I’d love to hear what people think about Wisp, the new Gleam web framework started by Gleam’s primary creator Louis Pilfold. Gleam, alon...
New
PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
WildYorkies
It seems that the more I read, the more I find Elixir users speaking about all the ways that Elixir is not good for x, y, and z use cases...
New
AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
AstonJ
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
New
100phlecs
Are there any downsides, like perf issues, to putting all functional components in CoreComponents as long as you prefix it with the conte...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement