Make your JS code more readable

Source: Internet
Author: User

I. To add a reasonable comment

    • Functions and methods--each function or method should contain a comment describing its purpose and the algorithm that might be used to complete the task. It is also important to state prior assumptions, such as what the parameter represents and whether the function has a return value (because it cannot be inferred from the function definition).
    • Large segment code--Multiple lines of code to complete a single task should precede a comment describing the task.
    • Complex algorithms-If you use a unique way to solve a problem, explain how you do it in the comments. (This will not only help other people who browse your code, but also help you understand it the next time you check the code yourself)

Two. Proper naming of variables and functions

Proper naming of variables and functions is important for increasing the understanding of code. You must avoid the name of a useless variable that cannot represent the type of data that is contained. With the right nomenclature, the code reads like a story and is easier to understand.

The general rules for naming are as follows:

    • The variable name should be a noun such as car or person.
    • The function name should start with a verb, such as getName (). A function that returns a Boolean type value is usually preceded by an IS, such as isenable ().

    • Variables and functions should use logical names and don't worry about length. Length issues can be mitigated by post-processing and compression.

Three. Variable type transparency

Since variables are loosely typed in JavaScript, it is easy to forget which data types the variables should contain. The proper naming method can alleviate this problem to some extent, but it is not enough to put it in all cases. There are three ways to represent variable data types. The first way is to initialize. When a variable is defined, it should be initialized to a value that implies how it should be applied in the future. For example, a variable that holds a Boolean type value in the future should be initialized to true or false, and the variable that holds the number in the future should be initialized to a number, as shown in the following example:

    • Specifying variable types by initializing
    • var found = false; Boolean type
    • var count =-1; Digital
    • var name = ""; String
    • var person = null; Object

Initializing to a specific data type can be a good indication of the type of the variable. But the disadvantage is that it cannot be used for function arguments in function declarations.

The second method is to use the Hungarian notation to specify the variable type. Hungarian notation precede the variable name with one or more characters to represent the data type. This notation is popular in scripting languages and has long been the way JavaScript advocates. The most traditional Hungarian notation in JavaScript is to use a single character to represent the base type: "O" for the object, "s" for the string, "I" for the Integer, "F" for the floating-point number, "B" for the Boolean. As shown below:

    • Hungarian notation for specifying data types
    • var bfound; Boolean type
    • var ICount; Integer
    • var sName; String
    • var Operson; Object

The advantage of the Hungarian notation in JavaScript is that function parameters can be used as well. But its disadvantage is that the code is somehow difficult to read, hindering the intuitive nature of the code and the sentence style when it is not used. As a result, the Hungarian notation lost some of the developers ' favor.

The last way to specify variable types is to use type annotations. The type comment is placed to the right of the variable name, but before initialization. This is done by placing a comment of the specified type next to the variable, as follows:

Type comment for the specified type

    • var found/*:boolean*/= false;

These three methods specify the variable data type. Each has its own strengths and weaknesses and should be evaluated before use. The most important thing is to determine which one is best for your project and use it consistently.

Make your JS code more readable

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.