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

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement