newLISP Fan Club

Forum => Anything else we might add? => Topic started by: cormullion on October 27, 2007, 06:25:17 AM

Title: Naming for global symbols?
Post by: cormullion on October 27, 2007, 06:25:17 AM
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?
Title:
Post by: rickyboy on October 27, 2007, 07:05:07 AM
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
Title:
Post by: Jeff on October 27, 2007, 07:14:41 AM
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.
Title:
Post by: m i c h a e l on October 27, 2007, 08:00:14 AM
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
Title:
Post by: Lutz on October 27, 2007, 08:40:54 AM
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