Si consideri il codice:Erlang: semplice refactoring
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
Ora immaginate che ci sono un sacco di comandi e sono tutti chiamata is_registered in un primo momento. C'è un modo per generalizzare questo comportamento (refactare questo codice)? Intendo dire che non è una buona idea posizionare lo stesso caso in tutti i comandi.