-2
\$\begingroup\$

I'm writing a basic RPG at the moment, with characters, races, classes, and the characters evolve on maps, naturally.

How to go around the default names that exist in java.lang and java.util. I'm having a hard time trying to find equivalent names to the following RPG concepts that do not clash with Java basic utilities. Typically, the words Character, Class and Map cause me trouble. For instance this is how typically clashes would occur:

// Line 322, well below the imports
char[] characters = new String("foo").toCharArray();
Character character = new Character();
Map map = getMap();
Class heroClass = getDefaultClass();
// do stuff

In this example, it's hard to tell if Character is java.lang.Character or mygame.engine.Character. What type is Map? Can anyone answer without seeing the imports?

I could use the fully qualified name:

java.lang.Character c = new String("foo").toCharArray()[0];
mygame.engine.Character character = new mygame.engine.Character();
java.util.Map map = getMap();
mygame.engine.Class heroClass = getDefaultClass();
// Do stuff.

But in the long run, and from experience, this is barely readable.

So I'd like to know what are usual techniques and/or synonyms that people use to both avoid confusion and unreadable code.

I have personally thought of using the prefix Game, which will lead to using Game a lot of times: GameClass, GameCharacter, GameMap. I think this is meh, but I can get over with it if people say it's not so unusual.

I'm not asking opinions: I'm looking for alternatives, such as which synonyms are used and are clear enough, or other techniques that don't decrease the readability of the code.

\$\endgroup\$
13
  • 1
    \$\begingroup\$ In java you can name the classes whatever you want except for the reserved keywords. There should not be any conflucts, if you import the correct packages. public class Class is no problem \$\endgroup\$
    – Raildex
    Oct 6, 2016 at 12:48
  • 4
    \$\begingroup\$ Put your classes your own packages. This is what these are for.... \$\endgroup\$
    – Vaillancourt
    Oct 6, 2016 at 12:49
  • 1
    \$\begingroup\$ I would probably prefix Game onto the start of my classes. GameMap, GameCharacter. or if you have a name or codename for your game prefix that. If your game was called Skyrim; SkyrimMap and SkyrimCharacter could make nice class names. Also for the record its only for your own use, you could name the abc or xyz for what its worth. I personally would avoid using custom class names that look close to language stuff, for example at work we have custom namespace (package) names that are the same as .NET namespaces, it can get very confusing and leads to you qualifying the full name. \$\endgroup\$
    – lozzajp
    Oct 6, 2016 at 13:12
  • 1
    \$\begingroup\$ @lozzajp You might want to add this as an answer. \$\endgroup\$ Oct 6, 2016 at 13:22
  • 1
    \$\begingroup\$ Character -> Actor, Class -> Profession or Specialization. I'm not really sure what you're describing as a map. Like a paper map with locations? Perhaps you could use 'Zone'. If it's like an actual map, then I would probably say 'GameMap' is sufficient, as it would be a 'subclass'. \$\endgroup\$
    – ndenarodev
    Oct 6, 2016 at 14:39

2 Answers 2

5
\$\begingroup\$

I would probably prefix Game onto the start of my classes. GameMap, GameCharacter. or if you have a name or codename for your game prefix that. If your game was called Skyrim; SkyrimMap and SkyrimCharacter could make nice class names.

Also if its only for your own use, you could name the abc or xyz for what its worth.

I personally would avoid using custom class names that look close to language stuff, for example at work we have custom namespace (package) names that are the same as .NET namespaces, it can get very confusing and leads to you qualifying the full name

Hope that helps.

P.S. alternatively as others suggested just use the names but in your own package, so it all looks like Game.Character, Game.Class etc.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ The convention GamenameClass gets awkward when you program a (spiritual) successor with a different name and want to reuse code. If Bethesda would have followed that convention, the class for characters in Skyrim would likely be called OblivionClass or even MorrowindClass. It can also lead to Smurf Naming Convention. \$\endgroup\$
    – Philipp
    Oct 6, 2016 at 13:44
  • \$\begingroup\$ Well, TESClass would be more appropriate then, but the point is nice. All I previously thought was GameClass where <real-game-name>Class is better. \$\endgroup\$ Oct 6, 2016 at 13:48
  • \$\begingroup\$ @Philipp I have heard of people using codenames in development source code for reasons as this. The game name may change just days before launch plans start happening and announcements. So just a codename is given and stuck to by everyone to avoid confusions etc. Very good point about the successors though. \$\endgroup\$
    – lozzajp
    Oct 6, 2016 at 13:57
  • \$\begingroup\$ A shorter addition than 'Game' I sometimes use is 'My' e.g. 'MyTextDraw'. I work alone and so it is at least clear to me. I also use words like 'Enemy' rather than 'character'. You could use other synonyms such as 'Plan' for 'Map'. I would avoid using the name of the game because it makes it much harder to re-use and compare different games. \$\endgroup\$ Oct 7, 2016 at 17:07
0
\$\begingroup\$

I strongly advice to use java analog of namespaces.

They are called packages.

You can read about packages there:

http://docstore.mik.ua/orelly/java-ent/jnut/ch02_11.htm

\$\endgroup\$
2
  • \$\begingroup\$ Thanks, but you didn't read the whole question. Please read the last two paragraphs of my question to see why your answer is not useful at all. Also, on a side note, I have 18 years of experience in Java: it's only the first time I'm coding a RPG. I only have difficulties with naming in this very precise context. I don't have any trouble with Java concepts. \$\endgroup\$ Oct 6, 2016 at 13:28
  • \$\begingroup\$ Yeah, I got your point. \$\endgroup\$
    – PaulD
    Oct 6, 2016 at 13:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .