Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #3839

Re: Best practice for using constants outside of code?

From "Pascal J. Bourguignon" <pjb@informatimago.com>
Newsgroups comp.programming
Subject Re: Best practice for using constants outside of code?
Date 2011-02-02 21:29 +0100
Organization Informatimago
Message-ID <87oc6udvt5.fsf@kuiper.lan.informatimago.com> (permalink)
References <Xns9E80A0F37DC9Cikkehierbe@69.16.176.253> <4d4970a7$0$81476$e4fe514c@news.xs4all.nl> <Xns9E80A59ADF9BFikkehierbe@69.16.176.253>

Show all headers | View raw


Ikke <ikke@hier.be> writes:

> <snip>
>> That can only be solved by adding another layer of constant/value 
>> definitions.
>> How about a simple .INI file?
>
> Thanks for your suggestion!
>
> Unless I incorrectly interpreted your suggestion, that would leave the same 
> problem I guess...
>
> The original ini file would be changed from:
> person.gender=1
> to:
> person.gender=ConstGenderMale
>
> The ini file holding the constants would define the constants as:
> ConstGenderMale=1
>
> And the constants in code would remain the same:
> ConstGenderMale=1
>
> As soon as anybody makes a change in either code or .ini, he/she will need 
> to remember to change the counterpart in .ini or code as well, which is 
> what I was trying to avoid.

Obviously, you will have to define your constants differently:

const
   ConstGenderMale = "ConstGenderMale";
   ConstGenderFemale = "ConstGenderFemale";


Then of course, since you will have to provide functions to read and
write these representations and convert them into these constant values,
you might want to define a class:

class: Symbol
   slots:
       name: String;
       value: String;

function keyword(name)
   new Symbol name=name value=name

function read:
   token := parseToken();
   if token.type = identifier then
      return keyword(identifier)
   else
      ...

method print (self Symbol)
   write(self.value)



So you can define:

const
  ConstGenderMale   = keyword("ConstGenderMale");
  ConstGenderFemale = keyword("ConstGenderFemale");



Of course instead of apply Greenspun's Tenth Law all the time, you could
also just use Lisp.
  
-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.

Back to comp.programming | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Re: Best practice for using constants outside of code? Jongware <jongware@no-spam.plz> - 2011-02-02 15:56 +0100
  Re: Best practice for using constants outside of code? Ikke <ikke@hier.be> - 2011-02-02 15:16 +0000
    Re: Best practice for using constants outside of code? "Pascal J. Bourguignon" <pjb@informatimago.com> - 2011-02-02 21:29 +0100

csiph-web