code conventions

Discussion re sg development. You don't have to be a developer.

code conventions

Postby josh » Fri Feb 27, 2004 9:32 pm

We've been talking about refactoring spameater itself to be more readable/maintainable hopefully without impacting performance.

My natural tendency is to make it less procedural by introducing a couple of new classes that would be data based: User and Message, for instance. And instead of passing around big lists of arguments, we could encapsulate stuff better.

Of course, more classes means less performance -- so it will be good to look for ways to improve performance that will offset the use of the classes.

I realize that I've been passing by value in most of the function calls -- passing by reference would be great, and would presumably reduce the memory footprint of spameater. I was thinking that using prototypes would be a good way to go, but I just got done reading Tom Christansen's article "Far More Than Everything You've Ever Wanted to Know about Prototypes in Perl":

http://www.perl.com/language/misc/fmproto.html

and it's got me pretty freaked out about using them. I guess I'm thinking we can either use the convention:

Code: Select all
  myFunction(\$arg1,\$arg2);

  sub myFunction {
    my $arg1 = shift;
    my $arg2 = shift;
     #...
  }


or maybe:

Code: Select all
  myFunction($arg1,$arg2);

  sub myFunction (\$,\$) {
    my $arg1 = shift;
    my $arg2 = shift;
     #...
  }


I've been writing perl for over 10 years, but I must say I still feel like a beginner :) What do you all think?
josh
 
Posts: 1371
Joined: Fri Aug 29, 2003 2:28 pm

Postby SysKoll » Tue Mar 02, 2004 8:00 pm

Josh,

I second the beginner feeling. I'm at the point where I know too much for safety and not enough for comfort.

I'd drop the prototypes alltogether. I don't see what we'd gain.

I use the \% notation to pass references to hashes, and the \@ for lists and scalars works accordingly. It's cleaner than the older method which uses a glob trick. The following programs are equivalent:

Old method:
Code: Select all
#! /usr/bin/perl
sub myFunction  {
  local (*arg1)=shift;
  local (*arg2)=shift;
  my $arg3=shift;
  $arg1 .="xxx";
  $arg2 .="yyy";
  $arg3 .="zzz";  # This 3rd arg is not a reference =>no change in caller var
}
$a="abc";
$b="def";
$c="ghi";
print "a=$a, b=$b, c=$c\n";
myFunction(*a,*b,$c);
print "a=$a, b=$b, c=$c\n";



New method (cleaner, recommended):

Code: Select all
#! /usr/bin/perl
sub myFunction  {
  my $arg1=shift;
  my $arg2=shift;
  my $arg3=shift;
  $$arg1 .="xxx";  # Note the double dollar for dereferencing
  $$arg2 .="yyy";
  $arg3 .="zzz"; # This 3rd arg is not a reference =>no change in caller var
}
$a="abc";
$b="def";
$c="ghi";
print "a=$a, b=$b, c=$c\n";
myFunction(\$a,\$b,$c);
print "a=$a, b=$b, c=$c\n";

-- SysKoll
SysKoll
 
Posts: 893
Joined: Thu Aug 28, 2003 9:24 pm


Return to Developers

Who is online

Users browsing this forum: No registered users and 15 guests

cron