SPUG: Scope question

Richard Anderson richard at richard-anderson.org
Wed Jun 12 21:48:01 CDT 2002

Peter Darley wrote:
> I don't write OO Perl, as I don't see any particular value in it unless
> you're putting modules out into the wild.  I find it far easier to
> of functions as being things that transform data, so I can have any number
> of functions that get data into a certain state before passing it off to
> another function.  The other option seems to be having either tons of
> objects that are very similar, but have different methods of
> and/or processing, or having small numbers of huge unwieldy objects that
> break the idea of having an object fundamentally doing one thing

OO has a LOT of value if you are writing large programs or working on a
programming team.  OO helps organize code, protect internal data from
getting stomped on and make your code understandable to other programmers.
Admittedly, for programs of less than, say, 300-500 lines the formalism of
OO usually doesn't do much to improve the clarity or reusability of code.
The counterargument is that programs tend to grow or have code copied to
other programs.

Your view of functions being things that transform data is inherent in OO
programming: the ideal class had data, methods AND state.  "Tons of objects
that are very similar" is a classic opportunity for using inheritance, which
lets you write lots of programs that do similar things without maintaining
multiple versions of the same code.  I recently used inheritance to write a
single program that supports multiple vendors' proprietary databases and
multiple versions of each vendor's software.  Without inheritance the amount
of code would have more than doubled.  Any case statement structure with
subroutine calls is probably an opportunity to simplify code using

On the other hand, many CPAN modules show the lamentable results of
inappropriately shoehorning simple procedural code into the OO model.  A
good example is Text::Wrapper, which forces the user to initialize an object
just to call the module's one and only method.  As an added obfuscation, the
constructor (new) uses a polymorphic call to the class's AUTOLOAD method
just to initialize an attribute.  The idea of OO is that it is supposed to
make code SIMPLER.

Whether I'm doing OO or procedural programming, I usually fall back on the
old "each subroutine should do one thing well" adage for organizing code.

richard at richard-anderson.org

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org

More information about the spug-list mailing list