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

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
_mfierro
Hello, I wrote Stop My Hand, a Scattergories-like web application using Phoenix/LiveView as my learning project for Elixir (after readin...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
durvia
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes. We’re a small ...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews