Erlang: simple refactoring

Posted by alexey on Stack Overflow See other posts from Stack Overflow or by alexey
Published on 2010-05-20T10:43:52Z Indexed on 2010/05/20 10:50 UTC
Read the original article Hit count: 161

Filed under:
|

Consider the code:

f(command1, UserId) ->
    case is_registered(UserId) of
        true ->
            %% do command1
            ok;
        false ->
            not_registered
    end;

f(command2, UserId) ->
    case is_registered(UserId) of
        true ->
            %% do command2
            ok;
        false ->
            not_registered
    end.

is_registered(UserId) ->
    %% some checks

Now imagine that there are a lot of commands and they are all call is_registered at first. Is there any way to generalize this behavior (refactor this code)? I mean that it's not a good idea to place the same case in all the commands.

© Stack Overflow or respective owner

Related posts about erlang

Related posts about refactoring