views:

133

answers:

3

The reason I ask this is that c# could easily have copied the java convention, or a variation of it, but opted for the more flexible approach of explicitly declaring namespaces inside files. As a Java programmer often there are things that I wish I could do differently, but namespaces is not one of them.

The flexbility has a certain overhead (extra braces, extra decisions for developers, making it harder to view a projects contributions to the namespace, at least without a specialist IDE). So what practical examples are there when this flexiblity is advantageous?

edit: To clarify, the point is how classes declare themselves within namespaces and not how to import/reference classes from other namespaces.

A: 

From code design view namespace is same as package. Good style in Java is to put classes into packages based on their roles in your application. It increases readability and maintainability of code. From this point of view namespaces are the same. But namespace doesn't force you to put classes in different folders.

Andrey
package* :) :) :)
Midhat
Yes, but when/what is the advantage of not using the good style?
mike g
+1  A: 

It could be useful in generated code. Sometimes code generation may wish to create a single source file for multiple classes - and they may be in different namespaces.

Jon Skeet
+1 I was just typing this up, but for human generated code such as when you have several very small classes (e.g. config section handlers) and it doesnt make sense to create a half dozen tiny files for something that is effectively one unit in the code.
GrayWizardx
Several small classes that belong in different namespaces?For generated code, it could be convenient, but I think to a degree the same cost is paid. If 100 classes are coming from a single file it is harder to comprehend this fact by glancing at the files of a project.
mike g
@mike g: I was thinking particularly of Protocol Buffers; you *almost* want to regard the generated code as a separate assembly - except that you might want to use the fact that it generates partial classes, and you might want the classes to be internal.
Jon Skeet
+1  A: 

A problem I see with the Java convention is there is no way to specify multiple imports of the same classname in different packages. So suppose you have two classes like...

com.yourcompany.blah.blah.verylong.blah.blah.FantasticClass
com.someothercompany.blah.blah.also.very.lengthy.blah.blah.FantasticClass

...inside of one Java file, you can use import on just one of them. If your code needs to use both classes, then you'll have to write variable declarations for one of them with the full package name. That means you end up with cumbersome code like...

com.someothercompany.blah.blah.also.very.lengthy.blah.blah.FantasticClass = new com.someothercompany.blah.blah.also.very.lengthy.blah.blah.FantasticClass();

...or weird refactoring exercises to avoid it.

Other than that hiccough, which could be fixed pretty easily with a new language feature like "import class alias class2", I prefer the simplicity of the Java approach.

Erik Hermansen
I agree, but i think this is not a flaw in namespaces by convention of layout. It could be overcome by allowing aliases in the imports, or another system, or allowing namespaces for usage, but not declaration.
mike g