How to write Objective-c language gracefully?

Source: Internet
Author: User


① Reduction 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.


② of Process


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,
      };
④ 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.
⑧ of the Unknown


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
⑨ constant or 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.



How to write Objective-c language gracefully?


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.