哪个方法名称最适合 Objective-C/Cocoa 约定?
这是一个快速问题。 哪个方法名称对于 Objective-C Cocoa 应用程序最有意义?
-(void) doSomethingWithAnimation:(BOOL)animated
或:
-(void) doSomething:(BOOL)animated
甚至:
-(void) doSomethingAnimated:(BOOL)animated
Here's a quicky question. Which method name makes the most sense for an Objective-C Cocoa application?
-(void) doSomethingWithAnimation:(BOOL)animated
or:
-(void) doSomething:(BOOL)animated
or even:
-(void) doSomethingAnimated:(BOOL)animated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为 Cocoa 约定将为您的示例提供以下语义(显然,忽略参数的 BOOL 类型):
实际上会期望一个动画作为参数(即代表动画的东西。
会期望做一些事情。
会,如诺亚回答说,用可选的动画做一些事情。
I think the Cocoa convention would give your examples the following semmantics (ignoring the BOOL type for the argument, obviously):
would actually expect an Animation as the parameter (i.e. something that represents the animation.
would expect the Something to do.
would, as Noah answered, do something with optional animation.
-(void)doSomethingAnimated:(BOOL)animated 似乎最符合苹果的命名风格。 作为参考,请检查 iPhone UIKit 文档 - 例如 UINavigationController 的 -popToRootViewControllerAnimated: 方法。
-(void)doSomethingAnimated:(BOOL)animated seems most consistent with Apple's naming style. For reference, check the iPhone UIKit docs - UINavigationController's -popToRootViewControllerAnimated: method, for instance.
这里还有另一个需要考虑的选项:创建两个方法,
-doSomething
和-doSomethingWithAnimation
。然后,如果您愿意,您可以让它们都尾部调用第三个私有方法,并为该方法指定您想要的任何名称。 :)
Here's another option to consider: make two methods,
-doSomething
and-doSomethingWithAnimation
.Then, if you want, you can have them both tail-call a third, private method, and give that method any name you want. :)
清晰简洁地写下您的评论,例如:
//do Something with the Animation
然后根据这个注释写下你的方法名称,如下所示
doSomethingwithAnimation:(BOOL)动画
Objective-C 和 Cocoa 的设计是为了便于阅读,所以如果你的方法不能清晰地叙述你的代码,它可能没有被很好地命名。
write your comment clearly and tersely for example :
//do Something with the Animation
Then write your method name based on this comment, like below
doSomethingwithAnimation:(BOOL)animated
Objective-C and Cocoa are designed to read well, so if your method cannot be a clear narrative of your code, it might not be well named.