In a recent thread, the monastery weighed the relative merits of using shift() to extract function parameters. That's fine as far as it goes, but I have some opinions about argument passing that pretty much moot the question of how to extract them.

My basic rule is: Never write a function that takes more than one argument.

By and large, function parameters fall into one of three categories:

To illustrate those three ideas, let's look at a toy program:

sub print_multiple { my ($count, $string, $format) = @_; for (1..$count) { print &format_string ($string, $format), "\n"; } return; } sub format_string { my ($string, $format) = @_; my %templates = ( 'bold' => '<b>%s</b>', 'italic' => '<i>%s</i>', ); return (sprintf ($templates{$format}, $string)); } &print_multiple (5, 'hello, world.', 'bold');
(I admit this is a contrived example, but non-contrived examples tend to be big. If you want to see switching parameters in real code, flip through a copy of Numerical Recipes in C)

In print_multiple(), $count is a switching parameter, while $string and $format are tramp data.

In format_string(), $string is primary data and $format is a switching parameter.

The whole point of using functions is to isolate tightly-cohesive bits of logic so you can call them from other parts of the program. That gives you primary data. Then you want your functions to be general enough that they're useful in lots of settings, which gives you switching parameters. Then you build high-level functions that call low-level functions, but you still have to get all the parameters to the low-level code, which creates tramp data. Then the whole thing gets so convoluted that you can't keep track of it any more, and you start buying books about OOP.

The way to break out of that trap is to think about data independently from the logic. Instead of thinking "what parameters does this function need?" think "what does this information describe?". Assemble your data in coherent structures, then pass those structures to functions that use whatever information they need.

Applying that concept to the toy program gives us this:

sub print_multiple { my $b = shift; for (1..$b->{'count'}) { print &format_string ($b), "\n"; } return; } sub format_string { my $b = shift; my %templates = ( 'bold' => '<b>%s</b>', 'italic' => '<i>%s</i>', ); return ( sprintf ( $templates{ $b->{'format'} }, $b->{'string'} ) ); } $block = { 'string' => "hello, world.", 'format' => "bold", 'count' => 5, }; &print_multiple ($block);

Instead of dealing with random data, we now have a structure that represents a block of formatted text. We can add other attributes to that structure if we need to, without imposing any change on the functions we already have. Heck, we can store it and use it again, which isn't true for the previous version.

The control flow and logical structure of a program are only one half of the story. The data model is the other half. Instead of worrying about how you're going to get parameters into a function, work out a data model, then build your logic to fit that model. Put the burden of organizing information into the data structures where it belongs, instead of smearing it out across your function signatures. You'll find that your parameter lists shrink dramatically, and that your functions will be easier to write.


In reply to passing arguments by mstone

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.