A few naming rules in Swift

Tram Ho

You may think that naming variables, functions, constants, classes, etc. seems to be simple in your code. But it is actually a very difficult thing.

Proper naming is extremely important for you to code and for others to read your code.

1. Basic principles

Clearly.

Obviously a most important point in naming. Try to make your name as concise and clear as possible. Entities like methods or properties are declared once but used multiple times. Always check if the use case is appropriate for the context. For example:

Clarity is more important than brevity.

In general, do not abbreviate the name of everything. Try to make the most sense though its name will be long. For example:

Ignore unnecessary words. Each word in a name should convey its most striking information. For example: allViews.removeElement(cancelButton) = not clear

allViews.remove(cancelButton) = clearer

Consistency.

Try to use consistent names throughout your project. Consistency is especially important when you have a class that has methods that use polymorphism.

Methods that do the same thing in different classes should have the same name. For example:

Wrong way

Right way

No need to explain

Names do not need an explanation. For example :

2. Typing rules.

Follow the typing rules to name elements:

  • For names that consist of many words, do not use punctuation marks as part of a name or a separator (dashes, underscores, ..).
  • Instead, capitalize the first letter of the next word (nameOfSomeMethod) – camel rule.
  • However, note that with the method, the variable is lowercase the first letter.

3. Name the class and protocol.

The name of the class should be a clear noun that the class represents. As for protocols, it should be named according to its behavior. Most protocols that are not related to a class should be named so as not to be confused with a class. Often people add “ing” to a name. For example:

A number of small, unrelated protocols are often associated with a class, and the naming convention is for them to have the same name.

For example, the NSObject protocol, which is a group of methods you can use to query any object about its position in the class hierarchy to make it call specific methods and increase or reduce its reference. Because the NSObject class provides the main methods, the protocol has the same name as the class.

4. Name the method.

General rules

  • Starting with a lowercase letter, the following letters capitalize the first letter.
  • For voliws of methods that express the action an object performs, start with a verb.
  • Do not use do or does in the name. Because these verbs rarely have more meaning, they do not use adverbs or adjectives before them.
  • If the method returns a caller’s attribute, name the method according to the attribute. Avoid using get unless one or more values ​​are returned indirectly.
  • Add words to clarify the role of the parameter:

Especially when a parameter type is NSObject, Any, AnyObject or a basic such as Int, String, type of information and context at the time of use may not fully convey the intention. For example:

To be clearer, we should use the following:

  • Use names that make up the English grammar phrases. For example:

  • Begin the name of the make tape initialization methods. Example: x.makeIterator ()
  • When the first argument forms part of the phrase, give it an argument label.

  • On the other hand, if the first argument does not form part of a grammar phrase, omit the argument label and add a word to increase its meaning. For example:

5. Name the Properties.

Name variables, parameters, and related types according to their roles instead of their types. For example

Wrong way

How to use

  • If the attribute is shown as a noun, the form is: (type) noun. For example: title
  • If the attribute is expressed as an adjective: isAdj. For example: isHidden.
  • If the property is expressed verb form: verObject. For example: showPopup.
  • The verb should be in the present simple tense.
  • Do not use participles to turn words into adjectives.

  • Use verbs like: can, should, will, .. to make sense more but don’t use do or does.

6. Summary.

Through the article hope to help you better understand the naming in code to easily use the most 😄

Reference: https://medium.com/better-programming/naming-conventions-in-swift-4b7ca5eed4d2

Chia sẻ bài viết ngay

Nguồn bài viết : Viblo