amirOrbe

amirOrbe

Hi everyone, I have a function with two parameters with default values something like:

def(param1 \\ %{}, param2 \\ "") do
###
end

this is the best practice for do that? or you know another way ?

Showing Posts 1 to 10

srcoulombe

srcoulombe

You can also use functions from the Keyword module

def foo(opts \\ []) do
    arg1 = Keyword.get(opts, :arg1, "default_value") 
    ...
end
zachdaniel

zachdaniel

Creator of Ash

I’ve learned the hard way that if you ever have more than one optional argument its a smell and you should refactor it to an opts list :smiley:

21
Post #2
amirOrbe

amirOrbe OP

amazing! @srcoulombe and @zachdaniel thanks for you advice :slight_smile:

dimitarvp

dimitarvp

Absolutely never do this. Your colleagues or even your own future self will forget or be in a hurry just once and introduce a bug that would be difficult to track down.

Either only one optional argument and it must 99% of the time be the last one – unless the differences in type will make it completely obvious if something goes wrong – or use a keyword list as others have said. That approach also allows you to easily name stuff f.ex.

do_stuff(repo, changeset, username: "amir", password: "nope", dob: ~D[2000-01-01])
gushonorato

gushonorato

Or, using Keyword.validate!/2, would be better.

thiagomajesk

thiagomajesk

You have no idea how much I agree with this @zachdaniel, and it bites too many people in the butt too frequently. Maybe it should be part of the anti-patterns documentation!?

I’ve been working with Elixir for a long time now, how come I’ve missed this gem!? I surely must have overlooked it. Thanks for sharing!

derek-zhou

derek-zhou

Never say never :slight_smile: . If param2 depends on param1, I don’t see why not.

Independent optional arguments should be in keyword list for sure.

zachdaniel

zachdaniel

Creator of Ash

I don’t see why you’d need multiple default arguments for that? You can detect presence with keyword lists and derive defaults etc.

Keyword.put_new_lazy(opts, :key, fn -> 
  other_value = opts[:other_key]
  some_function_of(other_value)
end)
D4no0

D4no0

I personally have found default arguments to be a good tool for trolling, as the default arguments are not restricted to the last arguments only as in many other languages, hence this is valid code too:

def(param1 \\ %{}, param2 \\ "", param3, param4 \\ []) do
# param3 is not optional
end

:see_no_evil:

garrison

garrison

You know, it really bothers me knowing that KW lists require O(n) iteration over the list to extract the argument. I know full well that it will (almost) never matter in practice and I’m pretty sure it’s actually pushed down to native code, but I wince every time anyway…

Of course, I still use them frequently because named args can’t be beat for readability when you have a lot of them.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
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
JesseHerrick
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
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews