I was reviewing the MSDN guidelines on naming your namespaces, which hints at the solution to an issue I see a lot, namely “in which namespace do I put non-specific members”? If you’re wondering what I mean by that, take a look through your own hierarchy of namespaces and look for any that end .Common, .General, .Generic, .Core, .Universal, .Wherethehelldoesthisgo etc.
The MSDN guidelines state “A nested namespace should have a dependency on types in the containing namespace”, so to turn this on its head, members which are used throughout many nested namespaces should go in the parent. To give an example, say you’ve got some classes to deal with accountancy business rules in these namespaces:
- Mycompany.Accounts.Payroll
- Mycompany.Accounts.Invoicing
- Mycompany.Accounts.Receipts
and you’ve got some helper methods in a static class which help you work out taxes, which would potentially be used across all three of these namespaces, don’t put them in
- Mycompany.Accounts.Hmmnotsureaboutthislot
instead put them in
- Mycompany.Accounts
You’ll find your namespace hierarchy is much cleaner and suddenly makes a whole lot more sense.