wintermeyer

wintermeyer

What do you think about using `_` vs `_other`?

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.

Most Liked

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
bjorng

bjorng

Erlang Core Team

Just to make it perfectly clear, this is not an optimization, not even a slight one. The Erlang compiler can see when a variable is unused and will emit the same code for matching _ as when matching _other or some other variable that is never used. Actually, to avoid having to handle _ as a special case, the Erlang compiler replaces _ with a freshly created variable that will never be used.

sodapopcan

sodapopcan

Citation needed!

Credo is for enforcing per-project standards, not general community standards, and can be customized (which is what I do).

Also, check the root module module of Phoenix:

https://github.com/phoenixframework/phoenix/blob/7d5b954/lib/phoenix.ex#L12-L13

Last Post!

sodapopcan

sodapopcan

This is all fair. I’m all for code consistency and I do use Credo and I was also a RuboCop user for a long time. I think adjustments for team preferences are totally fine so long as you have a process for introducing new rules (as you mentioned). The Elixir community seems to be more accepting of different styles, though, so for me I just consider the formatter law (though many people don’t use it). My problem with linters is exactly what you were saying in your first post: it doesn’t leave room for “it depends” situations where breaking the rules in certain cases makes sense. Of course it would be fairly trivial to write a Credo rule to allow _ for case catch-alls. Same for side-effect throw-aways which I would be extra annoyed to have to name. I actually really like this pattern to mark a call as effectful. It would be super annoying to have _throwaway = fetch() or _effect = fetch() everywhere. Like very quickly you’re going to be like, “Uh, ya, duh!”

But yes, I’m getting very bikesheddy here :slight_smile: I was on this really amazing team and anything low-stakes like this that came up was decided in minutes or less, so like, I gotta bikeshed somewhere :smiley:

Where Next?

Popular in Discussions Top

PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New
ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
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
cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
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

We're in Beta

About us Mission Statement