从其他开发者那里继承代码让我坚信通过类扩展尽可能减少公共接口中的消息数量是非常必要的。我也坚信采用特殊的命名约定来区分类的私有成员和实现特定的成员。我真的很喜欢一眼就能看出在实现上下文中发送了哪些消息,引用了哪些成员,它们都不是为公共使用而设计的,反之亦然。如果没有其他什么,这使得我更快地理解一个类的整体语义,这是值得的。
抛开理由不谈,我写了大量带有大量私有方法的类,但我从未真正想出一种我真正喜欢的命名模式(就像我对于ivars的有争议的ivar_约定一样)。值得一提的例子:
@interface myClass()
// I like this, but as we all know, Apple has dibs on this one,
// and method name collisions are nasty.
- (void)_myPrivateMessage;
// The suffix version promoted by Google for ivars doesn't really translate
// well to method names in Objective-C, because of the way the method
// signature can be broken into several parts.
- (void)doWork_; // That's okay...
- (void)doWork_:(id)work with_:(id)something; // That's just ugly and tedious...
- (void)doWork_:(id)work with_:(id)something and_:(id)another; // My eyes...
// This version is suggested by Apple, and has the benefit of being officially
// recommended. Alas, I don't like it: The capital letter is ugly. I don't like
// underscores in the middle of the name. Worst of all, I have to type three characters
// before code-sense does anything more useful than inform me that I am typing.
- (void)BF_doWork;
@end
目前有很多不同的方法可以混淆我的私有方法名称,但是我并不想自己编造,所以我想首先询问一下是否存在任何我不知道的流行惯例。那么,你用过什么呢?