iOS Development Coding Recommendations (OBJECTIVE-C) (ongoing update)

Source: Internet
Author: User

In the development process, we not only have to look at other people's code, but also let others see our code. Then, having a good coding habit will be very important. The following is a list of coding recommendations for developing iOS using OBJECTIVE-C.

"1"

The length of any function must not exceed 50 lines. (In fact, it's easy to get over 50 lines, so consider code extraction.) )


"2"

Any line code cannot exceed 80 characters. (It is also easy to more than 80 characters, you can consider multi-line display, such as having multiple parameters, you can put one row per parameter.) You can set a reminder for more than 80 characters in Xcode, and select Page Guide at column. After Setup, there is a vertical line at 80 characters of the code.




"3" leaves a white line before the definition of each method.

The "4" function is similar to the method to put together, and it is recommended to use #pragma mark-* * * To navigate the code, slicing blocks of code.

There should be a space between the "5" two-dollar operator and the parameter, such as the assignment number = left one space left and right.

self.mystring = @ "235423rew523452345";
The "6" unary operator and parameter do not place spaces between them, for example! Non-operator,& bitwise AND, | bitwise OR.

  BOOL IsOpen = true;  BOOL isclose =!isopen;

"7" forces the type conversion and does not place spaces between the arguments.

NSString *STR3 = (nsstring*) self.mystring;

The value of the "8" long variable should be split into multiple lines. This is especially true when using arrays or dictionaries. The following are also methods for quickly declaring array @[] and dictionary @{}, respectively.

Nsarray *array = @[@ "111",                     @ "2222222222",                     @ "3333333",                     @ "wwwwwwwwwwww"                     ];

  Nsdictionary *dict = @{@ "name": @ "Jack" @ "Age                         ": @ "@" @ "                         gender": @ "female" @ "                         ismarried": @ "false"                         };

"9" Try to use meaningful names and refuse to use meaningless characters such as i,j to name them. The name of the class is capitalized, the name of the other variable is lowercase, and the word is split with a camel.

"10" minimizes the use of numeric constants directly in your code, and uses macro definitions. such as: Max_number_phone replacement 8 and so on. So our search is also more convenient.

"11" minimizes repetitive calculations in your code, such as using the screen width in multiple places in your code, and then calculating:[[uiscreenmainscreen] Bounds]. size . width, many times, idle very cumbersome, code is also lengthy. Rather than direct macro definition:

#define SCREEN_WIDTH ([[UIScreen mainscreen] bounds].size.width)

"12" reasonable use of the customary abbreviations:

Alloc: distribution;

ALT: turns, alternates;

App: app;

Calc: calculation;

Dealloc: Destroy and reconstruct;

Func: function, method;

Horiz: Horizontal;

Info: information;

Init: Initialize;

Max: the largest;

Min: the smallest;

MSG: message;

Nib:interface Builder;

Rect: Rectangle;

Temp: temporary;

Vert: Vertical;


The "13" macro defines all uppercase letters.

The "14" function is not more than 50 lines long, and the small function is more readable than the large function. function parameters should not be too much, 0-dollar function is best, unary function is also good, higher than ternary function virtual reconstruction.

"15" use chained programming within a reasonable range:

NSString *myname = [[NSString alloc] init];
But nesting should not be more than 3 layers, more than 3 layers need to be reconstructed.

All parameters are on the same line when the "16" function is called. If there are too many arguments, you can have one argument per line, and each parameter is aligned with a colon.
"17" for the protection of incoming parameters or whether it is empty judgment, try not to use if (!obj), and use the Nsassert assertion to handle. Nsassert is a system-defined macro.

Nsassert (myName! = nil, @ "MyName parameter is empty");

If the condition is true, the program continues execution;

If the condition is false, an exception is thrown, and the exception is the string defined later;

"An", "the", "new" are generally used to modify the "18" method before the parameter name. Such as:

-(void) Setpersoninfo: (nsstring*) Theid thename: (nsstring*) thename theage: (nsinteger*) theage
"19" if-else more than four layers of time, it is necessary to consider the reconstruction, multilayer if-else structure is difficult to maintain.

"20" When a certain condition is required to perform an operation, the leftmost one should be the most important code, and do not embed the most important code in the IF.

As the good style is:

-(void) SomeMethod {if (![ Someother Boolvalue]) {   return;  } The most important code is written here;}

Negative example

-(void) SomeMethod {if ([Someother boolvalue]) {      //important code;  }}

All logical blocks of "21" are surrounded with {} curly braces, even if they are just a single line of code.

"22" explicitly specifies the constructor and has the appropriate annotations.

"23" Do not initialize the variable or attribute to 0 or nil in the Init method because it is not necessary.
Do not perform any layout operations when the subclass of "24" UIView is initialized. The layout operation should be done in layoutsubviews, call Setneedslayout when you need to re-layout, instead of calling Layoutsubviews directly.

"25" keeps the public API simple, which is to keep the. h file simple. The functions declared in. h are exposed, and if it is not necessary to expose the other classes at all, do not declare them in. h. The methods in OC are public methods, there is no private method to say.

A file of "26" implements only one class. Do not have more than one class in the same file.

The "27" protocol is created separately with a single file, so try not to mix it with related classes in a single file.

"28" When using the class definition in its own definition of the class, try not to introduce itself in the header file to define the class header file, using @class substitution. Instead, introduce the header file in the implementation file.

The "29" layout uses relative layouts whenever possible, such as using sub-view relative positions in the parent view.


This article references: Http://toutiao.com/i6218665014828466690/?tt_from=mobile_qq&utm_campaign=client_share&app=news_ Article&utm_source=mobile_qq&iid=3279310661&utm_medium=toutiao_ios



GitHub Home:https://github.com/chenyufeng1991 . Welcome everyone to visit!

iOS Development Coding Recommendations (OBJECTIVE-C) (ongoing update)

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.