[erlang-questions] The If expression

David N Murray <>
Thu Apr 22 01:48:13 CEST 2010


On Apr 22, Richard O'Keefe scribed:

>
>
> The best thing to do is to show us some code and invite us to refactor
> it.
>

I'm coming from an imperative background and constantly run into one-armed
ifs, or more usually, one-armed cases:

init([]) ->
    case cfg:get(ibr_connect) of
        ok -> gen_server:cast(self(), connect);
        _ -> ok
    end,
    {ok, {init, [], []}}. % State, MsgId, Buf

When this gen_server is started, I have other housekeeping to do before it
can actually start doing it's thing, and the supervisor casts a connect
message when it's ok to connect.  When I process the connect in this
server, I store {ibr_connect, ok} in the cfg module.  This is my
breadcrumb that I've been here before and everything is good to go.  If
the gen_server gets restarted, I want it to connect right away and not
wait for someone to tell it to (automatic reconnect logic, the module is a
tcp client to another server).

Usually, my tests are "has this happened, or is this a that" and I really
don't care about the "false" side of that test.  I'm having a hard time
getting my head around this.

TIA,
Dave



More information about the erlang-questions mailing list