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
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
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
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #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).