命名类型和实例

发布于 2024-10-12 23:09:22 字数 305 浏览 5 评论 0原文

我正在尝试清理我正在创建的游戏的代码。从一开始,我就将相关函数放在一个覆盖对象中。例如,可以通过 Init.initialize() 访问初始化函数。

但是,对于这个对象和其他对象,我想使用类型,然后创建它们的实例。例如,我有一个设置类型,其中包含游戏的一些设置。设置应该是一种类型,而实例应该保存实际的设置值。

现在,我应该如何最好地命名类型,以及如何命名实例,特别是当我只有一个实例时?如果我将类型命名为“Settings”,我怎样才能以变量名的形式最好地描述实例?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦太阳 2024-10-19 23:09:22

最好的命名约定是六个月查看代码后仍可阅读的命名约定。

在许多情况下,除非您记住您的意思,否则匈牙利表示法没有意义。

对于设置对象,请调用原型对象

SettingsObject

并调用实例

settings

,因为您(以及阅读代码的其他人)将了解 SettingsObject 实例将包含什么内容,并知道 settings 变量包含什么内容。如果您需要更长的名称,请使用更长的名称,在开发期间不会因描述性而受到惩罚。

IE。 UnitMouseClickListener 或任何其他有意义的名称。

开发完成后,您始终可以返回并缩小生产代码,但这只是在您解决了所有问题之后。或许在您编写代码时能够读取代码。

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.

浪漫人生路 2024-10-19 23:09:22

最常见的方法是以大写字母开头的类或类型的名称,以及以小写字母开头的用于存储对实例化类的引用的变量。
因此,您的类和实例名称将是这样的:

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

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

这完全取决于您的个人喜好。如果您更喜欢更标准化的命名约定,则可以使用匈牙利表示法,或者自行编写。关键是要保持一致。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文