"Learn Swift from scratch" study notes (Day 56) - naming conventions for Swift coding conventions

"Learn Swift from scratch" study notes (Day 56) - naming convention

Original article, welcome to reprint. Please indicate: Guan Dongsheng's blog 

 

The program code is full of self-defined names, and it is very important to choose a name that is similar and conforms to the specification.

There are many naming methods, but the more famous and widely accepted nomenclature are:

  • Hungarian naming, generally just named variables, the principle is: variable name = type prefix + description, such as bFoo represents a boolean type variable, pFoo represents a pointer type variable. Hungarian naming is still controversial, and several books in Swift coding standards do not use Hungarian naming.
  • Camel-Case (Camel-Case), also known as camel nomenclature, refers to the use of mixed uppercase and lowercase letters to name names. Hump ​​naming is divided into: small hump method and big hump method.

          a) The small camel case method is that the first word is all lowercase, and the first letter of the following words is capitalized, such as: myRoomCount;

          b) The big camel case method is that the first letter of the first word is also capitalized, such as: ClassRoom.

 

CamelCase is the main naming method of the Swift coding specification, and the content of the name is different, you can choose the small camel case method or the big camel case method. The categories are explained below:

  • Types such as classes, structures, enumerations, and protocols should be named in big camel case, such as SplitViewController.
  • File name, using big camel case, such as BlockOperation.swift.
  • Extension file, sometimes the extension is defined in a separate file, its name is "primitive type name + extension" as the extension file name, such as NSOperation+Operations.swift.
  • For variables and properties, the small camel case method should be used, such as studentNumber.
  • Constant, using the big camel case method, such as MaxStudentNumber.
  • Enumeration members, similar to constants, use big camel case, such as ExecutionFailed.
  • Functions and methods should adopt the small hump method, such as balanceAccount, isButtonPressed, etc.

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327080476&siteId=291194637