views:

134

answers:

1

In a framework, should class dependencies run down a namespace (root to leaf) or up a namespace (think of it as an inverted tree with the root at the top)?

I'm having a hard time describing this, so let's look at an example:

namespace foo
{
  class snafu : fubar {}
}

namespace foo.bar
{
  class tarfu : snafu {}

  class fubar {}
}

In this case, class tarfu has a base class dependency on snafu. The dependency walks up the namespace tree. Class snafu has a base class dependency on fubar. The dependency walks down the namespace tree.

Should dependencies only walk up the namespace tree? Or should it be the reverse? Why should it matter?

I'm developing in .NET, but if its different for other platforms I'd be interested in learning about those as well.

+2  A: 

Since you are using .NET I would strongly recommend that you match the convention of the .NET framework. This will keep your code consistent with the framework.

So this means that you ought to do this:

namespace First
{
    class Parent { }
}

namespace First.Second
{
    class Child : First.Parent { }
}

And just to clarify, it doesn't really matter what you do as the C# compiler simply appends all namespaces to the type name to create a longer type that is more likely to be unique. That is why I think it is best to avoid confusion and stick with convention.

Andrew Hare