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?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Hello
I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley:
Mutonex gamedesign was finish...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #elixirconf-us
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #security
- #elixirconf-eu
- #api
- #forms
- #hex










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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
An initial style note: the first argument position is usually reserved for the “subject” -
valuehere - so that pipes work nicely:Whether this is better as
|> parse_date(:american)orparse_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
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
That’s a very good point. I think it alone is enough to avoid this style.
The format is always hard-coded, that’s a key part of the question.
Could you expand on that? I didn’t see it as a relevant factor.
roman
Do you mean that we can put the names in a list
[:iso8601, :american]and generate tests iteratively?LostKobrakai
I’ve looked into the arguments of martin fowler:
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.
Given we’re not in oop land, somewhere some functions needs to decide for which “flag” to use – only then
parse_dateis 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 likeparse_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
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_stringis another example of this style:https://github.com/elixir-lang/elixir/blob/7533e56c1baec77c2aa1a4c0ad02730fae464201/lib/elixir/lib/calendar/iso.ex#L899-L911
date_to_stringis only responsible for type guards and delegates its work to a helper function. Passing the format as an argument instead of havingdate_to_string_basicanddate_to_string_extendedhelps here, because the decision about which format to use (in the code callingto_iso8601) is several layers of function calls away from where the code branches (date_to_string_guarded’s two heads).