Erlang: simple refactoring
- by alexey
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.