Friday 26 August 2011

Where should I store hard-coded values

Question:
I need to change the PL/SQL packages of 24 apps, 10 of which are business critical. These packages have a few hard coded values (like company name).
I know I can use package-level variables with default values (put in a common schema to establish one location for these values across the Oracle instance). Or, I was thinking of using a table to init these values (keep them empty) and load them up only when necessary. Thus the table will be accessed once per connection.
What do you think is a better approach, given that there are four such hard-coded values?
Answer:

Your overall direction and intentions are great! You certainly should get those hard-coded values out of your programs and define them in a single place. Then if any of them need to be changed, you change the value in one place, recompile code as necessary, and from that point on all the programs are using the new value.
So the question becomes: Where to store the values?
As you point out, we have several options:
• Define them in a package, in the specification as constants, or as functions with the value hidden (hard-coded) in the package body.
• Define them in a database table. Then provide a function in a package that returns the value from the database table. Use the initialization section of the package to load the values just once per session (which is cleaner than the technique you mention above).
• Define them in a file, and then load the values from the file upon package initialization—ugh!
So let's agree on one thing: delivering the values through a package specification is best. It is also preferable to place the values in the package body instead of the specification so that when the value changes, you don't have to recompile the package spec and thereby invalidate all references to the package.
I suggest you absorb the overhead and complexity of storing the values in a database table only if the values change fairly often, and nonprogrammers can or should be allowed to set the values themselves.
If the values change rarely, if ever, a simple package specification of constants will work just fine. Here is an example of such a package for use with UTL_FILE:
CREATE OR REPLACE PACKAGE utl_file_constants
IS
c_read_only CONSTANT VARCHAR2 (1) := 'R';
c_write_only CONSTANT VARCHAR2 (1) := 'W';
c_append CONSTANT VARCHAR2 (1) := 'A';
c_min_linesize CONSTANT PLS_INTEGER := 1;
c_def_linesize CONSTANT PLS_INTEGER := 1024;
c_max_linesize CONSTANT PLS_INTEGER := 32767;
END utl_file_constants;
/

No comments:

Post a Comment