8
$\begingroup$

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?

$\endgroup$
6
  • $\begingroup$ Would you describe in further detail what you need? I am not familiar with Java so your analogy does not help me. $\endgroup$
    – Mr.Wizard
    Commented Jun 19, 2012 at 18:03
  • $\begingroup$ 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. $\endgroup$ Commented Jun 19, 2012 at 18:47
  • $\begingroup$ What kind of interface does your application have? Notebook? Java? $\endgroup$
    – Mr.Wizard
    Commented Jun 19, 2012 at 18:48
  • 1
    $\begingroup$ 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. $\endgroup$
    – Jagra
    Commented Jun 19, 2012 at 19:00
  • $\begingroup$ @Mr.Wizard - The user-interface is ( currently ) Notebook, and Java is used for custom language parsing ( ANTLR ). $\endgroup$ Commented Jun 19, 2012 at 19:21

1 Answer 1

10
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ Mathematically elegant. "For constants define a function that returns a constant." $\endgroup$ Commented Jun 20, 2012 at 8:25
  • $\begingroup$ 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. $\endgroup$
    – Jagra
    Commented Jun 20, 2012 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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