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

RSP87
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
nseaSeb
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
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

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
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews