nhpip

nhpip

So I came from the Erlang world. Recently I got into a debate at work about underscored variables.

Half of us (including me) do:

 def foo([], _byte_count, _file_count, acc), do: acc

 def foo([node | tail], byte_count, acc) do 
  # Some code

 def bar(%{something: var} = _state) do
  # Some code 

The other half does:


 def foo([], _,  _, acc), do: acc

 def foo([node | tail], byte_count, acc) do 
  # Some code

 def bar(%{something: var})do
  # Some code 

What’s the preference?

Showing Posts 1 to 10

Sebb

Sebb

I think (a) is better, but I do (b) because I’m lazy.
As longs it’s consistent (over the codebase - use credo), both are OK I think.

cmo

cmo

I prefer (a) because it saves time.

pdgonzalez872

pdgonzalez872

having done both a and b, now it’s a from now on. b is great, until you come back to the code after a while. @cmo is right, imo.

tomkonidas

tomkonidas

I do a, however sometime go for b when I have a function with multiple heads and have to repeat a lot of the same stuff.
For example:

def handle_event(event, unsigned_params, socket)

def handle_event("validate", %{"some" => value}, socket) do
  ...
end

def handle_event("save", %{"some" => value}, socket) do
  ...
end

def handle_event("refresh", _, socket) do
  ...
end

def handle_event(_, _, socket) do
  ...
end

...
zachallaun

zachallaun

Agreed with @tomkonidas! Generally if I have 3 or more clauses, I’ll use a function head and then refer to skipped arguments with _.

The exception to that, however, might be very commonly-used callbacks like handle_event/handle_info in LiveViews. Basically if I’m implementing a well-known and oft-used behaviour, I’m more okay using _.

If the function is not a callback, though, I definitely want to see written-out params somewhere. It can be very frustrating to read code that looks like:

def do_the_thing(nil, {_, false}, _, _, meta) do
  ...
end

def do_the_thing([], {true, _}, extra_cool_param, _, meta) do
  ...
end

def do_the_thing([item | rest], _, _, fun, _) do
  ...
end

...

I really enjoy reading through library code when I need to better understand something, and patterns like the above are rather common once you get pretty far “into” a library and are dealing with some kind of under-the-hood data transformation/reduction.

sergio

sergio

I do A. I always want to know what the var is, not now but six months from now when I revisit this part of my codebase. Super important to me

joey_the_snake

joey_the_snake

One argument for (b) is that it should be clear what it does from the function heads that are using it. And for the ones that aren’t using it you don’t care what it is. If you are trying understand the function as a whole there are function heads, docs and type specs.

tfwright

tfwright

Seconding this. At best naming an unused variable is a tradeoff. Cost of noise/cognitive load for benefit of reference, but as mentioned there are better places to use as reference. And as was also mentioned, either way you will want to be consistent, and consistently naming all unused vars ends up being a lot.

I think if a function is hard to read because it has a lot of unused variables, that could possibly be considered a code smell. And if refactoring really isn’t possible, function docs are the proper way to justify the tech debt.

dimitarvp

dimitarvp

I always use (a) because as others already said at one point you revisit the code and are wondering what the hell is the ignored variable. Happens much more often than our present lazy self wants to believe. If I don’t feel like using (a) then I stand up from the computer – obviously I shouldn’t be coding in that mind state!

That being said, (b) is useful when you need a catch-all clause but only if you already exhaustively covered all other cases.

sodapopcan

sodapopcan

Always a) except in pattern catch-alls where there is no obvious name. But even then I should still do a). Just don’t call it _other .

Where Next? Top

Trending in Discussions Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
jtormey
Lately I’ve been thinking about how to organize components as a LiveView application grows. One of the pain points I’ve found (for myself...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews