Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Secret Perl Operators: the boolean list squash operator, x!!

by Anonymous Monk
on Aug 01, 2006 at 20:05 UTC ( #565061=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Secret Perl Operators: the boolean list squash operator, x!!
in thread Secret Perl Operators: the boolean list squash operator, x!!

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'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.

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.

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...

# 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);

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".


Comment on Re^3: Secret Perl Operators: the boolean list squash operator, x!!
Download Code
Re^4: Secret Perl Operators: the boolean list squash operator, x!!
by ikegami (Pope) on Aug 01, 2006 at 20:50 UTC

    To be equivalent to the parent code, and to retain the genericity of ( ... ) x!! ...,
    return("");
    should be
    return;
    or
    return wantarray ? () : "";

    This doesn't affect your argument.

Re^4: Secret Perl Operators: the boolean list squash operator, x!!
by Aristotle (Chancellor) on Aug 02, 2006 at 15:46 UTC

    Sorry, but I find that 10◊ less readable than doing the work in place would be. In a realistic codebase, these pieces would be far apart, and making sure you understand exactly what is going on would require crossreferencing. Dependencies should not be introduced without cause, as they always carry a cost. Your suggestion is just an overmodularised version of this:

    my $uri = join '/', 'http://example.net/app', grep { defined } ( ( $is_admin ? 'admin' : undef ), $subsite, $node, $id, $subnode, );

    That is basically tyeís suggestion. And itís a fine suggestion if you can resolve the irregularities enough to do it this way. But it doesnít cover all cases that x!! can Ė see my other example where grep is clearly inappropriate.

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://565061]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2014-10-21 04:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (95 votes), past polls