Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Background: Every application has user-definable constants. The Java language has the Properties class to handle them.

Question: What is a good and re-usable coding style for setting and changing application level constants in Mathematica?

share|improve this question
    
Would you describe in further detail what you need? I am not familiar with Java so your analogy does not help me. –  Mr.Wizard Jun 19 '12 at 18:03
    
Think of values like directories, authorization, connection strings for a database, ip addresses, logging level etc. Things that are relatively constant for several apps, but may need to be changed in an easy way. - It's easy to do but I would like to know how you guys do it and what standards you use, in Java for example it is common to name constants in ALLUPPERCASE. –  ndroock1 Jun 19 '12 at 18:47
    
What kind of interface does your application have? Notebook? Java? –  Mr.Wizard Jun 19 '12 at 18:48
1  
Since it seems like you want constants accessible from a number of applications, why not create a package of constants and start each application by calling the constants you need. You would then change the constants only in the package should they ever need changing. –  Jagra Jun 19 '12 at 19:00
    
@Mr.Wizard - The user-interface is ( currently ) Notebook, and Java is used for custom language parsing ( ANTLR ). –  ndroock1 Jun 19 '12 at 19:21

1 Answer 1

up vote 10 down vote accepted

Since it seems like you want constants accessible from a number of applications, why not create a package of constants and start each application by calling the constants you need. You would then change the constants only in the package should they ever need changing.

    (* Mathematica Package *)

    BeginPackage["CONSTANTS`"]
    (* Exported symbols added here with SymbolName::usage *)  

    constant1::usage = "some text"
    constant2::usage = "some text"
    constant3::usage = "some text"

    Begin["`Private`"] (* Begin Private Context *) 

    constant1[]: = 22;
    constant2[]: = "myConstant";
    constant3[]: = 0.618;

    End[] (* End Private Context *)

    EndPackage[]

See: Package development Place package in an appropriate directory so Mathematica can see it.

To give your notebooks access to the constants:

Needs["CONSTANTS`"]

constant1[]
constant2[]
constant3[]

22
"myConstant"
0.618

Just a sketch but this should give you the idea.

share|improve this answer
    
Mathematically elegant. "For constants define a function that returns a constant." –  ndroock1 Jun 20 '12 at 8:25
    
Thank for noticing ;-) It did occur to me that Mathematica looks at everything as an expression. Some OOP languages also see variables and constants as objects. –  Jagra Jun 20 '12 at 13:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.