holandes22

holandes22

Grouping callbacks in GenServer - which style do you use?

I group functions with the same name in a Module

defmodule MyMod do
  
  def foo(1)
  def foo({1, 2})

  def bar()

end

But in a GenServer this can get messy when you have several public interfaces as you get a lot of handle_call or handle_cast definitions dealing with the different public interfaces.

What do you do in this case?
You keep all the callbacks grouped together:

defmodule MyMod do
  use GenServer
  ...
  def handle_cast(:foo, _from, %{})
  def handle_cast(:foo, _from, state)
  def handle_cast(:bar, _from, %{})
  def handle_cast(:bar, _from, state)
  ...
end

or group them by case:

defmodule MyMod do
  use GenServer
  ...
  def handle_cast(:foo, _from, %{})
  def handle_cast(:foo, _from, state)
  
  def handle_cast(:bar, _from, %{})
  def handle_cast(:bar, _from, state)
  ...
end

I prefer the latter when the public API is broad

Most Liked

OvermindDL1

OvermindDL1

I do it ‘by case’ for your example. However I keep one space between functions of the same ‘function’ (or none if they are single line functions), and two spaces between all functions that are not of the same functionality. Just something I’ve been doing since C++ and Pascal 25 years ago. :slight_smile:

holandes22

holandes22

I’m also influenced by my background in Python, where I use one space between methods in a class and two spaces between functions

Where Next?

Popular in Discussions Top

jswny
I would like to better understand what the advantages/disadvantages of umbrella applications are compared to structuring your app as as s...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
axelson
Decided against including more info in the title, but the gist is that Plataformatec sponsored projects will continue with the assets bei...
New
sashaafm
Piggy backing a bit on @dvcrn topic BEAM optimization for functions with static return type?, I’ve been trying to understand in a deeper ...
New
fireproofsocks
This is more of a general question, but I’m wondering how other people in the community think about the pattern matching in function sign...
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New
lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
Crowdhailer
I’ve been hearing much about the new formatter and it’s something I have been keen to try. I find examples buy far the most illuminating...
248 19204 150
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
kostonstyle
Hi all How can I compare haskell with elixir, included tools, webservices, ect. Thanks
New

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
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
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement