Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Am I wrong to want to do this? I like making everyone who's using my module happy. I like the flexibility.

My point is that if a module or library really benefits from being constructed using the OO style; a procedural interface to it doesn't make sense.

Conversely, if a module/library lends itself better to a procedural interface, adding an pseud-OO interface also, doesn't add flexibility, it adds unnecessary complexity.

For example. You'll find a bunch of statistics modules on CPAN that have OO interfaces. You create an instance; add your data to that instance either item by item or on mass; and then call various methods on it to retrieve the stats.

Which is fine if your script consists of: reading a file of data; and then outputting the required stats to the screen or a file. Only.

But most of the time in my scripts; the data I want stats on is wrapped up in some larger data structure that gets used for drawing graphs or processing to images or any number of other processes. The stats is just a small step in the overall picture.

Having to copy the data from where it lives into a object in order be able to get the average and stddev, means duplicating the memory and copying stuff from place to place; when all I want or need is:

... my( avg, $stddev ) = avgNstddev( $bigStruct->{something}{something +else} ); ## hash expression yields an array ref. ...

In this case, an OO interface would require:

... my $stats = Stats:Mod->new; $stats->add( $_ ) for @{ $bigStruct->{$something}{$somethingelse} +}; my $avg = $stats->avg; my $stddev = $stats->stddev; $stats->empty; ...

Not only more complex code; but in the process I trebled the memory requirement: the original source of the data; the list created by the for; and the copy held internally by the object. If the array is large, that's a problem.

If the ...s in the two snippets above are:

for my $something ( keys %bigstruct ) { for my $somethingelse ( keys %{ $bigstruct->{ $something } } ) { ... } }

It starts to be a big and expensive problem.

Now, for some types of stats, an OO interface makes sense because some calculations can benefit greatly from caching results from earlier calculations. Simple example, If you need both the average and stddev, having separate functions for each means you'll probably end up calculating the average twice. Whereas an object interface can cache the average internally and reuse it for the stddev; or if stddev is called first, calculate and cache the average whilst calculating the stddev and when (if) the average method is called, avoid having to recalculate it by returning it from cache. And both cached values can be reused when calculating the return for other methods.

But rather than then forcing me to add my data to an object to get the simple values; make the average() a separately callable sub (or a class method) that takes an array ref, usable procedurally. Then have the object interface use that internally to calculate averages, Ditto for stddev(). Have it be another sub/class method with parameters of the array ref and their average.

That way, for the simple cases, I can use the procedural interface, and only use the OO interface for the more complex calculations.

Of course, the best interface might be to have the OO stats module take an array ref as the argument to its constructor:

my $stats = Stats::Module->new( $bigstruct->{ $something }{ $something +else } ); my( $avg = ( $stats->average, $stats->stddev ); $stats->done;

Still a little more code than the simple procedural interface; but with the advantage that it can handle the more complex operations without needing to replicate the data internally. Of course, it comes at the cost that if you modify the data between calls to methods for the various stats, it could try calculating the stddev for the modified data by reusing the cached average calculated against the original data.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong

In reply to Re^2: module w/ object-oriented and functional interfaces: best practices? by BrowserUk
in thread module w/ object-oriented and functional interfaces: best practices? by temporal

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (7)
    As of 2017-12-18 21:07 GMT
    Find Nodes?
      Voting Booth?
      What programming language do you hate the most?

      Results (498 votes). Check out past polls.