In what situation is a C ++ / C # namespace approach better than a Java approach?

The reason I'm asking about this is because C # can easily copy the java convention or its variant, but chose a more flexible approach for explicitly declaring namespaces inside files. As a Java programmer, there are often things that I would like to do differently, but namespaces are not one of them.

Flexibility has certain overheads (additional bindings, additional solutions for developers, which makes it difficult to view the contributions of projects to the namespace, at least without a special IDE). So, what practical examples exist when this flexibility is beneficial?

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

+4
source share
3 answers

This may be useful in the generated code. Sometimes code generation may require creating a single source file for several classes - and they can be in different namespaces.

+1
source

The problem that I see in the Java convention is that there is no way to specify multiple imports of the same class name in different packages. Suppose you have two classes, for example ...

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

... inside a single Java file, you can use import for only one of them. If your code needs to use both classes, you will have to write variable declarations for one of them with the full package name. That means you get 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 this.

Besides this hiccup, which can be pretty easily fixed with a new language function like "class class alias class2", I prefer the simplicity of the Java approach.

+1
source

From the namespace in the code design view is the same as the package. A good style in Java is placing classes in packages based on their roles in your application. This improves readability and usability of the code. From this point of view, the namespaces are the same. But the namespace does not force you to put classes in different folders.

0
source

Source: https://habr.com/ru/post/1304382/


All Articles