Update: Perhaps a more appropriate title for this node is Minimizing code redundancy vs over-parameterizing

Hi,

I recently refactored a program and eliminated a lot of redundant code. Now I have a series of functions that are structured like this (simplified example):

sub foo { my ( $type ) = @_; my %data = ( type1 => { dir => 'a', text1 => 'text-a1', text2 => 'text-a2' }, type2 => { dir => 'b', text1 => 'text-b1', text2 => 'text-b2' }, ... type9 => { dir => 'i', text1 => 'text-i1', text2 => 'text-i2' } ); # do stuff with $data{$type}{dir|text1|text2} my $barresult = bar( $type ); my $bazresult = baz( $type ); } sub bar { my ( $type ) = @_; my %data = ( type1 => { url => 'a', regex => '\d\d(\d\d)(\d\d)(\d\d)' }, type2 => { url => 'b', regex => '(\d\d)(\d\d)(\d\d)' }, ... type9 => { url => 'i', regex => '\.(\d{8})\.' } ); # do stuff with $data{$type}{url} and $data{$type}{regex} } sub baz { # similar to &bar }

Initially each function was duplicated for each $type (&type1_foo, &type1_bar, &type1_baz, &type2_foo, etc) and the data now in the %data hashes were hard coded. Now the code is easier for me to read and maintain, and all the guts are hidden behind a single call to foo with only one parameter: foo( 'type1' ); However, the code portion for each function is only 20 or 30 lines, which is sometimes shorter than the %data hash that is initialized for each sub, and I am starting to feel like the subroutines are being dominated by the hashes instead of actual code.

I considered pulling all of the %data hashes out and combining them, then passing a hashref ($data{$type}), but while I think that removing the data elements from the subs they are used in will clean up the code, it will obfuscate the purpose of each data element (which would make adding new $types more difficult). I also considered converting the subs into class methods and creating instances for each $type, but for some reason that seems like overkill since all of the data elements would have to be specified as instance attributes anyway. Or should I create separate classes for each $type?

How should I structure a series of relatively short functions that each require a handful of parameters, without having to pass all the parameters around? Was I better off before refactoring? Is OO the way to go here?

Thanks!!


In reply to Structuring code by bobf

Title:
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, details, 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, summary, 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.