leocharre has asked for the wisdom of the Perl Monks concerning the following question:

I'm doing oo. And embracing the need for consistency accross interfaces. get_x() and set_x() are great consistent ways of naming methods to help maintain integrity of your encapsulation.

add_data(), get_data() and set_data() - where "data" is user, name, date, page, price, whatever.

I am trying to come up with a good general practice to name methods that return data prepped for HTML::Template. For example, a method that returns an array ref of users might be get_users_array() or simply get_users().

What would be a good genral way to distinguish methods that return data for HTML::Template?

I want to keep using this prepend, postpend, description- for methods accross all modules I make which are intended for possible use with HTML::Template. Any ideas? What would be good?

  • Comment on Consistent naming for methods that return HTML::Template prepped data?

Replies are listed 'Best First'.
Re: Consistent naming for methods that return HTML::Template prepped data?
by jeffa (Bishop) on Oct 23, 2006 at 15:30 UTC

    I see no reason to mention that you are using HTML::Template, get_users() should suffice. HTML::Template, Template Toolkit, Class::DBI, and Mason all pretty much expect the same kind of data structures, so i wouldn't bother appending the templating name into your method names.

    However, these days i have been using Class::DBI and class methods to retrieve such data, so i can make my own Customer class and use it like so:

    use Customer; my @customer = Customer->retrieve_all();
    Class::DBI takes care of creating getter and setter methods for you. And there is also the "poor man's object" method by simply using DBI's selectall_arrayref() method:
    my $customer = $dbh->selectall_arrayref( "SELECT customer_id, username FROM customer", { Slice => {} } );
    Now $customers contains a data structure ready to be passed to an HTML::Template <TMPL_LOOP> tag. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      get_x() and set_x doesn't necessarily have to be db retrieval, etc. What if I am getting a listing of files on disk?

      I think I should keep get_directory_list() doing a normal return of array ref to list of files for a directory- for development, for coding use. And I would still need get_directory_list_prepped()

      Yes, your suggestion is golden in regards to db retrieval! Althought- it *could* cripple preformance for large datasets- right?

      Often the data that will be fed to the template is not so simple- It may be a collection of data on disk, metadata in a db, something in relation with who is viewing the data.. etc. Real time things that change and need to be looked up at the moment.

        get_x() and set_x doesn't necessarily have to be db retrieval, etc. What if I am getting a listing of files on disk?

        Leave that up to the implementation of the class you are building.

        I think I should keep get_directory_list() doing a normal return of array ref to list of files for a directory- for development, for coding use. And I would still need get_directory_list_prepped()

        I would consider using one method, get_directory_list(), and pass a parameter in that specifies what kind of data structure to return. I might even consider using a Factory Pattern.

        Yes, your suggestion is golden in regards to db retrieval! Althought- it *could* cripple preformance for large datasets- right?

        Thanks, that's just advice i have picked up from hanging out here at the Monastery. As for large datasets ... anything that returns large datasets should be identified and handled accordingly. Sometimes you can even apply outside solutions to the problem, so don't think you have to always sacrifice development/maintenance effeciency for runtime efficiency. And as for data ... well, data is as data does. :) I have used AJAX in conjuction with Perl to handle "real time" lookups and it wasn't so bad after all.

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: Consistent naming for methods that return HTML::Template prepped data?
by blue_cowdawg (Monsignor) on Oct 23, 2006 at 15:49 UTC
        I am trying to come up with a good general practice to name methods that return data prepped for HTML::Template.

    Why the focus on naming conventions for data prepped for [mod://HTML::Template? My reason for asking that may not be immediately obvious, so let me just make some comments about naming conventions in general.

    A lot of time, energy and brain cycles has been expended over the last 40 or so years that the programming profession/advocation has been around. Some naming conventions make sense and some are downright silly.

    When I worked as a resident contractor for AT&T Bell Labs in the early to mid nineties they released a document that spelled out a set of coding standards that not only embodied naming conventions but how to properly provide comments (embedded code documentation) that looked scarily like the conventions that are used in Java coding. For sake of illustration of my point let me bend the rules of the Monastery a bit by offering this sniglet of (not so great) C code:

    typedef struct myStruct ( int m_i_nrecs; /* How many records? */ record *m_ptr_records /* the records */ } RecordTable; /** *** initRecordTable(n) -- set up the record table *** *** Parameters: *** n (int) how many records do we allocate? *** *** Returns: *** (void) **/ void initRecordTable(n) int n; { /** blah...blah... blah... ***/
    Now, the one part of the AT&T programming guideline that I never agreed with was the prepending of member fields with a "m_" to desginate that it is a member of a structure. Well... duh... I know that and so will any other programmer reading my code.

    Let's take another example: this time from the Java world:

    class Event { private: Date start_date; Date end_date; String description; public: Event() { } public Date getStartDate(){ return this.start_date; } public void setStartDate(Date d) { this.start_date= d; } };
    I left out the requisite comments ('cuz I'm lazy!) but I think you get the idea.

    In the Perl world I've carried over my experiences in other languages and for the example you ask about I'd go one of two ways.

    If I wanted to have a seperate namespace for stuff specifically HTML::Template prep into a module.

    package HTMLTPrepper; use Exporter; use vars qw/ @ISA @EXPORT_OK / ; @ISA = qw/ Exporter /; @EXPORT_OK = qw/ get_users /; sub get_users { # blah.. blah... blah... return @stuff; } 1;

    Then whereever I'm using the prep subs I'll just "use" and away I go. You've just simplified your method names and you've isolated the namespace in one fell swoop.

    The danger to this approach is the fact that if you reuse the name get_users elsewhere you're apt to get confused or worse yet someone who is inheriting this code from you may get confused as to which instance you are referring to.

    The other approach I'd think of using is along the same lines but even more OO in nature:

    package MyTemplates; use HTML::Template; use vars qw/ @ISA /; @ISA = qw/ HTML::Template /; sub get_users { # blah blah blah... return @stuff; } # # # and other subs. 1;
    Now your new module inherts all the HTML::Template methods and plus you can add your own localized methods to the module and contain your namespace very nicely. Now you use a naming convention that makes sense to you and also if someone inherits your code they have some sense as to what in the world you mean by get_users and everybody's happy.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      Wow, I like that last idea a ton. That was very insightful. Thank you so much.