The elegant name of Objective-C, the name of objective-c

Source: Internet
Author: User

The elegant name of Objective-C, the name of objective-c

There are only two hard things in Computer Science: cache invalidation and naming things.
There are only two difficulties in Computer Science: cache invalidation and naming.

-Phil Karlton

Computer Language is a medium for communication between people and computers. Good code should be as natural and elegant as people speak to computers. Naming seems to be a simple task, but the simpler it is, the harder it is to do well. Otherwise, masters will not regard naming as a difficult task in the computer industry. The question of how to extract Objective-C from a computer language in an elegant way is still a test of the depth of understanding by engineers. There are a lot of APIs in Apple's SDK. We can learn some naming art from these Apis.

Abbreviation for reduction

The abbreviation is used only for general technical terms, suchURLYou cannot create your own name abbreviations, suchCtr,Msg. The name should be longer than hard to understand.

Do you know the abbreviations when looking at other people's code? The short name is really good, but it cannot be abused to lead to loss of readability.

Procedural

Used before an action occursWill, Used after occurrenceDidTo check whether the application is used.Should.

Each processing process has a certain process. This process usually produces some notifications and Callbacks. A good name must clarify the steps in the current process. When naming these notifications and callbacks, it is best to provide the two versions before and after the occurrence. If you want to confirm the callback before the occurrence, useShouldName the callback and returnBOOLValue.

Namespace

The names of various global functions, such as constants, classes, enumeration, and structures must be prefixed.

Objective-C does not have a namespace concept like C ++ or a Java package name. As the project code increases, name conflicts may inevitably occur, therefore, the name of the global scope must be unique. A classic practice is to add a naming prefix. Most people think that the naming prefix only adds several uppercase letters to the front of the class, not just that.

  • Type (class, enumeration, structure) must be prefixed with the relevant module.

    UIViewNSStringCGRect
  • The constant name must be prefixed with the relevant type name.

    UIApplicationDidFinishLaunchingNotificationCGRectZero
  • The function name must be prefixed with the relevant type name.

    CGRectMakeCGPointMake
  • To name an enumeration type, you must add the prefix of the relevant class name and the prefix of the enumeration type.

    typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {    UIViewAnimationTransitionNone,    UIViewAnimationTransitionFlipFromLeft,    UIViewAnimationTransitionFlipFromRight,    UIViewAnimationTransitionCurlUp,    UIViewAnimationTransitionCurlDown,};

When we do the above, we can achieve no conflict of names.

Parameter prompt

When naming a parameter, you must add a parameter name prompt before each parameter.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender
Object Name

To name a modified object, you must use the Modifier + type instead of specifying its type.

Many people like to put the object type before the object name to identify the type of an object. This is not in line with the characteristics of Objective-C language and may cause ambiguity, such as a UILabel object:

TitleLabel // indicates the title label, which is the title of the UIlabel object labelTitle // label? Seems to be an NSString? ConfirmButton // The confirmation button buttonConfirm // The name does not appear to be a button-clicking action.
Method naming conforms to the syntax

Most methods can be divided into the following two categories, which are often used in disorder. They are:

  • What do you want
  • What to do

"What" indicates obtaining an object, starting with a noun as a method; "what" indicates executing an operation, starting with a verb as a method. Take a look at the following naming method:

-(XXItem *) itemNamed :( NSString *) name // Good. clear meaning-(XXItem *) findItemWithName :( NSString *) name // is more like an operation than returning an object.

findItemWithNameThis name indicates an operation without returning an object. For example, it can be used to set internal members of a class, for example:

- (void)findItemWithName:(NSString *)name{    ...    self.foundItem = xxx;    ...}
Get

"What is it" is often namedgetMethod. First, get is a verb, so it is still "what to do" or "what to do ". The get method is not used to return objects, but can be used to return objects in parameters.

-(XXItem *) getItemAtIndex :( NSUInteger) index // Bad !! Nonstandard naming-(XXItem *) itemAtIndex :( NSUInteger) index // Good, clear naming-(void) getItem :( XXItem **) outItem atIndex :( NSUInteger) index // It is more compliant, but the second is better.
Intellectual

The caller needs to know the caller during callback.

You can add the caller in the first parameter of the callback method:

- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions- (void)buttonTapped:(UIButton*)sender
Constant or macro

A global constant cannot be defined using a macro.

We often see macro-defined notifications and keywords. In fact, this is very dangerous, because macros may be redefined, and referencing different files may lead to different macros, so try to useconstTo define constants.

Some Thoughts

Naming is often not important in development. After all, poor Naming does not affect program logic.

However, the invisible maintenance cost caused by poor naming in large projects is quite high, which may be difficult to detect at the beginning of the project, and will be in the difficult maintenance dilemma.

We often pay great attention to the complexity of the project logic, but cannot properly name "simple.

In fact, if simple things do not work well, it is also junk to make complicated things.

 

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.