note
Anonymous Monk
<I>Let’s see an example of how clean and self-describing such a thing looks in the language of your choice. Go on, astound me. :-)</I>
<P>
I'm not likely to astound anyone. I write boring code, deliberately. It's easier to find bugs in boring code; code that's full of clever, subtle tricks causes all the problems, in my experience.
<P>
A simple series of append statements describes exactly what you want to do, but you seem to want to make life complicated, because you seem overly-concerned about the repetition of the variable name and the .= operator. I think that's only a real concern if you have to write such repeated expressions a lot in a given body of code, but if you really have to be concise (which is part of what I think you mean by "clean"), I still think there are simpler ways to write them.
<P>
Here's an even more concise way to write the main expression, using two helper functions. No wierd operator tricks are required, and this code could be written in just about any language, let alone a "language of choice". In some languages, using macros as opposed to helper functions might give a performance boost, but the principle remains the same...
<CODE>
# If the constant string "admin" changes, you can change it
# in just one place...
sub admin_link {
my ($flag) = @_;
if ($flag ) {
return('admin');
}
return("");
}
# this can obviously be optimized as necessary...
sub join_if_defined {
my ( $join_string,@list) = @_;
my @defined_elements;
@defined_elements = grep( defined($_),@list);
return join($join_string, @defined_elements );
}
@sections = ( 'http://example.net/app',
admin_link($is_admin),
$subsite,
$node,
$id,
$subnode
);
$uri = join_if_defined("/", @sections);
</CODE>
<P>
I think that's reasonablely concise; it's certainly easier to read than a list containing phrases like "a list of one element consisting of <something> repeated not not <some expression> times".
564792
564866
12