Naming for global symbols?

For the Compleat Fan
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Naming for global symbols?

Post by 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
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Post by rickyboy »

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
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

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...

Artful code

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Post by m i c h a e l »

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

Code: Select all

> (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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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

Code: Select all

(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

Code: Select all

(save "appglobals.lsp" 'G)

lutz

Locked