Naming for global symbols?

Started by cormullion, October 27, 2007, 06:25:17 AM

Previous topic - Next topic

cormullion

If there is some data that I want to refer to throughout a script - such as the path to a database - I store it in a global symbol/variable. In the past I've sometimes used the (CommonLisp?) convention of enclosing with asterisks - eg *db* - which doesn't look that great:



(set '*db* {/Users/me/misc/db})



but it's good to have some visual reminder that the symbol is global, and it's harder to type by accident.



Does everyone else use this? What other conventions are used for marking variable names as global? I'm not a fan of underscores... :_) Is an initial capital enough? Or is it better to avoid them altogether somehow?

rickyboy

#1
I also use the asterisk enclosing in my global names.  My reasons:



1. This is the convention in CL, and so having the same convention in newlisp makes it easier for me to go back and forth between newlisp and CL, and



2. Putting asterisks around something was the Usenet and Internet way of applying *emphasis* to some text in flat ASCII.



So the convention is well ingrained in me.  :-)



--Rick
(λx. x x) (λx. x x)

Jeff

#2
The other thing you can do is maintain state more safely in a context.  Call it 'global or even *global* ;)



I use asterisks for globals in any context, though.
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code

m i c h a e l

#3
What about the $ (dollar sign)? newLISP uses it for its global variables ($n and $idx, for example).


> (set (global '$db) {/Users/me/misc/db})
"/Users/me/misc/db"
> (context 'C)
C
C> (define (C:print) (println $db))
(lambda () (println $db))
C> (context MAIN)
> (C:print)
/Users/me/misc/db
"/Users/me/misc/db"
> _


Don't forget to make your globals global ;-)



m i c h a e l

Lutz

#4
Just put all globals in its own context 'global' or 'gl' or even shortening to capital 'G' for those who hate typing (like myself ;-))


(set 'G:database "/usr/home/joe/data/database.lsp")
(set 'G:n-config 12345)


A context qualified variable is global by definition, because contexts names are global. It also keeps all globals together in one place for inspection or serializing with


(save "appglobals.lsp" 'G)
 

lutz