wintermeyer

wintermeyer

I am used to this syntax for a case structure (my question is about the _ -> part):

case {1, 2, 3} do
  {4, 5, 6} ->
    "This clause won't match"
  {1, x, 3} ->
    "This clause will match and bind x to 2 in this clause"
  _ ->
    "This clause would match any value"
end

A new team member introduced this into our project:

case {1, 2, 3} do
  {4, 5, 6} ->
    "This clause won't match"
  {1, x, 3} ->
    "This clause will match and bind x to 2 in this clause"
  _other ->
    "This clause would match any value"
end

So instead of a _ he/she uses _other. I do know that technically this is no different but I wonder what the majority does/thinks. I don’t want to tell that person in a code review that _other “feels” funny in case everybody but me does it. Every time I see it I stumble upon it but I understand why one would use _other.

Showing Posts 1 to 10

zachallaun

zachallaun

In this case, I would use _ because the word “other” adds no additional context or meaning.

I do tend to prefer the named variant in function heads with multiple clauses. For instance:

def some_function(:explicit_match, _right), do: ...
def some_function(_left, :explicit_match), do: ...
def some_function(left, right), do: ...

This can be helpful especially with longer function heads that take, say, 4+ arguments, as multiple _ can get a little hard for me to parse visually.

12
Post #1
preciz

preciz

Actually I did use _other a few times, but now that I think about it I probably just should have used _.

It’s probably part of an inner monologue just like when people abuse the then function because it fits the inner monologue?

sodapopcan

sodapopcan

Just want to +100 this as it’s one of my biggest pet peeves. Like, no %#$@ it’s “other.” When it comes to scanning unfamiliar code, this type of thing is just distracting. If I see _some_var, I think that something useful is being communicated. As already said, _other as a catch-all is not at all useful.

And, technically they aren’t equivalent as _other gets bound whereas _ doesn’t. I’m not advocating that is some sort of meaningful optimization, just sayin’ :upside_down_face:

dimitarvp

dimitarvp

Yeah, I dislike _other as well but many others are valuable i.e. you have a long chain of matching that checks for several types of structs / maps and then ultimately it’s fine if below you have _not_a_map -> ... – and variations of that. A proper unused variable name really helps code readability.

sodapopcan

sodapopcan

Absolutely, that’s what I was trying to convey. Another way to look at it is that matching is basically (albeit not strictly) assignment—you wouldn’t called a variable other (at least I hope you wouldn’t). So long as there is a name that makes sense I name my _s, which is the vast majority of the time.


To take this a bit further, due to Elixir being dynamic I’d vouch for writing OP’s example like this (depending on context):

case tuple do
  {4, 5, 6} ->
    "This clause won't match"
  {1, x, 3} ->
    "This clause will match and bind x to 2 in this clause"
  {_, _, _} ->
    "This clause would match any 3-tuple"
end

and if nil is allowed, add a specific nil clause. This makes it very clear what is expected an errors out quickly if it gets junk.

hauleth

hauleth

With multiple clauses in pattern plain _ is even more important as:

case {1, 2} do
  {_other, _other} -> :doesnt_match
  {_, _} -> :match
end
sodapopcan

sodapopcan

Oh ya, good point!

You’ll at least get a compiler warning if you do that, though.

EDIT: wait, no you won’t since _other, _other binds as I even pointed out earlier :sweat_smile:

axelson

axelson

Scenic Core Team

You actually do get a warning in this case:

> mix compile
    warning: the underscored variable "_other" appears more than once in a match. This means the pattern will only match if all "_other" bind to the same value. If this is the intended behaviour, please remove the leading underscore from the variable name, otherwise give the variables different names
    │
  4 │       {_other, _other} -> :doesnt_match
    │                ~
    │
    └─ lib/compile_test.ex:4:16: CompileTest.my_fun/0
sodapopcan

sodapopcan

Well I should probably stop snap-responding then :upside_down_face:

yukster

yukster

Kinda surprised no one else pointed this out but if you use Credo on your project (and you should) then – unless you’ve suppressed the rule – it will give you a warning for just ‘_’ and tell you to give it a more meaningful name.

Now “other” may not be the most descriptive variable name, but without better context of what sort of thing you are actually matching it will do in a pinch.

The TL;DR though is that Credo is the compendium of idiomatic Elixir choices and a bare underscore for a variable name is not idiomatic.

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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews