What is a namespace and why do we need them?
Archive - Originally posted on "The Horse's Mouth" - 2011-09-03 08:48:34 - Graham EllisWe name variables. We do it so that we can identify specific pieces of data by a key, rether than have to give some longer sort of identification description when referring to them. We don't refer to "the patriarch of the household" = we refer to Zeb. And we refer to Esther, John and Olivia, Jason Mary Erin Benjamin James and Elizabeth too. On occasions, we need to make compromises when a father is named after his son - such as referring to the son as John_Boy.

But what happens as our community grows? As we pull in code from elsewhere, we also find that we have the same names cropping up again. The next family added may have no repeats - Abi amd Jack, Dot and Jim, Lauren and Max, and Lily and Oscar. Bradley, Ronnie and Stacey. But then - look carefully - you start getting repeats. There's Dan and Grace, Phil and Jill, Kenton, Shula, David and Elizabeth, Lillian, Tony, Helen and Tom. And as the list grows, more and more possibilities of a repeat.

We can solve issues caused by this repetition of names by using family names - we've got the Brannigans, the Waltons and the Archers. We can identify each individual by forename and family name - Jason Walton, Jim Brannigan, Elizabeth Archer, Elizabeth Walton and with the extra qualification we know whether we're lookig at somewhere in Virginia, or the lady of Lower Loxley Hall.
Namespaces in Tcl (and packages in Perl) are like family names - we group whole sets of variables together within a namespace, and we can then refer to them by a fully qualified name which makes it clear exactly which of several of the same name we're referring too. Within an individual namespace / package area though we can refer to variable just by their name within the namespace, just as round the family table we'll talk to Jack or Jim or - in a different part of the country - David or Elizabeth.
Programming wise, the use of namespaces allows different developers to provide cde which can more easily be shared without the need (prior to coding) for an agreement to be reached as to who's going to use what name. And that becomes important as code grows, with major coding elements loadable from shared libraries having just a single (family) name brought into the parent namespace. On a course such as the Tcl course which I gave last week, examples tend to be quite short which means that the true significance of namespaces as programs grow has to be talked obout much more in theory than in practise, though we can (and do) do a practical exercise.
There's an example [here] from yesterday's course that shown namespaces. The code is all in one file (that's easier for a first demo than having multiple files to manage) and withn our namespace we've defined 3 procs to work out the area, circumference, and doggie distance of a rectangular table (the doggie distance is the maximum distance from the edge of the table that a treat can be place to keep it out of the dog's reach).
Namespaces are often used in association with packages. Technically, they're different facilities but in practise they're usually used together. There's a follow up article [here] introducing packages