命名约定(单数或复数名称类) [英] Naming convention (Singular or plural name class)

查看:271
本文介绍了命名约定(单数或复数名称类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模拟商店的。所以它具有以下属性:

I have a class that modelizes a Shop. So it has the following attributes:


  • name

  • description

  • 地址

  • 电话

  • ...等。

  • name
  • description
  • address
  • phone
  • ... etc.

我是否必须将其命名为 ShopInfo ShopInfos

Should I have to name it ShopInfo or ShopInfos?

指向命名约定?

推荐答案

如果你有一个类的对象,它只是信息一个商店。
该对象描述了一个商店信息。
以这种方式编写类名是很常见的,它在使用对象时有意义

If you have an object of the class, it is only the information for one shop. The object describes one shop info. It is common use to write a class name that way, that it makes sense when using an object.

特别是在方法中名称和声明变量更清楚

Especially in method names and declaring variables it is clearer

// CORRECT WAY
- (UIAlertView *)calcualteSomething:(UIView I)view {
    UIView *newView = view;
    ...
    return [UIAlertView alertWith...];
}

在我看来使用复数形式会很奇怪(似乎就像单个对象不止一个!):

It would be very odd to use the plural form in my opinion (it seems like the single object is more than one!):

// BAD IDEA
- (UIAlertViews *)calcualteSomething:(UIViews *)view {
    UIViews *newView = view;
    ...
    return [UIAlertViews alertWith...];
}

然后你就可以轻松地使用该类的对象或测试,这是有道理的也是:

You can then easily use the object or test for the class and it makes sense too:

[myObject isKindOfClass:[NSString class]]

以下是您应该如何写一个名字的官方链接:

Here is the official link how you should write a name:

https://developer.apple。 com / library / mac / documentation / Cocoa / Conceptual / CodingGuidelines / Articles / NamingBasics.html#// apple_ref / doc / uid / 20001281-BBCHBFAH

在那里,你看,Apple总是避免使用复数形式(NSString,NSArray等)。

And there you see, Apple always avoids using the plural form (NSString, NSArray, etc.).

当然其他绕过它的方式也很有意义,但更多的是用于类方法等。其他流行语言也使用单数表格。

其他一些好的编码指南来自 CocoaDevCentral

Some other good coding guidelines is from CocoaDevCentral

http:// cocoadevcentral .com / articles / 000082.php

http://cocoadevcentral.com/articles/000083.php

这篇关于命名约定(单数或复数名称类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆