exported variables (was: RE: How to make GS Widgets)

Richard A. O'Keefe ok@REDACTED
Fri Apr 16 03:11:28 CEST 2004


Richard Carlsson <richardc@REDACTED> wrote:
	Yes, but if the code is readable enough, as I would argue that it
	definitely is in the above case, there is no real reason to rewrite
	it to avoid "exporting variables from clauses". 

I would argue that the code in question is definitely NOT particularly
readable.  The most important thing about it is that it binds
Red, Green, Blue.  But the way it is written, it is hard to see that.
In fact, there's something else hard to see:

	> 	if
	> 		is_tuple(SuppliedColor), size(SuppliedColor) == 3 ->
	> 			{Red, Green, Blue} = SuppliedColor;
	> 		true ->
	> 			{Red, Green, Blue} = {0, 0, 0}
	> 	end,

Let's rewrite this for clarity:

    {Red,Green,Blue} = case SuppliedColor of
                          {_,_,_} -> SuppliedColor;
                          _       -> {0,0,0}
                       end

NOW it is obvious that
(1) the purpose of the code is to determine Red, Green, Blue
(2) SuppliedColor is used for this purpose if it looks right
(3) 0,0,0 is used if it doesn't.
(4) The test for SuppliedColor might not be strong enough;
    SuppliedColor = {green,eggs,ham} will in both versions of
    the code result in non-numeric values for Red, Green, Blue.
    It's rather easier to fix this using the 'case' form.

	It's mostly a
	matter of personal taste, but after all, Erlang was designed from
	start to allow this way of binding variables, so why not use it
	as long as your code is readable?
	
On the other hand, why write long-winded unreadable code when short
readable code is easily within reach?



More information about the erlang-questions mailing list