using guards in gen_server

Steve Davis <>
Mon Aug 30 00:06:11 CEST 2010


I think it was Joe A who said something similar to: "crash early,
crash often".
I think it was Robert V who said: "check at the boundary".

Both are excellent insights, and together this means to me: Ensure the
type input is right at the boundary of the system, and let it crash
inside if anything gets into an unexpected state.

- so the right place to check the type would the API function, I
believe.

/s

On Aug 29, 4:20 am, Zvi <> wrote:
> Hi,
>
> When writing gen_server, do you put validation guards in API functions
> or in handle_call / cast or in both ? What's the convention?
>
> For example:
>
> send(Pid) when is_pid(Pid) ->
>    gen_server:call(?SERVER, {send, Pid}).
>
> handle_call({send, Pid}, _From, _State) ->
>    Resp = do_send(Pid),
>    {reply,  Resp, State}.
>
> OR:
>
> send(Pid) ->
>    gen_server:call(?SERVER, {send, Pid}).
>
> handle_call({send, Pid}, _From, _State) when is_pid(Pid) ->
>    Resp = do_send(Pid),
>    {reply,  Resp, State}.
>
> OR:
>
> send(Pid) when is_pid(Pid) ->
>    gen_server:call(?SERVER, {send, Pid}).
>
> handle_call({send, Pid}, _From, _State) when is_pid(Pid) ->
>    Resp = do_send(Pid),
>    {reply,  Resp, State}.
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> Seehttp://www.erlang.org/faq.html
> To unsubscribe; mailto:


More information about the erlang-questions mailing list