Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users
best practice: where to put constants
 

Index | Next | Previous | View Flat


jarom at jaromsmith

Jun 4, 2009, 8:20 AM


Views: 898
Permalink
best practice: where to put constants

Hi all:

This is probably my lack of Perl knowledge showing... but where would be
the best-practice place to put constants in my code? I'm talking about
magic numbers that I use internally but need to reference in multiple
locations of the code. Things like

"VENDOR_COMMIT" => 1,
"VENDOR_DECLINE" => 2,
"TIME_OUTSIDE_WINDOW" => 3,

In Java I would declare them as "public static final" members the class
that describes that particular object (in this case, a vendor
notification) and I would refer to them like

if (notification.getAction() == Notification.VENDOR_COMMIT) ....

So I think the analog to that in Catalyst would be to define the
constant(s) in the ResultSource classes that are automatically generated
for me (below the line) and then refer to them like

if ($notification->action ==
MyApp::Schema::ResultSet::Notification::VENDOR_COMMIT) ....

The problem with doing it that way is that the namespace starts to
become unwieldly, and I have to remember where I put the thing I'm
trying to reference. The alternative would be to put them at the top
level, i.e., in MyApp.pm:

use constant NOTIFICATION_VENDOR_COMMIT => 1;
use constant NOTIFICATION_VENDOR_DECLINE => 2;
use constant NOTIFICATION_TIME_OUTSIDE_WINDOW => 3;

use constant NOTIFICATION_PROPERTY_ACCEPT => 1;
use constant NOTIFICATION_PROPERTY_CANCEL => 2;
use constant NOTIFICATION_PROPERTY_OFFER_NEW_WINDOW => 3;

and then in the code I just say

if ($notification->action == MyApp::VENDOR_COMMIT) ....

In the end, this is what I decided to do because I have relatively few
of these guys (so far) and I'd rather have them thrown together all in
one place than spread all over the system. But I'm wondering if there
is a best practice?

I don't want to put them in the config hash or in a configuration file
because these are not things that a user should be able to change or
override.

Thanks in advance for the advice/insight; this group has been a great
help so far.

jarom smith
tech go-to guy



_______________________________________________
List: Catalyst[at]lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

Subject User Time
best practice: where to put constants jarom at jaromsmith Jun 4, 2009, 8:20 AM
    Re: best practice: where to put constants bobtfish at bobtfish Jun 4, 2009, 8:31 AM
        Re: best practice: where to put constants jarom at jaromsmith Jun 4, 2009, 8:52 AM
            Re: best practice: where to put constants bobtfish at bobtfish Jun 4, 2009, 9:17 AM
                Re: best practice: where to put constants alejandro.imass at gmail Jun 4, 2009, 9:36 AM
        Re: best practice: where to put constants ijw at cack Jun 4, 2009, 10:07 AM
        Re: best practice: where to put constants mihai at bazon Jun 4, 2009, 11:01 AM
    Re: best practice: where to put constants alejandro.imass at gmail Jun 4, 2009, 8:38 AM
    Re: best practice: where to put constants jarom at jaromsmith Jun 4, 2009, 8:55 AM
    Re: best practice: where to put constants lance at bearcircle Jun 4, 2009, 9:14 AM
        Re: best practice: where to put constants alejandro.imass at gmail Jun 4, 2009, 10:02 AM
    Re: best practice: where to put constants jjn1056 at yahoo Jun 4, 2009, 10:09 AM
        Re: best practice: where to put constants jarom at jaromsmith Jun 4, 2009, 10:23 AM
            Re: best practice: where to put constants moseley at hank Jun 4, 2009, 12:14 PM

  Index | Next | Previous | View Flat
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.