Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Oh Smart Ones,

Had a question for you - I have written some code which allows people to list a specific item (for the purpose of this question here), search through them and then contact the lister. Basically 4-5 perl functions plus 2-3 html template files doing all this stuff. Now the thing is that I want to reuse all the code, especially the perl code, so that they can list different types of items, and the search is specific to that item category, etc.

Now here's the catch, I could just copy all the code, rename the functions, change the variables and SQL statements and use it for the other item, but that seems like a sloppy solution. I would like to resuse the same code, but the thing is that (1) there has to be the ability to accept different variables for each of the different sections (2) the sql statements should be dependent on these different values, etc.

So should I build a library of what sql statements and what variables are assosciated with each category? or ......?

Please do let me know what you guys advise and also if I havent explained this clearly enough.

Thanks, SP
  • Comment on Confused.... question on code scalability (reusing functions, etc)

Replies are listed 'Best First'.
Re: Confused.... question on code scalability (reusing functions, etc)
by Elian (Parson) on Feb 16, 2003 at 15:55 UTC
    Good rules of thumb:
    1. If you're going to do something more than twice, make it a function
    2. If you're going to use it in more than two programs, make it a module
    3. If doing it even once made your head hurt, throw it in a module
    4. If it has data you don't want to change yourself, make it work from template files
    5. If the code is could be generated from some sort of parameters, use a templating code generator for it
    6. If it feels like a monkey could do it, check the above list to see what you missed

      Nice list :). Any reason for the 'more than twice' rather than more than once? Anytime I find myself rewriting code, I put it in a module and don't wait for the third time. Also:

      If it feels like a monkey could do it, check the above list to see what you missed

      I'd argue that if it feels like a monkey could do it, you're using the right tool ;-). Then again, if it feels like it would bore a monkey, you're definately going about it wrong.

        The reason for "more than two" (really three or more) is that I've found, doing a lot of support programming over the years, that tasks come in three forms--the one-off, the one-off you screwed up and have to redo, and the repeater. (Or the one, two, and many times cases)

        I know the traditional CS counting scheme is "0, 1, or many", and that's more or less true for counts on data instances, but I've found in practice for writing code segments it's more "0, 1, 2, not again!"

        The monkey comment was there to make people realize that, if the job requires no brain at all (we're presuming the monkey isn't smarter than the person doing the task) then a program should be doing the task, and you'd be far more effective writing a program to do the task rather than just doing the task. (Time permitting, of course, and it often doesn't)

Re: Confused.... question on code scalability (reusing functions, etc)
by Cabrion (Friar) on Feb 16, 2003 at 12:48 UTC
    Going back to your original question, you can (and probably should) build your SQL statements up from function params.

    Here's an idea as to how that might be done with some untested code.

    sub build_sql () { my $table = shift; my %where = @_; # should use hash refs, but this is an example my $sql = "select * from $table where "; foreach (keys(%where)) { #assumes quote() defined elsewhere to escape values $sql .= "$_ = ".quote($where{$_}).','; } chop $sql; #remove trailing comma return $sql }
    Of course there are serveral modules on CPAN that can abstract you even further (and do so more robustly) from the SQL. Things like DBI::Abstract, Alzabo(sp?), etc.
Re: Confused.... question on code scalability (reusing functions, etc)
by Zaxo (Archbishop) on Feb 16, 2003 at 05:40 UTC
    Please do let me know what you guys advise and also if I havent explained this clearly enough.

    Well, yes, we can critique your code if you show it. Otherwise, we have to guess what you did. We can probably guess some of your mistakes, but it would be silly to keep guessing till you acknowledged the winners.

    After Compline,
    Zaxo

Re: Confused.... question on code scalability (reusing functions, etc)
by Anonymous Monk on Feb 16, 2003 at 05:53 UTC
    Here's some code, which allows the user to make a NOTE request, somebody else to view it and search it and reply to it. Now supposing I want to resuse all of this but more generically, say for notes & GAMES, depending on what the user clicks on ..... how would I go about doing that. BTW I've only been working with Perl for a few weeks and my code is suckky.... please dont flame me too much ... haha (grin). Thanks in advance.
      Two minor and probably utterly useless nitpicks.
      1. I would suggest standardizing on which negator you are going to use. Not or !.
      2. You really don't need parens around every single item in an if statement. if(($foo) and (not($baz=~/qux/))) is surely much easier to read as if($foo and not $baz=~/qux/)

        About "standardizing on which negator you are going to use. Not or !."...

        ...that depends on the intent of expression evaluation. Word operators (not, and, or) bind rather loosely than otherwise (!, &&, ||). Just consider the output of the following...

        perl -e 'print join(" ", (!0 && 0) , (!0 and 0) , (not 0 && 0) , (not +0 and 0))'

        It is ill advised to ask a beginner to standardise on a negator, as you put it, without mentioning the operator precedence level.

        BUU, Noted. Thanks ..... will definitely keep that in mind from now on. SP