bug or feature?

Richard A. O'Keefe ok@REDACTED
Wed Apr 19 03:51:01 CEST 2006


Serge Aleynikov <serge@REDACTED> wrote that

	strip_prefix(Prefix ++ Suffix, Prefix) ->
	    Suffix;
	strip_prefix(String, _Prefix) ->		% fixed
	    String.
	
would be "much easier to read" than

	strip_prefix(String, Prefix) ->
	    strip_prefix2(String, Prefix, String).

	strip_prefix2(Suffix, [], _String) ->
	    Suffix;
	strip_prefix2([C|Tail], [C|PrefTail], String) ->
	    strip_prefix2(Tail, PrefTail, String);
	strip_prefix2(_, _, String) ->
	    String.
	
I don't find either of them particularly easy to read, because the
code contradicts the name.  However implemented, this function DOESN'T
always strip a prefix.  By the time that has been addressed with a
fair comment explaining what the function really does, there is little
to choose between these functions.

	strip_prefix(List, Prefix) ->
	    case lists:prefix(Prefix, List)
	      of true  -> lists:nthtail(length(Prefix), List)
	       ; false -> List
	     end.

is only one line longer than the ++ version, and seems pretty clear
(except for the non-intention-revealing name).




More information about the erlang-questions mailing list