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

JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

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
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement