Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: I wrote some clever code - can the comment "defuse" it?

by jepri (Parson)
on Jun 18, 2003 at 05:58 UTC ( [id://266734]=note: print w/replies, xml ) Need Help??


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

As others have kind of touched on, comments should never explain what the code actually does. If you ever feel the need to to explain code, then you do need to rewrite it to make it clearer.

I think that your rewritten version is clearer, but I feel the problem in the original is the splice. I have developed a well earned loathing of tinkering with arrays like that. I prefer to map or push elements and work on them methodically.

I think you could do it a little more lisp like with multiple maps taking the place of the join and regexp, but that's personal style, and that would require a temporary variable outside the scope of the map block. But temporary variables aren't always bad.

____________________
Jeremy
I didn't believe in evil until I dated it.

  • Comment on Re: I wrote some clever code - can the comment "defuse" it?

Replies are listed 'Best First'.
Re^2: I wrote some clever code - can the comment "defuse" it?
by Aristotle (Chancellor) on Jun 18, 2003 at 10:02 UTC

    I need slices of varying length from the input array. There's currently no way to express this succintly - the splice solution is merely the least unsatisfactory given my biases. It's not good, no, but the alternative is even worse.

    The other approach would have been to loop over the letter array and manually compare previous and current property. Conceptually, that's a very basic form of pattern matching - so why not use the native pattern matching mechanism Perl already offers?

    The problem is that the regex engine only works against characters in a string - if it was possible to run against any ordered sequence of elements, regardless of representation (whether that be the traditional characters in a string or elements in an array or maybe something else completely like records in a database), I wouldn't need to go through mapping to a string first. Rather than get a series of strings out of the match, I'd get exactly the series of slices I'm building manually here.

    Since I can only have the former, I have to somehow construct the latter from that. And a series of slice lengths to take from an array is not elegantly expressible in current Perl idioms.

    Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 10:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found