Adding namespace after the fact

Discussion in 'C++' started by snorble@hotmail.com, Sep 12, 2012.

  1. Guest

    Is it better to build code in a namespace from the start? Or is it relatively easy to place it into a namespace after the fact (just put all of the code between the namespace foo { ... })?
     
    , Sep 12, 2012
    #1
    1. Advertising

  2. wrote:
    > Is it better to build code in a namespace from the start? Or is it
    > relatively easy to place it into a namespace after the fact (just
    > put all of the code between the namespace foo { ... })?


    When you declare something inside a namespace, such as for example:

    namespace Foo
    {
    void bar();
    }

    you can then implement that by using the namespace qualifier instead of
    having to surround your code with a namespace block. In other words, you
    can do this:

    void Foo::bar()
    {
    // implementation here
    }

    This can be practical, especially if you want to be strict about indenting
    (or are using an editor that autoindents), as it reduces the amount of
    indentation of your code.
     
    Juha Nieminen, Sep 12, 2012
    #2
    1. Advertising

  3. Öö Tiib Guest

    On Wednesday, September 12, 2012 5:28:39 AM UTC+3, (unknown) wrote:
    > Is it better to build code in a namespace from the start? Or is it relatively easy to place it into a namespace after the fact (just put all of the code between the namespace foo { ... })?


    Namespaces provide way to systematically qualify the names with
    additional context, sub-context etc. Within context then you can write
    the code using very short (unqualified) names for things in same
    context. There are lot of ways how to import, alias and typedef names
    from other contexts to keep everything short to the point within a
    context. As result using namespaces systematically from scratch your
    code is short and elegant.

    When you write without namespaces first (with maybe add them later) then
    you can not have names that make sense only within small context (like
    "filter", "loader", "access", "range", "logic"). That very likely does
    not even compile to use so common names in global namespace. Therefore
    you qualify the names immediately somewhat anyway like
    "network_service_loader". Later you add namespaces and get abominations
    like "device_network::service::network_service_loader". The whole point
    of having the namespaces in language was actually lost as result. It
    will feel like converting from C to C++ by replacing 'malloc' with 'new'
    and 'struct' with 'class' pointlessly.
     
    Öö Tiib, Sep 12, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. comcast
    Replies:
    5
    Views:
    417
    giganews
    Mar 3, 2004
  2. Cirene
    Replies:
    5
    Views:
    584
    Cowboy \(Gregory A. Beamer\)
    Apr 30, 2008
  3. Jonathan Wood

    AJAX "After the Fact"

    Jonathan Wood, May 13, 2008, in forum: ASP .Net
    Replies:
    11
    Views:
    531
    BennySutton
    May 17, 2008
  4. dhyams
    Replies:
    6
    Views:
    431
    Steven D'Aprano
    Nov 8, 2011
  5. Kyle Schmitt

    Installing tk after the fact?

    Kyle Schmitt, Apr 23, 2008, in forum: Ruby
    Replies:
    3
    Views:
    97
    Kyle Schmitt
    Apr 23, 2008
Loading...

Share This Page