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

Re^2: Perl style: Arguing against three-argument join() (++join '')

by jplindstrom (Monsignor)
on Jan 25, 2008 at 00:27 UTC ( [id://664181]=note: print w/replies, xml ) Need Help??


in reply to Re: Perl style: Arguing against three-argument join() (++join '')
in thread Perl style: Arguing against three-argument join()

++ for the reasoning behind your choice.

However, reading the code it strikes me there may be a more general approach. Name all final expression values and put them in variables. Then you can simply interpolate them in a qq string and be done with it.

As a bonus, it's more self documenting.

Also, from your example above, note how the duplication of $new + $old goes away.

I rarely generate HTML like this, but perhaps an argument can be made that the method and sub calls above (createLink, sprintf) are special enough to do something clever with the Interpolation module to be able to write something like this:

my $message_count = $new + $old; $html .= "$createLink{$USER, "you"} have $message_count $plural_of{"me +ssage", $message_count}";

(Escaping the HTML is left as an exercise for the reader, as is tradition)

/J

Replies are listed 'Best First'.
Re^3: Perl style: Arguing against three-argument join() (vars)
by tye (Sage) on Jan 25, 2008 at 02:47 UTC

    The repetition of $new+$old was an artifact of quickly fitting several demonstrations in a single example. I wouldn't jump to separating out such simple expressions that are only used once so that you have to use the variable names to mentally map a large number of items being combined to what they were set to. If an expression is more complex than the ones in my example, then introducing a named variable often makes sense.

    But the benefit of a named variable usually depends heavily on the quality of the name. With such short expressions, the name of the variable is often going to be only slightly shorter and can be much less clear. I can put a lot of effort into coming up with names when coding. Great names versus unclear names makes an enormous difference in the clarity of code. So I'm not excited to introduce a new name for something so small as createLink($USER,'you') that is used only once.

    And part of the point I was making with the example was how things scale when the code deserves to be split over multiple lines. I find this makes interpolation break down as far as code readability; that is, I don't like reading several lines of strings each with several named variables interpolated and above this a long list of variables being declared and initialized. For example, it makes it hard to notice that you are doing work to assign a value to a variable that you are actually no longer using below.

    As for Interpolation.pm, you don't show how much code has to be added to create %createLink and %plural_of and then has to be understood. A function or method like createLink() is a general-purpose abstraction while you wouldn't define just %createLink and only use it, avoiding the duplication of making a createLink(). So I've never gone that route. But since interpolating into strings also doesn't scale well for me, that isn't surprising.

    - tye        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-03-28 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found