The Art of naming

Source: Internet
Author: User

"There are only two problems with computer science: Cache invalidation and naming. "--phil Kariton

Objective

Naming has always been a headache in my programming process, sometimes for a proper name is to think and think, but also can not help Google a bit. Naming is really an art, good name that call a tall on. Today, summarize some of the front-end naming conventions, comb one of your own named ideas, and get inspired from the following 4 code-style naming specification sections:

1. Google JavaScript Code style Guide

2. Crockford Code Specification

3. Dojo Javascript Programming Specification

4. jquery Core Style Guide

PS: Not listed, the representative did not make the specification of this kind of naming.

Javascript

1. Variables

Google JavaScript Code style Guide Small hump naming rules
Dojo Javascript Programming Specification Small hump naming rules
jquery Core Style Guide Small hump naming rules

Here is a point to clarify, many of the online Dojo Javascript programming specifications of the data said "variable name must be lowercase letter", I can not believe that, directly to the Dojo official website documents, the inside is not so said, but that is mixed case (is small hump).

-So the conclusion is that the variable uses " small hump naming rules ".

2. Constants

Google JavaScript Code style Guide Uppercase letters, underlined between words, example: var seconds_in_a_minute = 60;
Dojo Javascript Programming Specification Uppercase letters, underlined or big hump naming rules between words

Well, in fact, the Dojo Javascript programming specification does not say "capital letters, underline words", just said the capital letters, I do not like to add to the eye, I love this.

-So the conclusion is that constants use " uppercase letters, underlined words ".

3. Functions

Google JavaScript Code style Guide Small hump naming rules, example: Functionnameslikethis
Dojo Javascript Programming Specification The small hump naming rule must be a verb or a verb phrase. Example: Obj.getsomevalue ()
jquery Core Style Guide Small hump naming rules

This time I am very much in favor of the Dojo Javascript programming specification naming method, it must be a verb or a verb phrase. Functions are also methods, which are called different.

--so the conclusion is that the function name uses the " Small hump naming convention, which must be a verb or a verb phrase ."

4. Class

Google JavaScript Code style Guide Large hump naming convention, example: Classnameslikethis
Dojo Javascript Programming Specification Big Hump naming rules

JavaScript has no such thing as itself, it's just a simulation, essentially a function. But the naming is really different, and the constructor is named the same as the class.

-So the conclusion is that the class name uses the " Big hump naming convention ".

5. Enumeration variables

Google JavaScript Code style Guide Large hump naming convention, example: Enumnameslikethis
Dojo Javascript Programming Specification Big Hump naming rules

There is no controversy about this.

-So the conclusion is that the class name uses the " Big hump naming convention ".

Css

There are several guidelines for writing CSS:

1. Short ID or class naming, but make sure they are not too cryptic, or straightforward (such as #blue-button).

2. Large projects use prefixes, such as #xyz-help,. Xyz-column (sub-modules).

3. Use "-" for direct word spacing ("-" is used in the NEC Specification document).

Even if you don't use other humps or underscores, you have to unify your style, and don't use it for a while.

Ask yourself a self-answer

1. Is the word interval for css,class or ID used as a cross bar or an underscore or a camel?

I think that the choice of one, then the whole style is unified into that, do not like the wind like clouds and rain, anything. I suggest using a bar to look comfortable.

2. The file name should not hump type, the first letter to uppercase?

First, the file name must be concise, not smelly and long. There are many ways:

1. All lowercase, underline between words. Example: Photo_gallery.js

2. All lowercase, with a horizontal bar between the words. Example: Photo-gallery.js

3. All lowercase, without any character segmentation between words. Example: Photogallery.js

4. Small hump type. Example: Photogallery.js

I'm not sure which one to use, which is the most reliable, but it should not start with a capital letter.

3. Do you want to use the underscore prefix for private variables and functions?

The Dojo Javascript programming specification is required to have an underscore prefix, and no other specification is required. And I think the use of the underscore prefix is better, its great advantage is that it is very clear to distinguish between public variables and private variables, at a glance (there is a touch of a faint force lattice).

Summarize

The Crockford code specification does not have a description of the naming specification, perhaps I did not find it. On the contrary, the Dojo Javascript programming specification is a very detailed part of the naming convention, and there's a lot to learn from it, and I've taken something. and HTML itself is not recommended to use custom tags, so there is no naming problem, as for attributes, unified style on the line.

I often say in the article is a unified style, the reality of teamwork, the beginning can be unified under the norms, the overall code style is very good, and I look comfortable. The final name needs to be careful, concise, clear and understandable.

Reference documents

1. Translation: Google html, CSS and JavaScript style specification by Zhang Xin Asahi

2. "Google JavaScript code style Guide"

3. "Crockford Programming Specification" by Ruan Yi Feng

4. Code your own JavaScript writing –dojo JavaScript programming specification by Bin Go

This article for the original article, reproduced please retain the original source, convenient traceability, if there is the wrong place, thank you correct.

This address: http://www.cnblogs.com/lovesong/p/5423425.html

The Art of naming

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.