roman

roman

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?

Showing Posts 1 to 7

rcavanaugh

rcavanaugh

I view that as kinda similar to the way Objective-C makes it’s named arguments part of the method name.

NSString initWithData:encoding:

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?

zkessin

zkessin

Normally you would put the value first, but I don’t think that is in any way an anti pattern. It also makes testing easier.

roman

roman OP

the first argument position is usually reserved for the “subject” - value here - so that pipes work nicely

That’s a very good point. I think it alone is enough to avoid this style.

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)?

The format is always hard-coded, that’s a key part of the question.

are the two function heads independent, or do they rely on common private functions?

Could you expand on that? I didn’t see it as a relevant factor.

roman

roman OP

Do you mean that we can put the names in a list [:iso8601, :american] and generate tests iteratively?

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

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).

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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