SPUG:Designing Programs

Brian Hatch spug at ifokr.org
Fri Jan 10 23:01:12 CST 2003



> I have a question. How do people design large programs with Perl?
> Do you start typing code? Or do you spend some time designing it
> on paper? 

I'm pretty bad.  I don't do much design ahead of time (mind you
most of my projects seldom require lots of code, so it's not
so bad to plan as you go, ~5000 lines is pretty high for me.)

I tend to write starting with the 'main' code, using subroutines
that aren't written as I go.  So I might write

	#!/usr/bin/perl

	use strict;

	my $something = function1(something);
	if ($something == "something else" ) {
		my $tmp = function2();
		my $filename="$DIR/blah";
		sysopen FILE, $filename, O_RDWR|O_EXCL|O_CREAT
			or bail("Some error $?");
	}

Then I run a 'perl -c' and see that I haven't yet defined function1,
function2, or bail, so I go and write them.  In general, my main
section of code is no more than 50 lines, calling functions which I
then write.  Those functions probably call more, which I write as
needed.  Eventually, there's nothing left to write, and I'm done.

I tend to test the functions as I write them.  So say I have the above
code, I might modify it to test function1, ignoring the main code:

	#!/usr/bin/perl

	use strict;

	# test function1 while I debug it
	my $blah = funciton1(something);
	print $blah;

	exit 0;

	my $something = function1(something);
	if ($something == "something else" ) {
		my $tmp = function2();


By doing the exit 0, the actual main code is never
reached, but the functions at the bottom are still
compiled and available.

This is a development method that has served me well for
many years (in other languages as well) but I wouldn't say
it's for everyone, and it certainly doesn't involve much
forethought.  It tends to come out well, because I'm good
at telling when I should subroutineize something for reuse
or to simplify the main code, and it ends up working out
well down the road.

This works for OO code just as well, and yes I use OO
when appropriate, but the things I write are frequently
better suited to procedural programming (sysadmin tasks,
etc.  Sure, I could write an OO object to describe
/etc/passwd, but OO for OO's sake is a bad idea.)



--
Brian Hatch                  ASCII stupid
   Systems and                question, get
   Security Engineer          a stupid ANSI.
http://www.ifokr.org/bri/

Every message PGP signed
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 240 bytes
Desc: not available
Url : http://mail.pm.org/pipermail/spug-list/attachments/20030110/354631f3/attachment.bin


More information about the spug-list mailing list