Phoenix.pm: Please help, my brain is fried

Kevin Buettner kev at primenet.com
Thu Jan 27 14:49:02 CST 2000


On Jan 27, 12:43pm, Mark Pease wrote:

> # Pick the number of characters to have in the login id. It must have
> # at least 6 and no more than 12 elements.
> $id_length = int(rand(6)+6);
> 
> # Build an array where each element is a reference to another array
> # that contains all of the upper and lower case letters (52 elements in
> each).
> # (The 'x' operator, in array context, will build an array where each
> # element is the same.)
> @passwd = (['a'..'z','A'..'Z']) x $id_length);
> 
> # Now pick one letter from each array reference.
> # (Map will execute a chunck of code on each element of an array,
> # and return an array where each element contains the value returned
> # from the chunck of code.)
> @passwd = map $_->[int(rand(52)], @passwd;
> 
> # The array now contains the login id, one character per element.
> # Join all of the character together into one string, and return.
> return join '', @passwd;
> 
> 
> > (the classic 'brevity versus understandability' dillemma)
> 
> No dillemma, understandablility should always win over brevity.

Yes, but which code is more understandable, code which is brief
and to the point, or code which is heavily commented with lots
of intermediate variables thrown in?

I would probably comment the example under consideration as follows...

    # Create a string suitable for use as a login id.
    sub create_login_id {
	# Return a string consisting of random alphabetic characters
	# whose length (chosen randomly) is between 6 and 11 inclusive.
	return join('', map $_->[int(rand(52))], 
			    (['a'..'z','A'..'Z']) x int(rand(6)+6));
    }

I would not break the return expression up into pieces except for
pedagogical purposes.

Kevin

-- 
Kevin Buettner
kev at primenet.com, kevinb at redhat.com



More information about the Phoenix-pm mailing list