Seeking advice on semantics (pros / cons and best ways of) using constants across modules 
Author Message
 Seeking advice on semantics (pros / cons and best ways of) using constants across modules

Hi,

I have a collection of Perl modules that process and display in a web
browser objects of various types.

The display functions are adjustable in terms of layout, content to
some extent, and potentially output format. To define these
parameters, I would like to use constants, which is more readable than
using numbers and more efficienty than using string parameters.

Therefore I have something like the following:


COMPLETE TXT HTML XML MINIMAL SMALL MEDIUM LARGE MAXIMAL LINE TABLE);

#--- constants declarations ---------------------------------------
use constant DEBUG      => 0;
use constant BENCHMARK  => 0;

use constant TRUE       => 1;
use constant FALSE      => 0;
use constant YES        => 1;
use constant NO => 0;

use constant TERSE      => 11;
use constant NORMAL     => 12;
use constant VERBOSE    => 13;
use constant COMPACT    => 14;
use constant EXPANDED   => 15;
use constant COMPLETE   => 16;
use constant MESSAGING  => 21;

use constant TXT        => 100;
use constant HTML       => 101;
use constant XML        => 102;

use constant MINIMAL    => 201;
use constant SMALL      => 203;
use constant MEDIUM     => 205;
use constant LARGE      => 207;
use constant MAXIMAL    => 209;

use constant LINE       => 210;
use constant TABLE      => 211;
#------------------------------------------------------------------

This is so the cide using the modules in question could use:

$business_contact->to_string({'detail' => MEDIUM});

... as opposed to
$business_contact->to_string({'detail' => 'MEDIUM'});
or
$business_contact->to_string({'detail' => 205});

Here are the questions:

1. Is it even worth the bother? Am I complicating it far too much to
avoid the string comparison in the parameter (which is only done a
dozen or so times per execution)? Is there another / better way?

2. There are several modules that would all use this system of
arguments (i.e. these constants would be applicable to input arguments
of methods / functions in more than one module). Should the constants
be consolidated in a single My::Constants module or should each module
export them individually?

3. If the latter (i.e. each module should have and export its own
constants), how do I deal with warnings that constant definitions of
one module are being overwritten by another (even if it is an
identical definition)?

Your advice would be appreciated.

Alex



Wed, 26 Oct 2005 06:40:27 GMT  
 Seeking advice on semantics (pros / cons and best ways of) using constants across modules

Quote:

> Here are the questions:

> 1. Is it even worth the bother? Am I complicating it far too much to
> avoid the string comparison in the parameter (which is only done a
> dozen or so times per execution)? Is there another / better way?

C programmers use a lot of constants like this, it is quite normal.

Quote:
> 2. There are several modules that would all use this system of
> arguments (i.e. these constants would be applicable to input arguments
> of methods / functions in more than one module). Should the constants
> be consolidated in a single My::Constants module or should each module
> export them individually?

Depends on your taste. Constants which belong only to one module should
be declared there and not be exported. If you need them global, make a
My::Constants module.
A My::Constants Module makes life easier if you later have to change
something.

You could declare your constants in the file where your main function
lives but this will clutter up the file almost until unreadable if you
have lots of them.

C programmers in general have one header for the constants that will be
included by any source file that needs it.

In terms of perl that would read: One module for the constants, used by
each other module that needs it.

Hope that helps,
Robert
--

and goes directly into the bin.
To contact me privately reverse and use



Thu, 27 Oct 2005 19:26:41 GMT  
 Seeking advice on semantics (pros / cons and best ways of) using constants across modules

Quote:

> I have a collection of Perl modules that process and display in a web
> browser objects of various types.

> The display functions are adjustable in terms of layout, content to
> some extent, and potentially output format. To define these
> parameters, I would like to use constants, which is more readable than
> using numbers and more efficienty than using string parameters.

> Therefore I have something like the following:

> require Exporter;


> COMPLETE TXT HTML XML MINIMAL SMALL MEDIUM LARGE MAXIMAL LINE TABLE);

> #--- constants declarations ---------------------------------------
> use constant DEBUG => 0;
> use constant BENCHMARK     => 0;

> use constant TRUE  => 1;
> use constant FALSE => 0;
> use constant YES   => 1;
> use constant NO    => 0;

> use constant TERSE => 11;
> use constant NORMAL        => 12;
> use constant VERBOSE       => 13;
> use constant COMPACT       => 14;
> use constant EXPANDED      => 15;
> use constant COMPLETE      => 16;
> use constant MESSAGING     => 21;

> use constant TXT   => 100;
> use constant HTML  => 101;
> use constant XML   => 102;

> use constant MINIMAL       => 201;
> use constant SMALL => 203;
> use constant MEDIUM        => 205;
> use constant LARGE => 207;
> use constant MAXIMAL       => 209;

> use constant LINE  => 210;
> use constant TABLE => 211;
> #------------------------------------------------------------------

> This is so the cide using the modules in question could use:

> $business_contact->to_string({'detail' => MEDIUM});

> ... as opposed to
> $business_contact->to_string({'detail' => 'MEDIUM'});
> or
> $business_contact->to_string({'detail' => 205});

This seems like a lot of extra processing, but please clarify:

Do all those numbers _mean_ something? I.e., what does the method
to_string do with those numbers?

Are you exporting methods as well as those constants? Is to_string the
only method you export?

Is 'detail' the only parameter you pass to to_string? And do you pass
only one value per method call?

   - Bruce

__bruce__van_allen__santa_cruz__ca__



Thu, 27 Oct 2005 13:00:10 GMT  
 Seeking advice on semantics (pros / cons and best ways of) using constants across modules
Hi Alec, IMO and from the code quality side:


Quote:
> Here are the questions:

> 1. Is it even worth the bother? Am I complicating it far too much to
> avoid the string comparison in the parameter (which is only done a
> dozen or so times per execution)? Is there another / better way?

Absolutely.

Quote:
> 2. There are several modules that would all use this system of
> arguments (i.e. these constants would be applicable to input arguments
> of methods / functions in more than one module). Should the constants
> be consolidated in a single My::Constants module or should each module
> export them individually?

If you have few constants (say less than 25), I think it's ok to export
them from one single module. When a single module exports lots of
constants, it becomes difficult to attach a meaning to the constant, it is
then better to define them in the module that uses them. I sometimes go to
the extent to define them just before I declare the sub. Locality is a
tool one can use to understanding the code better. 'Magic' numbers (that's
the official name for values which cary no meaning by themselfs (ex 1, 25,
...) in oposition to named constants) are a pain when you re-read your
code or someone else does. The worst case being 'magic' numbers used to
index a data structure. So it is also a good idea to use constants even if
they have a very short life span.

Quote:
> 3. If the latter (i.e. each module should have and export its own
> constants), how do I deal with warnings that constant definitions of
> one module are being overwritten by another (even if it is an
> identical definition)?

either name them with a prefix, ex : use constant MODULEX_CONSTANT => ... ;
or access them through a module $a = Module::CONSTANT ;

Note that you should use the first method when exporting all your constants
from a single module (actualy the prefix doesn't have to be a module name,
it could be a structure type or whatever make the constant easier to
understand).

My preference goes to Module::CONSTANT (CONSTANT could still be prefixed to
cary more meaning)


                RULE_TYPE
                        UNTYPED
                        VIRTUAL
                        LOCAL
                        FORCED
                        POST_DEPEND
                        META_RULE
                        META_SLAVE
                RULE_NAME
                RULE_ORIGIN
                RULE_FILE
                RULE_LINE
                RULE_DEPENDER
                RULE_BUILDER
                RULE_BUILDER_ARGUMENTS
                RULE_TEXTUAL_DESCRIPTION
                ) ;

#-------------------------------------------------------------------------------
# data created in sub 'RegisterRule'.
use constant RULE_TYPE                => 0 ;
                use constant UNTYPED     => '__UNTYPED' ;
                use constant VIRTUAL     => '__VIRTUAL' ;
                use constant LOCAL       => '__LOCAL' ;
                use constant FORCED      => '__FORCED' ;
                use constant POST_DEPEND => '__POST_DEPEND' ;
                use constant META_RULE   => '__META_RULE' ;
                use constant META_SLAVE  => '__META_SLAVE' ;
use constant RULE_NAME                => 1 ;
use constant RULE_ORIGIN              => 2 ;
use constant RULE_FILE                => 3 ;
use constant RULE_LINE                => 4 ;
use constant RULE_DEPENDER            => 5 ;
use constant RULE_BUILDER             => 6 ;
use constant RULE_BUILDER_ARGUMENTS   => 7 ;
use constant RULE_TEXTUAL_DESCRIPTION => 8 ;
#-------------------------------------------------------------------------------

Thank you for your question, the project I am working on calls for constant
definitions, I started with a single module exporting all constants. I
have counted them and they have reached 60+. It is time to split them.

Hope this helped, Nadim.



Thu, 27 Oct 2005 18:39:59 GMT  
 Seeking advice on semantics (pros / cons and best ways of) using constants across modules

Quote:
>Therefore I have something like the following:

>require Exporter;


>COMPLETE TXT HTML XML MINIMAL SMALL MEDIUM LARGE MAXIMAL LINE TABLE);

Look into breaking these into multiple tags (see Exporter docs).

Quote:
>Here are the questions:

>1. Is it even worth the bother? Am I complicating it far too much to
>avoid the string comparison in the parameter (which is only done a
>dozen or so times per execution)? Is there another / better way?

I would be surprised to see any great difference even between
thousands string and numeric compares.  But IMO using exported
constants reads better.

Quote:
>2. There are several modules that would all use this system of
>arguments (i.e. these constants would be applicable to input arguments
>of methods / functions in more than one module). Should the constants
>be consolidated in a single My::Constants module or should each module
>export them individually?

I would put them in a separate constants module and export them from
there to your modules that take them as paramters and export them again
from those modules to the actual code that passes them.

Quote:
>3. If the latter (i.e. each module should have and export its own
>constants), how do I deal with warnings that constant definitions of
>one module are being overwritten by another (even if it is an
>identical definition)?

If you import FOO from Const.pm to A.pm and C.pm, and foo.pl uses
A and C, which both export FOO, you shouldn't get a warning.  But it
would be worthwhile to make sure the perl test suite has a test for
this situation.


Fri, 28 Oct 2005 05:21:36 GMT  
 Seeking advice on semantics (pros / cons and best ways of) using constants across modules

Quote:

> This is so the cide using the modules in question could use:

> $business_contact->to_string({'detail' => MEDIUM});

> ... as opposed to
> $business_contact->to_string({'detail' => 'MEDIUM'});
> or
> $business_contact->to_string({'detail' => 205});

is is so the cide using the modules in question could use:
 >>
 >> $business_contact->to_string({'detail' => MEDIUM});

Maybe you want a different design.

my $format = Bla::Format->new;

$format->txt;
$format->medium_detail;

$business_contact->to_string($format);

or something similar.

Quote:
> Here are the questions:

> 1. Is it even worth the bother? Am I complicating it far too much to
> avoid the string comparison in the parameter (which is only done a
> dozen or so times per execution)? Is there another / better way?

If you choose a code design where your constants are only used a few
times, you can simply always use the fully qualified constant name. That
way it's always clear where something is coming from.

malte



Fri, 28 Oct 2005 20:47:22 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. ADOExpress available?

2. Object Representation & Linking

3. Compiler error 88

4. Wildcards or LIKE in SQL

5. Borland Pascal 7.0 with objects

6. Interbase 4.2 via mail order

7. What are the pro's and cons of the different scripting languages

8. Tk vs Wafe: pros/cons?

9. Survey Pros & Cons OOP

10. intermixed code and POD - pros and cons?

11. system function, Pros and Cons??

12. Pros / Cons of upgrading from 5.005 to 5.6.0

 

 
Powered by phpBB® Forum Software