Re: Confused.... question on code scalability (reusing functions, etc)
by Elian (Parson) on Feb 16, 2003 at 15:55 UTC
|
Good rules of thumb:
- If you're going to do something more than twice, make it a function
- If you're going to use it in more than two programs, make it a module
- If doing it even once made your head hurt, throw it in a module
- If it has data you don't want to change yourself, make it work from template files
- If the code is could be generated from some sort of parameters, use a templating code generator for it
- If it feels like a monkey could do it, check the above list to see what you missed
| [reply] |
|
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.
| [reply] |
|
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)
| [reply] |
Re: Confused.... question on code scalability (reusing functions, etc)
by Cabrion (Friar) on Feb 16, 2003 at 12:48 UTC
|
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. | [reply] [d/l] |
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
| [reply] |
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.
| [reply] [d/l] |
|
Two minor and probably utterly useless nitpicks.
- I would suggest standardizing on which negator you are going to use. Not or !.
- 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/)
| [reply] [d/l] [select] |
|
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. | [reply] [d/l] |
|
BUU,
Noted. Thanks ..... will definitely keep that in mind from now on.
SP
| [reply] |