Cocoa 中的实例变量命名约定

发布于 2024-08-10 19:47:53 字数 580 浏览 7 评论 0原文

这个问题是关于 Objective C 和 Cocoa 中的变量命名风格。我只是想强调,我不是在寻找“正确”的答案,只是在寻找好的想法。

我读过 Apple 和 Google 的 Objective C 风格指南,但我对它们都不太满意。苹果的指南没有关于实例变量与局部变量的任何真正的风格建议。事实上,Cocoa 库本身似乎非常高兴拥有与实例变量同名的函数参数。这让我个人感到畏缩。

谷歌指南指定实例变量应使用尾随下划线表示。好吧,一切都很好,但它建议我们用 @synthesize property = property_ 合成每个公共属性。我不知道其他人是怎么想的,但如果我要对项目中的每个实例变量都这样做,那我就该死了。我认为这是一个浪费且令人困惑的解决方案。

我很想使用对象属性的 myX(例如“myInstanceVariable”)命名样式,但我很少在 Objective C 中看到这种样式。

那么是的,你用什么?我不知道有什么你认为有用的样式约定吗?您认为函数参数与实例变量同名是否危险,尤其是在多个开发环境中?谢谢大家!

注意 - 正如许多人指出的那样,我的术语在 OP 中是错误的。如果原来的措辞损害了清晰度,我深表歉意,但我认为要点仍然很明确。

This question is about variable naming style in objective c and cocoa. I just want to stress that I'm not looking for a "right" answer, just good ideas.

I've read through Apple and Google's objective c style guides and I'm not really happy with either of them. Apple's guide doesn't have any real style recommendations regarding instance variables vs local variables. In fact, the Cocoa library itself seems perfectly happy having function parameters of the exact same name as instance variables. That makes me cringe personally.

Googles guide specifies that instance variables should be indicated with a trailing underscore. Alright, all well and good, but it suggests that we then synthesize every public property with @synthesize property = property_. I don't know about anyone else, but I'll be damned if I'm going to do that for every instance variable in my project. I think it's a wasteful and confusing solution.

I'm tempted to go with the myX (eg "myInstanceVariable") naming style for object properties, but I have rarely seen that style in objective c.

So yeah, what do you use? Any style conventions out there I don't know about that you've found useful? Do you think function parameters with the same name as instance variables is dangerous, especially in multiple developer environments? Thanks guys and gals!

NOTE - As many people have pointed out, my terminology was off in the OP. Apologies if the original wording hurt the clarity, but I think the point was still clear.

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

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

发布评论

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

评论(5

素罗衫 2024-08-17 19:47:53

我倾向于使用无前缀的实例变量名称(请注意,“成员变量”是一种 C++ 主义,因为它暗示结构和类主要是可互换的,而 Objective-C 中的情况并非如此) ),并且在出现歧义的情况下,我使用 Smalltalk 约定,通过“a”或“an”的类型来命名参数,例如:(

- (void)setFoo:(SOFoo *)aFoo;
{
    foo = aFoo;
}

当然,在现代 ObjC 中,您可以使用一个属性。)

使用 theFoo 代替 aFoo 也很常见;请参阅此问题的答案。

如果您确实担心冲突,那么 Google 惯例是有意义的。如果您使用 Xcode 文本宏 或类似 补全字典Accessorizer 来生成您的指令,采用起来非常简单。

请注意, Cocoa 键值编码指南 几乎假设(a)您不为实例变量名称添加前缀/后缀,或者(b)您为它们实现(或合成)非前缀/后缀访问器。正如其他人提到的,不要使用 _ 前缀;它是为 Apple 在其框架中使用而保留的。

I tend to use non-prefixed instance variable names (note that "member variable" is a C++ism as it's suggestive of structures and classes being mainly interchangeable, which is not the case in Objective-C), and in cases where ambiguity arises, I use the Smalltalk convention of naming the parameter by its type with "a" or "an", e.g.:

- (void)setFoo:(SOFoo *)aFoo;
{
    foo = aFoo;
}

(of course, in modern ObjC you'd use a property for this.)

Using theFoo instead of aFoo is also somewhat common; see the answers to this question.

The Google convention makes sense if you're really worried about conflicts. If you use an Xcode text macro or tool like Completion Dictionary or Accessorizer to generate your directives, it's pretty simple to adopt.

Note that the Cocoa key-value coding guidelines pretty much assume either (a) you do not prefix/suffix your instance variable names, or (b) you implement (or synthesize) non-prefixed/suffixed accessors for them. As someone else mentioned, do not use the _ prefix; it's reserved for Apple's use in their frameworks.

千笙结 2024-08-17 19:47:53

首先:Objective-C中没有“成员变量”,有“实例变量”或“ivars”。

Google 并不是 Objective-C 编码或 Mac 开发方面的任何权威。 Google Earth 是一个 Qt 应用程序:'nuff 说。

我似乎记得看到过 Apple 针对 Objective-C 的官方编码风格指南,但目前我没有找到。不过,这篇文章是一个很好的总结:

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

找到了!以下是 Apple 的 Cocoa 官方编码指南:

http://developer .apple.com/mac/library/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html

First: there are no "member variables" in Objective-C, there are "Instance Variables" or "ivars".

Google is NOT any kind of authority on Objective-C coding or Mac development. Google Earth is a Qt app: 'nuff said.

I seem to remember seeing an official coding style guide from Apple for Objective-C, which I'm not finding at the moment. This article is a pretty good summary, though:

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

Found it! Here's Apple's official coding guidelines for Cocoa:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html

玩套路吗 2024-08-17 19:47:53

在Cocoa中,风格是使用pascalCased(或者是camelCased?我永远不记得)名称;并将成员变量命名为与访问器方法相同的名称。 (例如 NSInteger anInteger- anInteger- setAnInteger:)。

它可能不是最好的风格,但是如果您要使用 Cocoa 做大量的工作,那么习惯它可能是个好主意,因为许多机制都采用这种特殊的命名约定。

In Cocoa, the style is to have pascalCased (or is that camelCased? I can never remember) names; and have the member variables be named the same as the accessor methods. (Such as NSInteger anInteger, - anInteger and - setAnInteger:).

It might not be the best style, but it's probably a good idea to get used to it if you are going to do any amount of work with Cocoa, as a number of mechanisms assume this particular kind of naming convention.

红尘作伴 2024-08-17 19:47:53

m_variableName 对于成员变量来说也很常见。
就我个人而言,大多数时候,我只是对两个变量使用相同的名称,并区分 this.varnamevarname

m_variableName is pretty common too for member variables.
Personally, most of the time, I just go with the same name for both variables and making the distinction between this.varname and varname.

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