我是Stack Overflow的新用户,同时也是一名iOS开发者。我想知道如何审查我们的代码?
编程中有哪些命名规范?变量名、类名的指导方针是什么?如何管理这些规范?
我是Stack Overflow的新用户,同时也是一名iOS开发者。我想知道如何审查我们的代码?
编程中有哪些命名规范?变量名、类名的指导方针是什么?如何管理这些规范?
Variable names start with lower-case letters, but are internally capitalized wherever a new word appears:
NSString * streetAddress = @"1 Infinite Loop";
NSString * cityName = @"Cupertino";
NSString * countyName = @"Santa Clara";
--------- 正确方式 ---------
NSString * hostName;
NSNumber * ipAddress;
NSArray * accounts;
--------- 不正确的方式 ---------
NSString * HST_NM; // all caps and too terse
NSNumber * theip; // a word or abbreviation?
NSMutableArray * nsma; // completely ambiguous
Variables can't start with a number, no spaces, no special characters other than underscores.
Apple discourages using an underscore as a prefix for a private instance variable.
NSString * name // correct!
NSString * _name // _incorrect_
变量名:指示类型
正确写法
NSString * accountName;
NSMutableArray * mailboxes;
NSArray * defaultHeaders;
BOOL userInputWasUpdated;
还行,但不是最理想的
NSString * accountNameString;
NSMutableArray * mailboxArray;
NSArray * defaultHeadersArray;
BOOL userInputWasUpdatedBOOL;
何时指示类型
NSImage * previewPaneImage; // self-explanatory
NSProgressIndicator * uploadIndicator; // shows progress for uploads
NSFontManager * fontManager; // only one of these, basic name ok
方法名
方法可能是我们可以谈论的最重要的主题。大多数面向对象语言使用一种语法。
虽然这些方法名称在第一次写入时很容易,但实际行为并不清晰。在大量周围代码的情况下,这是更大的问题。
Cocoa程序员从最终效果出发思考,选择一个基于实际使用情况的方法名。假设我想编写一个写入磁盘的内存中文件对象。
在Cocoa/Objective-C中,它看起来像这样:
[fileWrapper writeToFile: path atomically: YES updateFilenames: YES];
方法名称:访问器
正确!
- (NSString *) name;
- (NSString *) color;
name = [object name];
color = [object color];
错误的
- (NSString *) getName;
- (NSColor *) getColor;
name = [object getName];
color = [object getColor];
何时使用“Get”前缀
// 将对象从NSArray复制到缓冲区
id *buffer = (id *) malloc(sizeof(id) * [array count]);
[array getObjects: buffer];
( Don't worry if you don't know what malloc does. )
The "set" prefix is always used on setters, though:
[object setName: name];
[object setColor: color];
形容词
Not all accessors return values like name, date, height, etc. Some represent a particularly quality of an object. These are often represented by BOOLs.
For example, "selectable". In Objective-C, the getter for this key is called -isSelectable, but the setter is -setSelectable:
BOOL selectable = [textView isSelectable];
BOOL editable = [textView isEditable];
[textView setSelectable: YES]; // no "is"
[textView setEditable: YES]; // no "is"
// if textview is editable.
if ([textView isEditable])
[textView setEditable: NO];
Keep in mind that naming your accessors according to all of these rules isn't purely an issue of clarity and aesthetics. Cocoa relies heavily on KVC for much of its magic, and KVC relies on properly-named accessors.
CDCTableView+CategoryName.h
,其中 CategoryName 是你创建的类别的名称。同时我也同意 Rob 的看法,这也是为什么我要求文档的原因。 - Popeye@synthesize
,编译器会默认生成一个使用前导下划线的ivar,并在您意外添加不带下划线的自定义ivar时提出警告。如果添加下划线,则该警告将消失。这种行为(以及我之前提供的文档参考)表明声称“Apple不鼓励在私有实例变量的前缀中使用下划线”是不正确的。恰恰相反,他们建议在某些情况下应该这样做(并且默认情况下会这样做)。 - Rob