Main Content

Separating groups of variables into namespaces

Archive - Originally posted on "The Horse's Mouth" - 2010-11-24 20:02:53 - Graham Ellis

If your program's going to grow in size beyond a certain point, you'll need to use some sort of namespace or package to segment global names into different groupings - moving from a single name to a forename / surname type system, if you like to think of it that way.

In C++, it's namespaces - see [here] for a source code example. In Perl, we use packages - see [here] and [here] (called from [here]). Both of those examples are not really object oriented, although both Perl and C++ are OO languages and use this namespace / package capability within their OO implementation.

There's an example of Tcl's namespaces [here], and in PHP [here] ...

You'll find that many of my simple namespace demos are rather less that useful programs, and you'll ask yourself "why's he using namespaces here?". The answer is that they're purely used for illustrative purposes in these cases - name spaces become useful as your program moves from big to massive, or where you're packing substantial chunks of code for others to use and you want to expose an absolute minimum of internals to them. And of course it would take a very long time on a course to develop something that was substantial enough to make namespaces worthwhile, only to end up with an example that was too cluttered to show you the features easily without them getting lost in the rest of the thing!