目录
- 1 Interface and API Design
- Item 15:Use Prefix Names to Avoid Namespace Clashes
- Item 16:Have a Designated Initializer
- Item 17:Implement the description Method
- Item 18:Prefer Immutable Objects
- Item 19:Use Clear and Consistent Naming
- Item 20:Prefix Private Method Names
- Item 21:Understand the Objective-C Error Model
- Item 22:Understand the NSCopying Protocol
- 2 Reference
1 Interface and API Design
Item 15:Use Prefix Names to Avoid Namespace Clashes
Since Objective-C
dosen’t have namespace like other language such as Java's Package
and Python's Module
,Prefix is the solution that minimize name clash possibility.
-
Choose 3 letter prefix(from your company & application) for your class. Apple reserve the 2 letter prefix for its future use.
-
If your own library use a thrid-party library, be sure to prefix the thrid-party library as well. For example, your library has
LALClient
, if the thrid-party library has prefixed class asXYZData
, you’d better to reprefixXYZData
asLALXYZData
. By doing so, if other people install your library and alsoXYZData
, there would be no clash; it also allow other people to install a different version ofXYZData
。This convention is common such as inRestKit
, which useAFNetworking
,it reprefix it asAFRK...
.
Item 16:Have a Designated Initializer
Item 17:Implement the description Method
-
Implement description method to provide a meaningful string description of instances
-
If the object description could do with more detail for use during debugging (
po
command forLLDB
), implementdebugDescription
(default todescription
).
1
2
3
4
5
6
7
8
9
10
//use dictionary format for description provides compact visualization of object information.
-(NSString *)description{
return [NSString stringWithFormat:@"<%@:%p,%@>",
[self class],
self,
@{@"title": _title,
@"labitude":_labitude,
@"longitude": _longitude
}
}
Item 18:Prefer Immutable Objects
-
When possible, create objects that are immutable (readonly property in
.h
); -
Extend read-only properties in a class-extention to read-write if the property will be set internally.
-
Provide methods to mutate collections held by objects rather than exposing a mutable collection as a property.
Item 19:Use Clear and Consistent Naming
The conciseness comes at the cost of ambiguity. While Objective-C reach unambiguity via verbosity. The Objective-C code reads like sentences with minimal ambiguity. This is what called self-documenting(自文档), code itself is enough to convery its functionality at most time.
-
Ensure method names are concise but precise to make them read from left to right as a sentence.
-
Avoid using abbreviations of types in method names.
-
Most important, make sure that method names are consistent with your own code or that with which it is being integrated.
Item 20:Prefix Private Method Names
-
Prefix priavate method names so that they are easily distinguished from public methods.
-
Avoid using a single underscroe as the method prefix, since this is reserved by Apple.
A better choice is to use double underscore __someMethod
instead of p_someMethod
.
Item 21:Understand the Objective-C Error Model
-
Use exceptions only for tatal errors that should bring down the entire application, such as sending message to abstract class.
-
For nonfatal errors, either provide a delegate method to handle errors or offer an out-parameter
NSError
object.
1
2
3
4
5
6
7
8
9
10
11
//out-parameter NSError object
-(BOOL)doSomething:(NSError **)error{
//some implementation
}
NSError *error = nil;
BOOL ret = [object doSomething:&error];
if(error){
// There was an error
}
Item 22:Understand the NSCopying Protocol
To Be Continued.