Naming conventions in iOS development (OC)

Source: Internet
Author: User
Tags coding standards

Desertion: Recently found himself to have an empirical problem, not too easy to accept new knowledge, this is engaged in technical research and development of the people are not very reasonable, need to change.

Text: By reading and writing a large number of code I have their own set of programming ideas and habits, self-thinking of their coding habits is good, code structure is also clear, because I have always been code to read more than writing, but the summary of less, knowledge is often not system. After spending a little more time to summarize their own experience and learning knowledge, this is conducive to the guidance of new people, but also more conducive to deepen their knowledge cognition. Start with code specifications today to summarize the good coding specifications in iOS development. We in the development of other people's code often complain at least inside the heart dozens, in fact, others look at your code may also be complaining about you, I am more deeply aware of: Master and Novice have code standard level difference, Master and master have become the difference between ideas, novice in imitation in the more messy. Therefore, team development must have a good and unified coding standards, so as to facilitate team development and rapid maintenance. Write the first article today: Naming conventions in iOS development (OC)

naming Principles

1. General principles: High readability (concise and clear) and prevention of naming conflicts (guaranteed by prefixing).

The names of objective-c are usually long and the name follows the camel-named method. A good naming standard is simply to be able to understand what it means and how to use it when the developer sees the name. In addition, each module to add its own prefix, the prefix is very important in the programming interface, you can distinguish the functional categories of software and prevent the naming conflicts between different files or classes, such as the album Module (photogallery) code is the PG as the prefix: Pgalbumviewcontroller, Pgdatamanager.

generation Code

reviews

insertobject:atindex:

good

insert:at:

not clear; At "table?" What?

removeobjectatindex:

g Ood

removeobject:

Yes, because the law is? To remove an object as a parameter

remove:

not clear; What to remove?

2. Consistency

As much as possible with the programming of the Cocoa name of consistent. If you're not sure about a named sex, look at the example in the header file or reference document, in the class that makes the Polymorphic method, the name of the sex is often important. The method that implements the same function in different classes should have the same name.

Code Reviews
– (Nsinteger) tag There are definitions in NSView, Nscell, NSControl
– (void) SetStringValue: (NSString *) There are definitions in many Cocoa classes.

Name of the file

The file should have the following extension:

.h c/c++/objective-c header file
.m ojbective-c implementation file
.mm ojbective-c++ implementation file
.cc Pure C + + implementation file
.c plain C implementation file

The file name of the category should contain the extended class name, such as: GTMNSString+Utils.h or ' gtmnstextview+autocomplete.h '.

Naming of classes

The class name (and the category, protocol name) should be capitalized, and the word will be split in hump format.

1. Prefixes for classes

1) All class names, enumerations, structures, protocol definitions, preferably with a uniform identifier, can be a project abbreviation, or the name of a personal item abbreviation, for example, plus all uppercase hoo (my last name) as a prefix

2) According to the function module, you can add the name prefix of the function module to the class of the function module, such as the profileviewcontroller of the User Center. can be named Hooucprofileviewcontroller.

2. Suffix of class

When all protocol are defined, the suffix delegate is added. such as, Hoorefreshviewdelegate, represents the Refreshview agreement;

All controllers are added with controller, all the notification names plus notification.

Category naming

Class name + identity + extension (uiimageview +hp+web)

Example: If we want to create a Uiimageview-based category for a Web request picture, we should put the category put it in a file named Uiimageview+hpweb.h. Uiimageview is the name of the class to be extended, HP is the exclusive logo , and the Web is extended functionality.

Method naming

The method name should follow the small hump principle, the first letter lowercase, the other words the first letter uppercase, each space division name begins with the verb. The method of execution should start with a verb, a lowercase letter, and a return method should return the content start, but do not add a get before.

Such as:

-(void) Insertmodel: (ID) Model Atindex: (Nsuinteger) atindex;-(instancetype) Arraywitharray: (Nsarray *) array;

  

Naming of Enumerations

Authentic iOS developers will of course name the enumeration in a objective-c way, such as:

typedef ns_enum (Nsinteger, uiviewanimationtransition) {    Uiviewanimationtransitionnone,    Uiviewanimationtransitionflipfromleft,    uiviewanimationtransitionflipfromright,    Uiviewanimationtransitioncurlup,    Uiviewanimationtransitioncurldown,};

  

attribute, variable name

The variable name uses the small hump method, makes the variable name as far as possible to speculate its use attribute to have the descriptive nature. Don't think about playing fewer letters, so that your code can be quickly understood and more important. each attribute is named with a type suffix, such as a button with a Button suffix and a suffix for the model Model .

@property (nonatomic, strong) UIButton *submitbutton;

1) class member variable name

Member variables are named with the small hump method and prefixed with an underscore, such as: UIButton *_submitbutton;

2) local variable name

Obey the small hump naming rules, such as:nsinteger numcompletedconnections =3;

CONST constant

Start with lowercase k , capitalize on the first letter of the word, and rest in lowercase. Such as:

Const float 100.0f;

If a constant is a special meaning it is also recommended to add a suffix, such as a notification plus notification as a suffix, such as:

extern NSString * Const kloginsuccessnotification

Resource file naming (image, localization file)

This image resource naming method, to function as a form of organization, is a good habit, beneficial to view resource files.

Principle:

1) Use words to complete the spelling, or everyone is generally recognized non-discriminatory abbreviations (such as: NAV,BG,BTN, etc.)

2) using the "Module + function" Nomenclature, the module is divided into public modules, private modules. Public module mainly includes unified background , navigation bar, tags, public button backgrounds, public default diagram and so on; Private modules are mainly based on the business function modules of the app , such as User Center, message center, etc.

For example, a user center avatar picture can be named: Uc_imageview_user_icon

Naming conventions in iOS development (OC)

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.