Saturday, January 21, 2012

Namespace using Directives Best Practice: Inside or Outside?

I wanted to write a few lines regarding this topic. For most of my years coding in C# I have always put my using directives outside the namespace declaration. I have seen thousands of samples from developer across the world and from Microsoft official documentation and Microsoft employees blogs and I'm pretty sure almost all of them tend to have their using directives outside the namespace. I would say that that makes it the de-facto industry standard.

However when I moved to the UK I found developers actually were including their using directives inside the namespace declaration. At the beginning it seemed totally wrong and I was against it. When discussing the practice with them the argument was that source analysis tools like Re-sharper and StyleCop actually encourage you to move the using directives inside the namespace. I though it was a valid reason although I don't personally share it.

Here is a basic sample of using the directory outside the namespace:



sample of using the directory outside the namespace:




Then I started to realize that these developers were actually breaking the single-class per file best practice. I know that some people will argue about it and I know that there are developers that tend to include several related classes in the same file. Furthermore, these developers will tend to use a lot of namespace aliases and fall into bad interdependency practices.

It turns out that although there are subtle differences between having using directives inside or outside the namespace, there is no prove whatsoever that there are advantages from using one or another approach, it will fall to personal taste. If you use namespace aliases heavily and tend to have several classes in the same file, then it will be probably better in most cases to have your using directives inside the namespace. However, I would argue that it is actually a good practice at all. Class file names represent the class itself, so by having more than one class in a file you are actually making your code and project structure harder to read and maintain. I would also say that by enforcing the one-file-per-class rule you are also enforcing the single responsibility principle at least in a conceptual sense, which is helpful when implementing complex solutions.

There are people claiming that having the the using directory inside the namespace enforces assembly lazy loading, however the claim is false. Placing the using directive inside or outside the namespace doesn't have any effect on the assembly loading process. 

My conclusion is that there are no tangible benefits of having the using directive inside the namespace at all, and instead, it promotes confusing code that goes against the de-facto industry best practices. I'll stick to having my using directives outside the namespace and having one-class-per-file as I strongly believes it promotes cleaner, easier to maintain code, OO practices and better looking code structure.

Cheers!

No comments:

Post a Comment