Namespace Qualification [was Re: Linux compiling]

From: Anthony Borla (ajborla_at_bigpond.com)
Date: 12/06/04


Date: Mon, 06 Dec 2004 13:51:02 GMT


"jafar" <nomorev14gra@idontlike.spam> wrote in message
news:pan.2004.12.03.20.18.49.873364@idontlike.spam...
> I demand that on Fri, 03 Dec 2004 08:04:24 -0800, James Dennett
> may or may not have written:
>
> > There's really no way I'd make a blanket statement like that
> > it's "even better" to have namespace directives -- they're
> > something of a hammer. They have their place, but be cautious,
> > they can also lead to subtle problems which are absent when
> > explicit qualification is used.
>
> Sorry if it was a bothersome statement ;) It's just the way I
> have been taught.
>

It is customary in introductory-level courses and text books to make
'simplifying assumptions' in order to reduce the amount of new information a
student needs to absorb.

So by making use of:

    using namespace std;

in place of individial namespace qualification such as:

    std::cout << ...

in sample code a teacher is able to postpone teaching the concept of
namespaces, and ensure students remain focused on other aspects of the code.

Whilst this practice simplifies teaching, or helps improve its effectiveness
[at least in introcuctory-level situations], does not necessarily mean that
it is a practice that is followed in professional development circles, or,
for that matter, in more advanced-level courses.

>
> I find it easier on the eyes and if you have 50,000 lines of
> code, it would save a lot of time if 10,000 or so lines of
> that code needed you to type std:: all the time. :)
>

I'd rather use a search or search / replace facility on 50,000 lines of code
than scroll through it - *much* easier on the eyes ;) !

>
> I haven't come across any problems so far using namespace
> directives, but I'm sure I'll learn a good lesson if/when I do.
>

I dare say you've not run into any namespace-related problems because you've
probably only been working on relatively small applications, or on
applications which don't use many different libraries. In such scenarios the
probability of 'name collisions', the problem the namespace facility has
been designed to help resolve, is rather low.

I'd like to leave you with a couple of thoughts:

* It's important to be aware of what constintutes a 'safe'
   practice even if it isn't necessarily followed for one reason
   or other. The other posters were attempting, I think, to
   make you aware of such namespace usage

* The danger of 'name collisions', and the provision of a
   'namespace' facility [in one form or other] goes well
   beyond the C++ language.

   For example, if you've ever written multi-table SQL
   queries you'll be aware that join attributes [i.e. attribute
   (field) name appearing in each of the query tables used
   to temporarily link the tables] are qualified with the table
   name so they may be distinguished e.g.
       ...
       where tableX.name = tableY.name
       ...

   Whilst different to the C++ facility, this is a namespace usage
   example [of sorts]. Another example, one more like the C++
   facility, may be found in XML code [i.e. an HTML-like language
   that allows for the creation of custom tags]. An XML document
   may, for example, contain references to the titles of books, CD's
   and DVD's, each of which is described by a <title> tag.

   In order to allow use of the same tag name to identify different
   item types, a namespace facility is used, thus allowing tag use:

       <book:title> ... </book:title>
       <cd:title> ... </cd:title>
       <dvd:title> ... </dvd:title>

   Without this facility such references wouldn't be possible

It is often well worth gaining a more conceptual understanding of mechanisms
/ facilities in order to appreciate their wider applicability.

I hope this helps.

Anthony Borla



Relevant Pages

  • Re: namespace problem
    ... > Hang on - is Mapping a class as well as a namespace, ... Mapping is a namespace of b2b.Server.Business and Facility is a ... with using declarations it shouldn't be a problem - just ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Random number generator in c# - again
    ... System.random namespace ... I think it also has facility to generate random numbers in integer, ... Shivprasad Koirala ... Prev by Date: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Union nested in structure.
    ... specifying the tag scope rules would have been a Bad Thing. ... (And, IIRC, the single global-scope tag namespace existed in pre- ... > structures, unions and enums? ... case when you're defining one struct inside another; ...
    (comp.lang.c)
  • Web part question in 2007
    ... try to tag the property to register it as a Sharepoint property. ... System.Web namespace I get an "Unable ... to import web part" error when I try to register the WP in Sharepoint. ...
    (microsoft.public.sharepoint.portalserver.development)
  • Re: XML namespace / element name question
    ... > It seems that two sibling elements, which are in the same namespace, may ... > not have the same name (tag name). ... ThinFlash: Linux thin-client on USB key drive ...
    (comp.text.xml)