[erlang-questions] Supervision Tree

Bengt Kleberg bengt.kleberg@REDACTED
Thu Nov 29 09:12:58 CET 2012


Greetings,

It is especially for beginners that I have found benefits with
separating the interface and the callbacks into two modules. It helps
them to mentally separate the the two very distinct parts(*) in any
Erlang/OTP behaviour.

Experienced Erlang programmers handle the one module mash-up much
better.


bengt
(*)
1: Interface to server that runs in the callers process
2: Server callbacks that runs in the servers process

On Wed, 2012-11-28 at 09:21 -0600, Garrett Smith wrote:
> On Wed, Nov 28, 2012 at 4:34 AM, Bengt Kleberg
> <bengt.kleberg@REDACTED> wrote:
> > Greetings,
> >
> > First let me mention that I prefer to never mix call back code that runs
> > in a special process (in this case the supervision process) with code
> > used by other processes. So, start_link/0 is better off in another
> > module.
> 
> I would disagree with this in general and especially for someone
> starting out in Erlang. There might be cases where separating the
> client and server implementations into two modules makes sense, but
> it's not typical.
> 
> There are a couple things I like to do to clarify the two very
> distinct layers in a gen_server/service module:
> 
> - Provide separate export attributes: one for the client facing API
> and another for the process callbacks
> 
> - Separate the two types of functions within the module at top and
> bottom and denote them with clear comment banners
> 
> E.g.
> 
> %%% ==============================
> %%% Public API
> %%% ==============================
> 
> ...
> 
> %%% ==============================
> %%% Callbacks
> %%% ==============================
> 
> ...
> 
> Certainly this is a matter of usability, so devs should use whatever
> works best for a particular case. But it's far more common to see a
> single module for this.
> 
> Garrett




More information about the erlang-questions mailing list