There is only a things in computer science:cache invalidation and naming things.
There are only two difficult things in computer science: Cache invalidation and naming.
–phil Karlton
Computer language is the medium of communication between man and computer. Good code should be as natural and elegant as a person talking to a computer. Naming seems like a very simple thing, and often the simpler things are harder to do well, otherwise the Masters will not regard the naming as a computer problem. How to objective-c this computer language to "say" in an elegant way, or to test the depth of the engineer's understanding of it. There are a lot of APIs in Apple's SDK, and we can see some of the art of naming it from these APIs.
Reduce abbreviations
Named abbreviations are used only for generic professional terms, such as,URLnon-self-creating named abbreviations, such asCtr,Msg. The name is rather longer and not difficult to understand.
Are you looking at other people's code without knowing what it's all about? A short name is better, but the abbreviation cannot be misused to cause loss of readability.
Process of
Use before the action occurs, andWillthen useDidit to ask if it will happenShould.
Each processing has a certain process, this processing often produces some notifications and callbacks, good naming must be clear the steps in the current process. The best way to name these notifications and callbacks is to provide the first and last two versions, and if you want a callback acknowledgement before it occurs,Shouldname the callback and return aBOOLvalue.
Name space
Various global scope functions, constants, classes, enumerations, structures, and so on, must be named prefixes.
Objective-c does not have the concept of C + + namespace, nor the concept of Java package name, with the increase in engineering code, there will inevitably be name collisions, so the global scope of the name must be unique. The classic approach is to prefix the name. Most people think that naming prefixes only add a few uppercase letters to the front of the class, but that's not all.
- Type (class, enum, struct) should be prefixed with the relevant module before naming.
UIView
NSString
CGRect
- Constant names are prefixed with the associated type name.
UIApplicationDidFinishLaunchingNotification
CGRectZero
- The function name is prefixed with the associated type name.
CGRectMake CGPointMake
- Enumeration type names are prefixed with the associated class name, and enumeration values are named to enumerate the type prefixes.
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
};
|
Do the above points can almost do not conflict name.
Parameter hints
When a method is named, a parameter name hint is added before each parameter.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender
Object naming
Name a decorated object with a decorated + type instead of specifying its type first.
Many people like to put the object's type in front of the object, so as to identify the type of object, which is not in line with the characteristics of objective-c language, easy to cause ambiguity, such as a Uilabel object:
titleLabel // Label for the title, which is a UILabel object
labelTitle // The title of the label? Seems to be an NSString?
confirmButton // Confirm button
buttonConfirm // Unnatural name, looks like a button click action.
Method naming conforms to syntax
Most of the methods can be divided into the following two categories, which are often confused. They are:
- What do you want?
- Do what
"What" means to get an object, to use a noun as the beginning of the method; "To do" means to perform an action, starting with a verb as a method. Look at the following naming method:
-(XXItem *) itemNamed: (NSString *) name // Good. The meaning is clear
-(XXItem *) findItemWithName: (NSString *) name // It's more like an operation than returning an object.
findItemWithNameThis naming represents an operation without having to return an object, such as it can be used to set internal members of a class, such as:
- (void)findItemWithName:(NSString *)name{
...
self.foundItem = xxx;
...
}
Get
"What to" is often randomly named asgetthe beginning of the method. First get is a verb, so it's still "what to do" or "what to do". Then the Get method is not used to return the object, but it can be used in parameters to return.
|
-(XXItem *) getItemAtIndex: (NSUInteger) index // Bad !! Irregular name
-(XXItem *) itemAtIndex: (NSUInteger) index // Good, clear name
-(void) getItem: (XXItem **) outItem atIndex: (NSUInteger) index // Comply with specifications, but the second one is better.
|
Knowable
Callback is called by the caller to know its caller
The caller can be added to the first parameter in the callback method:
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- (void)buttonTapped:(UIButton*)sender
A constant or a macro
Global constants are not available with macro definitions
We often see a number of macro-defined notifications, keywords, and so on. In fact, this is very dangerous, because macros are likely to be redefined, and referencing different files may result in different macros, so use them as much as possibleconstto define constants.
Some thinking
The name of the good or bad in the development often also do not pay much attention, after all, poor naming will not affect the program logic. But the poor naming of the hidden maintenance costs associated with large projects is quite high, which may be difficult to detect at the start of a project, and later fall into fought's maintenance dilemma. We often attach great importance to the complexity of the project logic, but can not be a good "simple" name to do well. In fact, if the simple things are not good, then make more complex things that is rubbish.
The elegant name of Objective-c