views:

78

answers:

3

Let's say I'm trying to implement a Chess Game.

I'd create my Chess classes in a Chess namespace. Now, should I prefix all my Chess related classes with the Chess word, or as they are already inside that namespace, should I just call them by what they are?

Example:

ChessGame vs Game
ChessPiece vs Piece
+4  A: 

Adding a prefix that matches the containing namespace is a waste of time and provides no real benefit. Just go with Game, Piece, etc.

Marcelo Cantos
That's what I thought. But what if I have other namespaces with different games and I have a GUI where I have several games side by side or something? I'll get name clash. Or is that a too contrived example?
devoured elysium
In that case, you'll have Chess::Game and Chess::Piece etc. unless you do something stupid like 'using namespace Chess;'.
cHao
@devoured: The whole point of namespaces is to avoid name clashes. You can resolve ambiguities by explicitly scoping names, e.g., `Chess.Game` and `Chess.Piece`.
Marcelo Cantos
+1  A: 

In java the domain namespace is the paquetage.

"name of a class" is : name of a paquage + an identifier.

In short, the identifier of the class is the name of the class, but it's a human convention. The name of the class is always paquage+identifier.

For anonyme class, java provide an identifier.

doing :

a.b.c.Foo foo = new a.b.c.Foo();

is equivalent to

import a.b.c.Foo;

...

Foo foo = new Foo();

How to name paquage / domain space ? With your name of your domain :-), somtimes with "ing" as suffix (java.util.logging).

You can also simulate some domain namespaces with the nested class (inner static class).

Istao
What's a paquetage (or paquage)?
Marcelo Cantos
The english term is Package, sorry :-) Paquetage is package in an other langage, paquage is my invention, I suppose.See "Learning the Java Language" at http://java.sun.com/docs/books/tutorial/java/index.html, Chapter "Packages".
Istao
+1  A: 

Suppose you had a language without any namespaces or packages (in some languages like Java the package is program module and namespace at the same time...). To avoid name clashes, you would use the prefixes. But you have to take care to name properly all program elements and write some characters every time you introduce something new. So the solution would be to introduce namespace into the language, construct which will automatically prefix each element with some name, which you can specify at one place (avoiding typos). Now as long as the namespace names do not class, you are safe. Namespaces allowed to specify the prefix on one place, so you can easily use longer names and therefore avoid the clash without having to type more each time. Look for example how the packages of some libraries are named - they use namespace uri (e.g. country_abbreviation.compay_name.product_name.program_module). However writing the whole identifier names, with package name, tend to be tedious, so many languages invented constructs which allow to use names from namespace as if you used them right in theri namespace (e.g. using, import etc.). But this gets us back to the original situation. The solution is to avoid use of implicit namespaces for program elements, which might change, as in your situation. So the solution you want is to use namespaces for you different games, so you can specify in the GUI component which exact Game element you mean by providing the full name with the package name.

Gabriel Ščerbák