Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Recently, one of the projects that I have been working on has many duplicate method calls. I have refactored out much of the duplicate code that remains, but they are still pretty much the same. Here are two examples:

sub add_company { my ( $self, $data ) = @_; my $table = 'company'; my $data = $self->_generic_insert( $data, $table ); $self->{ _dbh }->commit if ! $self->{ _error }; return $data; } sub add_financial_diary { my ( $self, $data ) = @_; my $table = 'financialDiary'; my $data = $self->_generic_insert( $data, $table ); $self->{ _dbh }->commit if ! $self->{ _error }; return $data; }

Note: I don't feel the need for transactions here, but several other methods need transactions and they share the same database handle so I do a commit. Any reason to set up a separate handle that doesn't require transactions?

These are basically the same method, with the exception of the method and table names. I'm thinking about switching this around to stuff the method names in a hash with the value being the table. Then, I'll create an AUTOLOAD routine that traps the name, autogenerates the method or throws an exception if the method is not found. There is no need to install the method in the symbol table as these methods are not called more than once per program run.

I am aware that this will likely slow down the performance of the program, but I was thinking that this will significantly reduce the size of the program and might make maintenance easier. There are several places where I could use this technique. Is this too trivial to really benefit from AUTOLOAD? WWMD? (What Would Monks Do?).


Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

In reply to Refactor method calls or not? by Ovid

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 the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (3)
    As of 2021-04-22 23:25 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found