Multiply Nested Classes

The concept of NestedClass can be applied to more than two levels. When should it?

CeePlusPlus example:

class A
    {
    class B
        {
        class C
            {
            };
        };
    };


When is it appropriate to have classes nested to multiple (3 or more) levels?

That really seems over the top. Stick to the ZeroOneInfinityRule.

I'm working with a developer who has chosen to do this. We need to implement containers of containers (lists of lists, an implementation detail that doesn't need to be exposed to the user of the component), and his design does it with multiply nested classes (similar to the example above).

For some reason this rubs me the wrong way; I don't see the need to introduce the mental/conceptual complexity of four levels of nested classes just to implement these nested containers. It doesn't clearly feel like it is driven by the problem, or the structure of the data. I think all of the container classes should be nested classes at the same level, inside the overall component class...

class A
    {
    class B
        {
        };
    class C
        {
        };
    };

I don't rule out the possibility of ever needing to have multiply nested classes, but I think it would have to be a pretty unusual situation, and I can't conceive of such an example. To me there would have to be some clear benefit to balance against the added conceptual complexity of such a design...

I think my coworker is overusing the concept of nested classes, applying it unnecessarily.

-- Anonymous


See also UsageOfInnerClasses.

EditText of this page (last edited December 28, 2004) or FindPage with title or text search