开源项目的 Objective-C 类和函数命名前缀
在 Objective-C 中,通常在类名或 C 函数的开头添加两个或三个字母,例如,数组类被命名为 NSArray,因为它最初是由 Next Step 开发的。这可以避免命名空间冲突。现在,如果我要创建自己的库,通常会将我的首字母缩写前缀添加到我创建的新类的开头,例如,我可能会以我无限的智慧决定世界需要另一个数组类,而我可能会创建一个全新的称为 BGArray 的数组类。
所以现在我正在开发一个开源项目,该项目最初是由 Prince Witherham The Second(完全虚构的名字)开发的,他在项目中的所有类都以 PW2
为前缀。我想扩展该项目并向项目添加一些我自己的类,我应该如何命名这些新对象,我应该:
在新对象上添加
PW2
前缀:这样可以保持一致性跨项目,但可能会为 Prince 创建命名空间冲突,例如,如果我创建一个新的 PW2Foo 类,而 Prince 在他正在处理的其他项目中已经有一个 PW2Foo 类。在新对象上添加
在BG
(我的姓名缩写)前缀:这可以避免任何命名空间冲突,但会将我的姓名缩写放在我没有真正开始的东西上,而且还可能给使用未来的项目。问威瑟汉姆二世王子他更喜欢什么:这可能是最困难的,但我可以看到这是有问题的,如果王子不再积极维护该项目,因为他正在他的私人岛屿上度假他们并不总是有互联网连接。
In objective-C it is common to prefix two or three letters to the beginning of a class name or C function, for Example an array class is named NSArray
the NS because it was originally developed by Next Step. This avoids namespace collisions. Now if I was to go off and create my own library it's common to prefix my initials to the beginning of new classes I create, for example I might decide in my infinite wisdom that the world need another array class and I might create a brand new array class called a BGArray
.
So now I'm working on an open-source project that was originally developed by Prince Witherham The Second (Totally fictitious name) and He's got all the classes in the project prefixed with PW2
. I want to expand on the project and add a few classes of my own to the project, How should I name these new objects, Should I:
Prefix the new objects with
PW2
: This keeps consistence across the project but could create namespace collision for the Prince, if say for example I create a new PW2Foo class and the Prince already has a PW2Foo class in some other project that he is working on.Prefix the new objects with
BG
(My initials): This would avoid any namespace collisions but would put my initials on something I didn't really start and it could also add confusion to people using the project in the future.Ask Prince Witherham the Second what he would prefer: This is probably the most strait forward but I can see it being problematic, if the Prince isn’t as activly maintaining the project anymore because he is vacationing of on his private island where they don't always have an internet connection.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信用你自己的名字作为类前缀是很常见的,但用项目名称作为前缀也很常见。请注意,
NS
并不是唯一的 Cocoa 前缀:还有CG
、CV
和CA
,一切都取决于该类来自哪个框架。你明白了。因此,我相信这些选项中最好的(尽管选项 3 当然可以覆盖它,如果项目经理更喜欢其他选项)是选项 1:项目中的所有类都应该具有相同的前缀。
I believe it's common to prefix classes with your own name, but also quite common to prefix with the name of the project. Note that
NS
is not the only Cocoa prefix: there isCG
as well, andCV
, andCA
, all depending on which framework the class comes from. You get the point.As such, I believe the best of these options (although option 3 can certainly override it, if the project manager would prefer something else) is option 1: all the classes in the project should have the same prefices.