[erlang-questions] Versioned variable names

Richard O'Keefe ok@REDACTED
Wed Jun 10 05:37:28 CEST 2009


On 10 Jun 2009, at 3:12 am, Attila Rajmund Nohl wrote:

> Hello!
>
> I think there wasn't any grumbling this month about the immutable
> local variables in Erlang, so here's real world code I've found just
> today:
>
>    % 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,
>
> I think it's fairly clumsy to use MOx (MO for managed object) ...

I suspect that the thing you are processing is not in fact a
managed object but the _name_ of a managed object.

I find the repeated [{return, list}, global] to be more of a
problem.  So let's do

     replace_all(Target, Replacement, Source) ->
	re:replace(Source, Target, Replacement, [{return,list}, global]).

...
     MON1 = replace_all("RX0", "RXO",
	       replace_all("_", "-",
	           toupper(MON0))),
     MON2 = case lists:member($-, MON1)
	     of true  -> MON1
               ; false -> insert_hyphen(MON1)
	   end,

I'd also look at defining insert_hyphen in such a way that it
doesn't change the string when there's one already.  The whole
thing would then become

     MON1 = ensure_hyphen(
              replace_all("RX0", "RXO",
                  replace_all("-", "_",
                      toupper(MON0))))

The problem's not the variable names, it's the functions.



More information about the erlang-questions mailing list