[V4REV] symbolic constants

Robert Brenstein rjb at robelko.com
Fri May 13 11:27:39 CDT 2005


>  > Lots of typing with plenty of chances for typos whereas typing a
>>  constant will be always simpler and less error prone.
>
>Wait, but what alternatives are ?!!!
>look
>
>1) you can just write
>         get Valentina( "DebugLevel", 3 )
>
>         VDatabase_SqlSelect( query, 1, 2, 2 )
>
>         -- do you remember what means 1, 2, 2 ?!
>            I never remember this
>            when I get project for test I always must go to docs to see
>            what his means and may be here wrong values?

Well, unless I memorize all of your 100 constants, I will have to 
look their names up just as I am looking up the constants. Agreed 
that with symbolic names the code is more readable. Not agreed that 
numeric values are evil per se.


>2) so if you agree that numeric values are devil, then you will start SELF
>define symbolic variable in the same way in EACH function where you need it
>
>  on event      
>       kClientSide = 1
>       kReadOnly = 2
>       kRandom  = 2
>
>       VDatabase_SqlSelect( query, kClientSide, kReadOnly, kRandom )
>  end

a) Such names would not be, however, acceptable IMHO. They must be 
prefixed with gValentina_ for example to avoid name conflicts. The 
names you are suggesting are just too generic. Somebody else looking 
at my code might see kRandom not used for whatever reason and wonder 
what it is and where it is coming from. Using "g" to prefix a global 
var is quite common. See the following article

http://www.fourthworld.com/embassy/articles/scriptstyle.html

b) I can just put your symbolic names and their numeric values as a 
comment when needed. Those entered as constant usually do not change 
during development anyway. If they need to change, then I use a 
variable or code like

   if the hilited of btn "debug" then
      put 3 into vDebugLevel
   else
      put 0 into vDebugLevel
   end if

>
>Actually Transcript itself is very weak here. No constants...

Transcript has constants. Local to function or to an object. It does 
not have constant globals. There are technical reason for not having 
them. But agreed that there are no constants that are useful for you.


>3) btw, way with globals have trap that if you use global var, but forget
>declare it inside of function as global you get zero value.

a) Actually, such a variable should initialize to its own name 
normally (that is it becomes a constant string not a variable), 
although Transcript compiler makes exception when it recognizes it 
must be a variable with a numeric value. True, it is a trap but we 
are used to having to redeclare these.

b) What you said is true but for an object not function. We can 
declare a global either inside a function to keep its scope within 
that function or we can declare a global at top of script's object 
(before any handlers or functions) so its scope is the whole script 
(it is possible to declare it in between as well, to reduce the 
scope, but this is bad practice IMHO).

Robert


More information about the Valentina mailing list