3

I'm trying to clean up the code of a game I'm creating. From the beginning, I have put relevant functions inside a covering object. For example, the initialize function can be accessed at Init.initialize().

However, for this and other objects, I'd like to use types and then creating instances of them. For example, I have a Settings type which holds some settings of the game. The Settings should be a type, while an instance should hold the actual setting values.

Now how should I best name the type and how should I then name the instance, especially when I only have one instance? If I name the type Settings, how could I best describe the instance in the form of a variable name?

pimvdb
  • 151,816
  • 78
  • 307
  • 352

2 Answers2

2

The most common way would be to start the name of your class, or type, with a capital letter, and your variable that will store the reference to the instantiated class, in lower letter. So your class and instance names would be something like this:

// your class name
function Settings() {
  this.initialized = false;
}
Settings.prototype.initialize = function() {
  this.initialized = true;
}

var settings = new Settings();
settings.initialize();

It's all about your personal preference. You can use Hungarian Notation if you prefer having a more standardized naming convention, or make up your own. The key is to be consistent.

ArtBIT
  • 3,931
  • 28
  • 39
  • Just wondering, you use both `settings` and `setting`, and `new Setting` and `Settings` - is that a typo? – pimvdb Jan 20 '11 at 21:43
  • Ok, thanks. However, if only a uppercase/lowercase makes the difference, I do not really prefer this way. Also, as far as I know, some languages are case-insensitive. – pimvdb Jan 20 '11 at 21:47
  • Oh, sorry. I thought you were talking about javascript specifically, since that's what tags were saying. Well, you can then go with Hungarian Notation like I suggested, and prefix your classes with 'cls_' or something. – ArtBIT Jan 20 '11 at 21:50
  • @pimvdb: Yes, but JavaScript is not. – Felix Kling Jan 20 '11 at 21:50
  • Yes I'm sorry. This case is in JavaScript, but when I'm writing in another language I'd like to be able to use consistent naming rules. Anyway, thanks for the Hungarian Notation link. – pimvdb Jan 20 '11 at 21:52
  • Hungarian Notation is rarely the solution you want. It was originally created to be descriptive of the object's function, not its type. – zzzzBov Jan 20 '11 at 22:00
1

The best naming convention is one that can be read after six months of not looking at the code.

In many cases hungarian notation doesn't make sense unless you remember what you meant.

For the settings object, call the prototypal object

SettingsObject

and call the instances

settings

because you (and anyone else reading the code) will understand what an instance of SettingsObject will contain, and know what the settings variable holds. If you need a longer name, use a longer name, there's no penalty for being descriptive during development.

ie. UnitMouseClickListener or any other name that makes sense.

After development you can always go back and minify code for production, but that's only after you've ironed out all the kinks. Might as well be able to read your code while you're writing it.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • 1
    Thanks. Some types I'm using are fine without the Object part at the end of its name (e.g. Vector3D - it wouldn't be descriptive to use this as a variable name), so putting Object after only *some* type names would not be very consistent again. Naming convention is quite a difficult aspect of programming actually... – pimvdb Jan 20 '11 at 22:09