in reply to Assign Contains RegEx WTF??

It looks like needlessly terse code to me even though it probably works great. Relying on ($bname = $fname) to bind the lefthand value ($bname) with the RE is accurate, but it's difficult to read. The substitution regular expression isn't all that hard to read, but should have been documented, although then you would have:

s#.*\\##s; # comments here.

So perhaps a different delimiter would have been prudent. Why not: s{.*\\}[]s # comments here? It would be a little more legible, and could tell the reader what's going on. Of course there's always YAPE::Regex::Explain that can get you out of a bind when things get cryptic.


Dave

Replies are listed 'Best First'.
Re^2: Assign Contains RegEx WTF??
by JavaFan (Canon) on May 19, 2009 at 15:48 UTC
    It looks like needlessly terse code to me even though it probably works great.
    I've seen enough code by enough people doing this, that's it's idiom to me.

    But then, most idiom will look like needlessly terse code to beginners.

      How does the quote go? "Any sufficiently advanced technology is indistinguishable from magic." (Or something along those lines.)

      Anyway, if the point is to write maintainable code, it still seems reasonable to document well enough that the intent is clear. Code comments aren't only for beginners. They also serve to illustrate to future maintainers that "you really did mean to do that." Plus, they will help to save time as someone else familiarizes him/herself with your code. Or is that "h(?:im|er)self"? ;)


      Dave

        Yes, but where do you stop? At the end of that road is:
        $i++; # Add 1 to $i
        IMO, if you have to document the syntax, you should rewrite the code. Now, I don't think:
        ($copy = $orig) =~ s/PATTERN/REPLACEMENT/;
        is obscure. It shouldn't need comments. If you feel you're among novices or bad programmers, still don't comment the code. Rewrite it in baby steps.