amirOrbe

amirOrbe

Default arguments best practices?

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 ?

Most Liked

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 #3
srcoulombe

srcoulombe

You can also use functions from the Keyword module

def foo(opts \\ []) do
    arg1 = Keyword.get(opts, :arg1, "default_value") 
    ...
end
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])

Last Post!

dimitarvp

dimitarvp

Oh, I am not against keyword lists as options, of course. That’s one of the gems in Elixir: it allows you to emulate named function arguments and it is super convenient!

My objection is towards more than one optional function argument (i.e. 2 or more having default values). That becomes messy super quickly.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
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

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement