views:

151

answers:

6

Which one is more acceptable (best-practice)?:

namespace NP
   public static class IO
   public static class Xml
   ...
   // extension methods

using NP;

IO.GetAvailableResources ();

vs

public static class NP
   public static class IO
   public static class Xml
   ...
   // extension methods

NP.IO.GetAvailableResources ();

Also for #2, the code size is managed by having partial classes so each nested class can be in a separate file, same for extension methods (except that there is no nested class for them)

I prefer #2, for a couple of reasons like being able to use type names that are already commonly used, like IO, that I don't want to replace or collide.

Which one do you prefer? Any pros and cons for each? What's the best practice for this case?

EDIT: Also would there be a performance difference between the two?

A: 

I think you should avoid exposed (public) nested classes and interfaces, or at least that is what Microsoft FxCop would say. Thus, the first one is better.

Edit: (yes, changed to the first one, i shouldn't reply in SO when i'm dead tired)

M.A. Hanin
Don't you mean the first one? The second one is nested classes.
RandomBen
I assume you mean the first one ;)
Thomas Levesque
The first one! indeed, sorry :-) +1 from me as well :-P
M.A. Hanin
+2  A: 

I prefer #1 because it does not require me to call through 1 class to get to another. I think it makes things a bit confusing because in general objects are meant to have member classes, methods, and such that deal directly with the objects made by instantiating the class. That means you are not really following the principles of OOP. Microsoft also says #1 is best practice.

RandomBen
+2  A: 

I would say #1. Because when you bundle up lots of classes into one static class you do the same thing as a namespace is ment for. Which is why I would say it is best to let namespaces do that for you.

In that case you can also get rid of having to write "NP" in front of everything by adding using in case you want. I think you should either nest your namespaces so that they wont collide our use more describing namespace names than IO.

Most often the best practice is what Microsoft do and I have never seen them to #2

Oskar Kjellin
+1  A: 

I've never seen your number 2 used. It reminds me of VB6 Modules.

John Saunders
+1  A: 

The whole point of namespaces is that they are used to organise your code:

Namespaces are heavily used within C# programs in two ways. Firstly, the .NET Framework classes use namespaces to organize its many classes. Secondly, declaring your own namespaces can help control the scope of class and method names in larger programming projects.

When you look at the .NET framework itself, you can see that virtually everything is structured like your first example (namespaces), and virtually nothing is structured like your second example (nested types). The rare occasions that nested types are used are when they are so closely tied to the outer type that they are essentially part of it, but need to be a separate class for reasons such as code re-use.

So if by "best practice" you mean "using things for the purpose they were designed", "most like the .NET framework" and "corresponding most closely to .NET design paradigms" then there is no contest; use namespaces for organisation, not nested types.

Regarding your edit - no, there is no performance difference that will matter in the real world.

Greg Beech
+1  A: 

I prefer #1 and let namespaces do it. Like others have said, it's very confusing to call into classes to get to other classes.

In addition, when you're doing more complex things like reflection or using something like a provider model, having nested classes being your actual provider section or the target you're attempting to reach becomes hairy.

Lastly, testing nested classes and interfaces make testing more difficult.

JamesEggers