I've been reading a fair bit on this lately, so while I'm certainly no expert, I do have a bunch of ideas fresh in my head.

Seems to me that one good way to make code more reusable is to make its different elements orthogonal to each other: that is, as uncoupled as possible. So, to take your CGI::FormBuilder example, you'd have one function (or set of functions) for form generation, another for validation, and another for session tracking, but they wouldn't depend on each other: as long as you conformed to the interface, you'd be able to validate input from a form you wrote with a different package; session tracking wouldn't depend on any magic in the form generation code; and so on.

The problem is, this is hard to achieve! You have to spend a lot of time thinking about the interfaces to your code. Writing orthogonal code often means that communicaton between two parts of the same module is more difficult than it would be if you could only share this one little bit of data... but that way lies madness, tight coupling, and endless hours of frustration for anyone who doesn't want to solve the exact problem you had in mind. I find it's useful to design interfaces from the perspective of a user who only wants to use the function you're writing: "I already have a form generator, I just want to do session tracking".

(The problem with that approach is that the resulting code can get disjointed and generally inconsistent. Oh well. If I had all the answers, I'd be making tons of money as a software design consultant.)

An excellent example of solid, orthogonal library design is the ccMath library. (It's ANSI C, not Perl, but the same principles hold.) I haven't worked with every part of it, but what I have worked with has been beautiful. No setup code, no gnarly data types, just call the function you need. And two books you might want to look at (in addition to the others already mentioned) are The Pragmatic Programmer (Hunt & Thomas, Addison Wesley) and The Practice of Programming (Kernighan & Pike, Addison Wesley).

--
:wq

In reply to Re: The temptation to roll your own module than use an existing one.... by FoxtrotUniform
in thread The temptation to roll your own module than use an existing one.... by rrwo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.