the problem of constants
- From: roberson@xxxxxxxxxxxxxxxxxx (Walter Roberson)
- Date: Thu, 5 Jun 2008 21:44:52 +0000 (UTC)
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
.
- Follow-Ups:
- Re: the problem of constants
- From: Walter Roberson
- Re: the problem of constants
- From: Kenneth Eaton
- Re: the problem of constants
- Prev by Date: Re: how to make the following case vectorized?
- Next by Date: Re: the problem of constants
- Previous by thread: how to solve transcendental equation with sin and cos
- Next by thread: Re: the problem of constants
- Index(es):
Relevant Pages
|