roman

roman

Using a literal parameter to choose between multiple function heads

Hi,
Imagine we have a code like this in a language that doesn’t support functions with multiple heads:

def parse_date(format, value) do
  case format do
  :iso8601 -> Date.from_iso8601(value)
  :american -> value |> String.split("/") |> …
  end
end

If we always pass the format as an atom literal, i.e. parse_date(:iso8601, value), this is considered an anti-pattern (Flag Argument).

In Elixir, however, this is not so clear, because the implementations are more separated and there’s no explicit branching:

def parse_date(:iso8601, value) do
  Date.from_iso8601(value)
end

def parse_date(:american, value) do
  value |> String.split("/") |> …
end

Do you consider this an anti-pattern in Elixir as well, and we should have differently named functions? Or does this approach fit the general pattern-matching style of Elixir?

Most Liked

LostKobrakai

LostKobrakai

I’ve looked into the arguments of martin fowler:

Tangled Implementation

This can happen, but given in elixir we usually write different function bodies like he does for two different functions I feel this is is not more of an issue then for his proposed solution.

Deriving the flag

Given we’re not in oop land, somewhere some functions needs to decide for which “flag” to use – only then parse_date is called. So most of the discussion is not applicable to elixir. But I’m with martin that boolean flags are bad. I don’t want to see calls like parse_date(true, date) vs. parse_date(false, date) to differenciate :america | :iso8601. Either use atoms describing the flag or if it’s truely boolean use a keyword list: parse_date(value, time_only: true)

al2o3cr

al2o3cr

An initial style note: the first argument position is usually reserved for the “subject” - value here - so that pipes work nicely:

some
|> chain()
|> of_expressions(that: return_a_string_shaped_like_a_date)
|> parse_date(:american)

Whether this is better as |> parse_date(:american) or parse_date_american() depends on usage; is the format always hard-coded or is it driven by data (for instance, from a user preference)?

Another deciding factor might be the implementation: are the two function heads independent, or do they rely on common private functions?

Last Post!

al2o3cr

al2o3cr

Here’s an example of “two heads” with overlapping implementation:

https://github.com/elixir-lang/elixir/blob/7533e56c1baec77c2aa1a4c0ad02730fae464201/lib/elixir/lib/calendar/date.ex#L348-L359

The heads here are matching on an atom (Calendar.ISO) inside the input struct, and the second one depends directly on the first. Making them separate functions would obscure that coupling.

The function called to do the work, Calendar.ISO.date_to_string is another example of this style:

https://github.com/elixir-lang/elixir/blob/7533e56c1baec77c2aa1a4c0ad02730fae464201/lib/elixir/lib/calendar/iso.ex#L899-L911

date_to_string is only responsible for type guards and delegates its work to a helper function. Passing the format as an argument instead of having date_to_string_basic and date_to_string_extended helps here, because the decision about which format to use (in the code calling to_iso8601) is several layers of function calls away from where the code branches (date_to_string_guarded’s two heads).

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
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

We're in Beta

About us Mission Statement