vinibrl

vinibrl

Case with multiple values

I refactored a code from:

def do_something(attribute) do
  bar = get_bar(attribute)
  baz = get_baz(bar)

  do_something_else(attribute, bar, baz)
end

defp do_something_else(_first, _second, 12381), do: {:ok, :ignored}
defp do_something_else(_first, second, _third) when length(second) > 100, do: {:ok, :ignored}
defp do_something_else(first, second, third), do: ExternalService.call(first, second, third)

to:

def do_something(attribute) do
  bar = get_bar(attribute)
  baz = get_baz(bar)

  case {attribute, bar, baz} do
    {_first, _second, 12381} -> {:ok, :ignored}
    {_first, second, _third} when length(second) > 100 -> {:ok, :ignored}
    {first, second, third} -> ExternalService.call(first, second, third)
  end
end

I replaced the private functions with a case. AFAIK I can’t use multiple values on the case clause, I wrapped them in a tuple. Is this the idiomatic way to do it?

Marked As Solved

yurko

yurko

I actually like the original version with private functions better.

Your context module will grow too big no matter how you organize its logic. I’d suggest to only have delegates and documentation in context module and delegate to multiple specific “private” modules that have actual logic, if you do that, then the size of each such module will not be a problem.

Also Liked

hauleth

hauleth

This is one of the ways to do so, yes. However in case of case you do not need to pass first, so you could do:

case {bar, baz} do
  {_, 12381} -> {:ok, :ignored}
  {list, _} when length(list) -> {:ok, :ignored}
  _ -> ExternalService.call(attributes, bar, baz)
end

Or you could squash the two cases:

case {bar, baz} do
  {list, num} when num == 12381 or length(list) > 100 -> {:ok, :ignored}
  _ -> ExternalService.call(attributes, bar, baz)
end

However in that case whole pattern matching is needless, so we can do:

cond do
  baz == 12381 or length(bar) > 100 -> {:ok, :ignored}
  true -> ExternalService.call(attributes, bar, baz)
end

Or you could use if macro if it is clearer for you:

if baz == 12381 or length(bar) > 100,
  do: {:ok, :ignored},
  else: ExternalService.call(attributes, bar, baz)

EDIT:

@tushar as length(baz) is used only once then it is much better to not cache it, as if baz == 12381 the length(baz) will not be called at all which can save time, as counting length of the list can be needlessly expensive for long lists.

dmkit

dmkit

But is it ideal to convert it from multi funs to case block / cond block? Or it’s just a matter of preference?

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Other popular topics Top

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement