stratacast

stratacast

Function with many arguments design

Hey everyone!

This is my first time posting on here, and I’m getting started with writing some real Elixir code! I’m not new to programming, but I also wouldn’t call myself a programmer, more of a hobbyist since I do a lot more with systems and security.

Anyhay, I really like to learn coding design when I trying new languages, so when I create a function in a module, I want to do it the right way. In my code I’m writing right now, I am writing some functions in a module that will be accepting many arguments, and the majority of them are going to be optional with default values, or optional with nil and if it’s nil it will get tossed.

With that context, if I have a function with say, 10 arguments, how would you write that? My thought is to have it something like this:

  def testfunc(
        arg1,
        arg2,
        arg3 \\ "default",
        arg4 \\ "default1",
        arg5 \\ nil
      ) do
    IO.puts("Hello")
  end

Is that acceptable in the Elixir world?

Most Liked

jeremyjh

jeremyjh

10 arguments is pretty unwieldy. What you’ll see more commonly is a data structure defined with defstruct that is used in the module. Maybe a couple of constructors, and then functions that accept other args and implement logic to add to the structure, and then functions that consume it.

The other pattern you will see is the options pattern - this is for when you have one-three args that are required and several optional arguments. You define the last argument as a Keyword list. Elixir has syntax sugar so you don’t have to wrap that in a list constructor.

seanmor5

seanmor5

Author of Genetic Algorithms in Elixir

What he’s saying is this:

defmodule MyModule do
   @enforce_keys [:arg1, :arg2, :arg3]
   defstruct :arg1, :arg2, :arg3, arg4: “default”, arg5: “default”

   def testfunc(%__MODULE__{} = params) do
      ...
   end
end

You can then access the arguments in the params variable. Enforce keys requires specific arguments be specified.

al2o3cr

al2o3cr

If your argument list is always used together, the struct approach can be powerful.

If the arguments are more ad-hoc, one approach you’ll see in other languages is named arguments. Elixir doesn’t specifically have named arguments, but you can get close with a keyword list:

def some_func(required_arg_1, required_arg_2, opts \\ []) do
  # extract optional args from opts with Keyword.get(opts, :optional_arg_name, "default value")
end

# call sequence
some_func("foo", "bar")
some_func("foo", "bar", baz: "wat")

if you have a LOT of optional arguments, you might even do something like:

def some_func(required_arg_1, required_arg_2, kw_opts \\ []) do
  opts = Enum.into(kw_opts, %{optional_arg_1: "default", optional_arg_2:: false, ...})
  # can now use opts.optional_arg_1 etc
  # or pattern-match the map
end

I first encountered this technique in this post: Passing in options: Maps vs. Keyword lists - #3 by josevalim

Last Post!

a-c-sreedhar-reddy

a-c-sreedhar-reddy

What if there are multiple functions which require different inputs?

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
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

We're in Beta

About us Mission Statement