Elegant naming of objective-C

Source: Internet
Author: User
Tags call back

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 reductionThe name abbreviations are only used in general technical terms, such as URLs. Do not create your own name abbreviations, such as CTR and 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. ProceduralUse "will" before an action occurs, and then use "did" to check whether the shold is used. 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 call back and confirm before the occurrence, use shocould to name the callback and return a bool value. NamespaceThe 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. • The type (class, enumeration, and structure) must be prefixed with the relevant module.
  1. Uiview
  2. Nsstring
  3. Cgrect
• The constant name must be prefixed with the relevant type name.
  1. Uiapplicationdidfinishlaunchingnotification
  2. Cgrectzero
• The function name must be prefixed with the relevant type name.
  1. Cgrectmake
  2. Cgpointmake
• To name an enumeration type, you must add the prefix of the relevant class name and the prefix of the enumeration type.
  1. Typedef ns_enum (nsinteger, uiviewanimationtransition ){
  2. Uiviewanimationtransitionnone,
  3. Uiviewanimationtransitionflipfromleft,
  4. Uiviewanimationtransitionflipfromright,
  5. Uiviewanimationtransitioncurlup,
  6. Uiviewanimationtransitioncurldown,
  7. };
When we do the above, we can achieve no conflict of names. Parameter promptWhen naming a parameter, you must add a parameter name prompt before each parameter.
  1. -(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) nibbundleornil
  2. -(Void) receivmseguewithidentifier :( nsstring *) identifier sender :( ID) sender
  Object NameTo 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:
  1. Titlelabel // indicates the title label, which is a uilabel object.
  2. Labeltitle // label title? Seems to be an nsstring?
  3.  
  4. Confirmbutton // confirmation button
  5. Buttonconfirm // an unnatural name, which looks like a button clicking action.
  Method naming conforms to the syntaxMost methods can be divided into the following two categories, which are often used in disorder. They are: • What to Do • What to do "what to do" means to get an object, starting with a noun as a method; what to do means to execute an operation, it must begin with a verb. Take a look at the following naming method:
  1. -(Xxitem *) itemnamed :( nsstring *) Name // good. Clear meaning
  2. -(Xxitem *) finditemwithname :( nsstring *) Name // is more like an operation than returning an object.
Finditemwithname indicates an operation without returning an object. For example, it can be used to set internal members of a class, for example:
  1. -(Void) finditemwithname :( nsstring *) name {
  2. ...
  3. Self. founditem = xxx;
  4. ...
  5. }
  Get"What is it" is often named as a method starting with get. 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.
  1. -(Xxitem *) getitematindex :( nsuinteger) index // bad !! Nonstandard naming
  2. -(Xxitem *) itematindex :( nsuinteger) index // good, clear name
  3. -(Void) getitem :( xxitem **) outitem atindex :( nsuinteger) index // It is more compliant, but the second is better.
  IntellectualThe caller needs to know that the caller can add the caller to the first parameter of the callback method:
  1. -(Bool) Application :( uiapplication *) Application willfinishlaunchingwitexceptions :( nsdictionary *) launchoptions
  2. -(Void) buttontapped :( uibutton *) sender
  Constant or macroGlobal constants cannot be defined using macros. 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 use const to 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.

 

This article is reproduced from cocoachina: http://www.cocoachina.com/applenews/devnews/2014/0816/9395.html

Elegant name of objective-C)

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.