[erlang-questions] Versioned variable names

Per Melin per.melin@REDACTED
Wed Jun 10 00:30:05 CEST 2009


Attila Rajmund Nohl:
>    % Take away underscore and replace with hyphen
>    MO1 = re:replace(MO, "_", "-", [{return, list}, global]),
>    MO2 = toupper(MO1),
>    % Replace zeros
>    MO3 = re:replace(MO2, "RX0", "RXO", [{return, list}, global]),
>    % Insert hyphen if missing
>    MO5 = case re:run(MO3, "-", [{capture, none}]) of
>              nomatch ->
>                  insert_hyphen(MO3);
>              match ->
>                  MO3
>          end,

On a bad day I would write this as below. But I admit that I have some
sort of compulsive disorder where I can't stop refactoring my code
until I average somewhere below three lines of code per function.

sanitize(MO) ->
    ensure_hyphen(replace_zeroes(replace_underscores(toupper(MO)))).

replace_underscores(MO) ->
    re:replace(MO, "_", "-", [{return, list}, global]).

replace_zeroes(MO) ->
    re:replace(MO, "RX0", "RXO", [{return, list}, global]).

ensure_hyphen(MO) ->
    case re:run(MO, "-", [{capture, none}]) of
        nomatch -> insert_hyphen(MO);
        match   -> MO
    end.


More information about the erlang-questions mailing list