stratacast

stratacast

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?

Showing Posts 1 to 7

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.

stratacast

stratacast OP

Thank you! You’ve given me some stuff to look into. I’ll do some playing around and report back with what I think is the code design you’re talking about :slight_smile:

But for starters, you’re saying it’d be like:

defmodule MyModule do
  defstruct arg4: "default1", arg5: "default2"

  def testfunct(
    arg1,
    arg2,
    arg3,
    [arg4: "user-value"]
  ) do
  IO.puts("Do something")
end

Then have some sort of control flow to check the optional args and if they aren’t defined use a value from defstruct?

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.

stratacast

stratacast OP

Oh interesting, I haven’t come across that syntax yet. I’ll try that out hopefully tonight or tomorrow when I can return to this

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

stratacast

stratacast OP

Fortunately my optional args are known so in this case the struct will make a lot more sense. The second suggestion you showed me reminds me a lot of what I’ve been doing in Python. And thank you for the link! That really helps too!

a-c-sreedhar-reddy

a-c-sreedhar-reddy

What if there are multiple functions which require different inputs?

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
mohsen
I’m using an Umbrella project for a Phoenix application, and I want to have one Ecto Repo and one PostgreSQL database shared by all apps....
New
subsaharancoder
I’ve followed the Phoenix LiveView file upload code here Uploads — Phoenix LiveView v1.0.0-rc.7 and so far everything works just fine wit...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New

Other Trending Topics Top

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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews