aptinio

aptinio

def_layout - deterministic function layout via mix format

I just published def_layout, a formatter plugin that lays out a module’s functions: callbacks first in source order, public functions alphabetical by name and arity, each private function just below its bottom-most caller.

Here’s the before/after that shows the tradeoff, on a GenServer:

# before (hand-ordered)
def start_link    # the entry point, conventionally first

@impl GenServer
def init

@impl GenServer
def handle_call({:get, key}, _from, state) do
  {:reply, fetch(state, key), state}
end

def take
def get
defp fetch
# after
@impl GenServer   # callbacks first, in source order
def init          # (alphabetical would flip these two)

@impl GenServer   # the attribute rides along
def handle_call({:get, key}, _from, state) do
  {:reply, fetch(state, key), state}
end

defp fetch        # private, sinks under its caller

def get           # publics, alphabetical
def start_link
def take

Yes, start_link lands mid-pack, wherever the alphabet drops it. That’s the honest cost of the rule, shown up front. Once you know publics are alphabetical you stop scanning for start_link; you jump to it. (The README FAQ takes this objection head-on - the section is literally titled start_link should be first.”)

The payoff is the private layout: each private function directly under its bottom-most caller means you read a function and its helpers are right there, in call order, instead of scattered across the file or piled at the bottom.

def_layout orders by AST but moves source text by line span, so comments and attached @doc/@spec/@impl ride along with their function. Quokka and Styler order the directive block; def_layout orders the functions below it. They compose - list DefLayout first, so the other plugin formats last. (The README has the full comparison.)

I ran it across fourteen codebases before publishing - Phoenix and LiveView apps, Ash, Absinthe, Plug, Nx, and my own production code. The README’s “Tested on real code” section has the methodology, and the sweep script ships in the repo so you can point it at your own tree. Modules it can’t reorder safely (an Ecto schema, a plug pipeline) are skipped - still formatted, just not laid out - and mix def_layout.skipped lists what was left alone and why.

Works with LSP format-on-save (tested with Expert in Neovim).

Setup is two lines:

# mix.exs
{:def_layout, "~> 0.1.0", only: [:dev, :test], runtime: false}

# .formatter.exs
[
  plugins: [DefLayout]
]

There are no ordering knobs. If you think public function order carries meaning worth maintaining by hand, this isn’t the tool for you. That’s by design. (The FAQ covers that one too.)

Requires Elixir 1.16+.

Hex: def_layout | Hex
Docs: def_layout v0.1.0 — Documentation

Where Next?

Popular in Announcing Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42842 311
New
tmbb
PhoenixWS - Websockets over Phoenix Channels Source code on Github here: https://github.com/tmbb/phoenix_ws Phoenix channels are a great...
New
tfwright
After working on it for a couple of months and using it in production for most of that time, today I’ve released LiveAdmin, a LiveView ba...
New
mikehostetler
I’m excited to announce Jido, a framework providing foundational primitives for building autonomous agent systems in Elixir. While develo...
New
Qqwy
Hello everyone, I wrote a small library today called MapDiff. It returns a map listing the (smallest amount of) changes to get from map...
New
Jskalc
Hi! Today, after a couple weeks of development I’ve released v0.1 of LiveVue. It’s a seamless integration of Vue and Phoenix LiveView, i...
New
kip
Image is an image processing library for Elixir. It is based upon the fabulous vix library that provides a libvips wrapper for Elixir. I...
622 18393 194
New
fuelen
Hey folks! Want to present a toolkit for writing command-line user interfaces. It provides a convenient interface for colorizing text...
New
benlime
LiveMotion enables high performance animations declared on the server and run on the client. As a follow up to my previous thread A libr...
New
New

Other popular topics Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I'm a nov...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement