Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: I wrote some clever code - can the comment "defuse" it? (stab at restructuring)

by Aristotle (Chancellor)
on Jun 17, 2003 at 22:13 UTC ( [id://266663]=note: print w/replies, xml ) Need Help??


in reply to I wrote some clever code - can the comment "defuse" it?

After a lot of discussion (much of which happened on the CB and is not going to be available for posterity, I'm afraid) I think the following is much clearer.
my @sequence_length = map { length } join('', @property{@letter}) =~ /\G(X|D+|L+)/g, my @letter_seq = map { $curr->[STYLE] . ":" . join(',', splice @letter, 0, $_), } @sequence_length;
Update: I got pretty positive feedback for this version from the monks involved in the discussion, so that's what I'm sticking with. Thanks everyone! :)

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: I wrote some clever code - can the comment "defuse" it? (stab at restructuring)
by dws (Chancellor) on Jun 17, 2003 at 23:09 UTC
    I think the following is much clearer.

    Saving the run lengths in a temporary does help readability. The only nit I have with the otherwise good updated version is that it destroys @letter from within an expression. In this case, it's what you want, but it risks a "Hey, what happened to my letters?!?" if someone else tries to reuse the snippet later.

      I don't like it either, but the alternative requires an index variable and some math in the index range for a slice, which I find awfully icky. I'm surprised by the number of people who were caught out by the splice though - I use it in similar fashion as I do here pretty frequently, so it's a familiar sight.

      Makeshifts last the longest.

        There are alternatives to either destroying @letters or using an index. Two I can think of off the top of my head are local @letters (unless it's lexical, which it probably is) or a temporary variable (my @l = @letters and change the code to use that instead). I can't say I like using an additional temp var here, but it is another possibility :)

        Alan

Re^2: I wrote some clever code - can the comment "defuse" it? (final form)
by Aristotle (Chancellor) on Jun 18, 2003 at 09:57 UTC
    It just dawned on me that I can follow MarkM's advice from the chatterbox more directly than I thought. My %property is completely redundant considering I have %enc which maps from PS names to Unicode characters.
    my @sequence_length = map { length } join('', @enc{@letter}) =~ / \G( \p{Letter}+ | \p{DecimalNumber}+ | [\P{Letter}\P{DecimalNumber}] ) /gx;
    This is, IMO, hugely more self documenting than what I had before.

    Makeshifts last the longest.

Re: Re: I wrote some clever code - can the comment "defuse" it? (stab at restructuring)
by castaway (Parson) on Jun 18, 2003 at 07:48 UTC
    I didn't follow this node.. Just wanted to mention that the discussion can be made available if anyone would like, and no-one objects.. (Until next log-delete, that is.. hey, rolling logs would be good for im2, which module does that? :)

    C.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-03-29 05:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found