Giter VIP home page Giter VIP logo

Comments (2)

initializedd avatar initializedd commented on July 22, 2024

We are less concerned with low-level issues, such as naming conventions and indentation style. However, no topic that can help a programmer is out of bounds.

In my opinion the indentation style that one chooses regarding namespaces, largely effects the answer for how many nested namespaces would be deemed as inappropriate. I say this because in the scenario where we use as much indentation as possible (assuming a tab width of 4 spaces), it suddenly becomes apparent that things are going to get messy pretty fast.

Example 3A:

This approach is an example using as much indentation as possible.
One benefit here is that it is clear which members are part of each namespace.
One drawback here is that this pattern implies there is a much stricter upper restriction on the amount of nested namespaces deemed appropriate, (since meeting the 80 or 120 horizontal character limit employed by most programmers suddenly becomes incredibly easy at this level of indentation).

namespace a {
    void foo();
    namespace b {
        void foo();
        namespace c {
            void foo();
            namespace d {
                void foo();
            } // namespace d
        } // namespace c
    } // namespace b
} // namespace a

Example 3B:

This approach is an example using indented members without indentation for subsequent nested namespaces.
One benefit here is that we are no longer limited by indentation at all for the amount of nested namespaces deemed appropriate.
One drawback here is that it could become difficult to distinguish which namespace you are currently inside when you are dealing with a lot of code, (but this shows the importance of trailing comments after a namespace).

namespace a {
    void foo();
namespace b {
    void foo();
namespace c {
    void foo();
namespace d {
    void foo();
} // namespace d
} // namespace c
} // namespace b
} // namespace a

Example 3C:

This approach is an example using indented subsequent nested namespaces without indentation for namespace members.
One benefit here is that we can still easily distinguish namespace members and their respective namespaces.
One drawback here is that it is still restrictive to the amount of nested namespaces, although this is not as restrictive as Example 3A.

namespace a {
void foo();
    namespace b {
    void foo();
        namespace c {
        void foo();
            namespace d {
            void foo();
            } // namespace d
        } // namespace c
    } // namespace b
} // namespace a

Example 3D:

This approach is an example using no indentation.
I don't see any benefits that this has over Example 3B.
Perhaps some people are a fan of no indentation though?

namespace a {
void foo();
namespace b {
void foo();
namespace c {
void foo();
namespace d {
void foo();
} // namespace d
} // namespace c
} // namespace b
} // namespace a

What is my personal favorite?

I think Example 3B is the clear winner for me. This is of course assuming that we are ending all of our namespaces with trailing comments for extra clarity. But I can totally see reasons for people still wanting to put restrictions on the amount of nested namespaces deemed appropriate, and for people preferring different indentation styles which may implicitly effect that too. Interested to hear your thoughts!

from cppcoreguidelines.

hsutter avatar hsutter commented on July 22, 2024

Editors call:

For nesting: We don't have a hard recommendation for namespace nesting. We might have a guideline for this later but are not ready to add one now.

For formatting: We don't recommend stylistic conventions for whitespace formatting. However, note that you can declare a deeply nested namespace by writing namespace a::b::c::d.

from cppcoreguidelines.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.