threeaccents

threeaccents

I wanted to see how others deal with private and public function defaults conflict. In my context I like to have public functions that take the current_user from the request what ever other arguments are needed with a “matching” private function that only takes the arguments after the public function does some authorization.

here is a quick example of update user

# user context
def update_user(%User{} = current_user, %UpdateUser{} = update_user) do
    with :ok <- authorize(:update_user, current_user, update_user),
        {:ok, updated_user} <- update_user(update_user) do
        # some logic
    end
end

defp update_user(%UpdateUser{} = update_user) do
  # some logic
end

This works great 95% of the time but sometimes certain functions need to have default parameters

def list_company_users(%User{} = current_user, company_id, filters \\ %{}) do
    # some logic
end

defp list_company_users(company_id, filters) do 
  # some logic
end

For situations like this there is a conflict of having a private and a public function
defp list_company_users/2 conflicts with defaults from list_company_users/3

As a hacky workaround I made the arguments of the private function a tuple.

defp list_company_users({company_id, filters}) do 
  # some logic
end

I was curious if anyone else used a similar pattern and what solution they’ve taken when running into this issue.

Showing Posts 1 to 8

sanswork

sanswork

Personally I’d use different names for the private functions. To add to this I know _function is pretty common for private, personally I use function/do_function

def update_user
defp do_update_user

Anything will work as long as you’re consistent though then you don’t have to go check the def.

dimitarvp

dimitarvp

Seconded, I use the do_* private functions notations myself. Mostly because I have no better idea. :smiley:

Aetherus

Aetherus

It seems that do_* has become a convention :grinning_face_with_smiling_eyes:
By the way, I follow this convention, too.

kokolegorille

kokolegorille

There is no way to detect if You call list_company_users/3 without filter, and list_company_users/2

You might add guard clauses…

# Use when is_binary if You use binary id.
def list_company_users(%User{} = current_user, company_id, filters \\ %{}) when is_integer(company_id) do
    # some logic
end

defp list_company_users(company_id, filters) when is_map(filters) do 
  # some logic
end

I also prefer do_* for private functions…

You might also check how to use function’s signature

eksperimental

eksperimental

Personally I would recommend against prefixing them with do_ most of the times.
If you can find a more descriptive name, use it. If you cannot, or it is too cumbersome, use do_ then.
It does not matter if the name of the function is long, it is private anyway.

In Elixir core the use of do_ has been discouraged, and while at the beginning it was a bit annoying once you get used it, it pays off.

In your first example, I would refactor it, by swapping the arguments of the arity-2 function. I hope you can see the benefit of it.

def update_user(%UpdateUser{} = update_user, %User{} = current_user) do
    with :ok <- authorize(:update_user, current_user, update_user),
        {:ok, updated_user} <- update_user(update_user) do
        # some logic
    end
end

defp update_user(%UpdateUser{} = update_user) do
  # some logic
end

As for your second example, I will rename your second function to a more meaningful name.

def list_company_users(%User{} = current_user, company_id, filters \\ %{}) do
    # some logic
end

defp filter_company_users(company_id, filters) do 
  # some logic
end
threeaccents

threeaccents OP

Thank you for all the replies. It does seem the Elixir community has a convention of doing do_* for private functions. I’m going to try it out and see how it feels.

Thank you everyone for the replies!

eksperimental

eksperimental

This is one of my favorite patterns, the _guarded suffix. You do all your guard checks in your public function, and you delegate to a private one and call it recursively if needed, without checking for a guard again and increasing performance.

This is the current implementation for Keyword.update/4

  @spec update(t, key, default :: value, (existing_value :: value -> new_value :: value)) :: t
  def update(keywords, key, default, fun)
      when is_list(keywords) and is_atom(key) and is_function(fun, 1) do
    update_guarded(keywords, key, default, fun)
  end

  defp update_guarded([{key, value} | keywords], key, _default, fun) do
    [{key, fun.(value)} | delete(keywords, key)]
  end

  defp update_guarded([{_, _} = pair | keywords], key, default, fun) do
    [pair | update_guarded(keywords, key, default, fun)]
  end

  defp update_guarded([], key, default, _fun) do
    [{key, default}]
  end
dmitrykleymenov

dmitrykleymenov

Wow, this is really nice and new for me. I’ve used the same concept, but when it is explicitly named, looks much better.

— All posts loaded —

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
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
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
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
psy-q
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; 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