the problem of constants



How do people deal with program constants? I should clarify that
some of these "constants" are expressible as literal constants,
but others need to be constructed by some simple operations upon
literal constants, and become invariant once set.

At present our code uses a global variable as a structure holding
the constants; there are multiple structure levels in some cases.
But there is (generally) a strong recommendation to avoid 'global'.

I could write a simple routine that returned a copy of a
'persistent' (rather than global) structure, counting on
Matlab's "copy on write" behaviour to provide the efficiency.
That would provide a level of access-control -- it would at least
prevent any routine from (somehow) accidently changing the master
copy of the structure. But it seems like a kludge, and it
surely cannot be good for efficiency, especially if one of the
values is needed in a routine called in a tight loop.

I could pass a copy of the structure of constants around through
virtually *all* routine calls (except those routines for which
it could somehow be proven that the constants would never be needed).
This does not seem very parsimonious with parameter lists.

I could write a routine that was handed the name of the constant
and which returned the value of the constant. I did something
very like that for the user preferences (semi-constants subject
to occasional change); in doing so, I encountered quite significant
slowdowns in my inner loops, which I traced to the need to translate
from the constant name into a cell index. Thus I know from experience
that providing a similar routine for the constants could have the
potential to slow the program quite a lot.

I suppose instead of using a global, I could evalin('base'),
but that seems unlikely to be any kind of improvement!

Using appdata() or a 'User' field of a control sounds unlikely
to be any better, in terms of programming theory or efficiency,
than the above-mentioned possibility of a routine to return a copy
of the structure. appdata and User fields and guihandles do not
prevent me from changing the values, after all: they just make it
more work to do so... and if I hypothetically have the programming
discipline to avoid making the calls to change the values once set, then
by the same measure I also have the programming discipline to avoid
assigning to a field in a global variable.

Would it be realistic to dynamically construct a class which had
methods whose names were the constants (or some other similar
object-based mechanism which prevented the objects from being written
to once finalized) ? Would that provide efficiency and constancy?

Or have I missed an efficient symbol-table function in Matlab ?
--
"There's no term to the work of a scientist." -- Walter Reisch
.



Relevant Pages

  • Re: Native Code vs PCode OT
    ... > register Left-to-right Routine Yes ... claim influence or invention from Pascal. ... invented by a Russian first. ... to that new concept - Structured Programming. ...
    (microsoft.public.vb.general.discussion)
  • Re: OCP and Java
    ... > a hard time imagining any programmer with the clairvoyance required ... The only place that I can think of where frozen is used in the Eiffel ... An example is a routine that compares to objects for equality. ... > kind of flexibiliy is valuable there are already programming ...
    (comp.object)
  • Re: bug in rem
    ... returned is due to roundoff error, but the real problem is different. ... values at some point in the routine. ... the programming issues were allowed to take precedence over what was ... what you have put into the 'rem' function input when you write the absurd ...
    (comp.soft-sys.matlab)
  • library function vs system calls
    ... I was going through peter van der linden's book expert C programming ... *call to a routine in a library ... *has the lower head overhead of procedure call. ... executes in the kernel address space ...
    (comp.lang.c)
  • Re: parser
    ... Probably because not all grammars can be parsed with a recursive descent ... But not all parser generators can handle all grammars, ... "Efficiency" is a vague term with the appearance of precision. ... In most demotic programming circles, in fact, the word "efficiency" ...
    (comp.programming)