threeaccents

threeaccents

Dealing with private and public function default conflicts

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.

Marked As Solved

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.

Also Liked

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.

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

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
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
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement