Finished!

Matts on 2002-03-01T17:21:55

Whew, I'm SO glad that's over. The 16000 line program no longer has any globals (at least no non-constant globals). Now I can move functions into different files and as long as I export the right functions everywhere, it should all just work.

Well, I did all that while being sick, and it didn't really contribute to my recovery, so I'm going to take it easy for a little while now.


Antiperl

ziggy on 2002-03-01T17:40:51

The 16000 line program no longer has any globals (at least no non-constant globals).
I was just thinking about that particular antipattern today. I'm dealing with code like this:
my $DB = 'db';
my $PARAM = 'param';

$obj->{$DB}->{$PARAM} = $FOO;

Now, obviously that's bad style, but why is it bad style? Then it struck me that it's a perversion of an old FORTRAN/C meme about peppering constant values around as bad style. Sure, it helps to put PI into a [constant] variable so that you localize the changes in exactly one place. Then, should your program ever be run in Indiana, it's much easier to reset the value of Pi to 3. :-)

But Perl is more smalltalkish in that respect, and it's more important to say what you really mean to say (when sending messages to objects, or indexing variables, or whatnot) rather than obscuring it through global variable references. So, constants used as part of a published API are good.

The more appropriate way to factor out globals and constant strings is when they are expected to vary, as in constants that are not part of an API:

my $input = "/opt/foo/bar/input.txt";
my $output = "/opt/foo/baz/output.txt";
Obviously, that's missing a $root_path type variable to handle the case where the program is moved to a host that has /opt/foo located elsewhere. Somehow, I don't recall that distinction being made to a novice audience. Maybe it's time to start updating that meme....

Sounds like a little project I once worked on ...

pudge on 2002-03-01T17:42:13

... called Slash. :-) It acutally wasn't as bad as its reputation. There were just a few globals, and a bunch of undeclared variables, but only a handful of conflicts. But there were a lot of lines of code to go through.