Learn Java "3" from the beginning--the method of naming

Source: Internet
Author: User
Tags naming convention

1. Naming law

For variables, arrays, methods/functions, classes, and objects, it is important to have a canonical name, especially if the size of the program expands and the number of variables surges. The more famous naming conventions are:

(1) Hungarian nomenclature

This naming method is proposed by Microsoft programmer Charles Simonyi Charles Simonyi, which is commonly used in the C language. The name of the identifier is prefixed with one or more lowercase letters, followed by a word or combinations of words that are capitalized in the first letter, which indicates the purpose of the variable. For example: Lpszstr, which represents a long-pointer (LP) variable that points to a string (SZ) ending with '/'.

(2) The popular Camel name law

There are many new libraries and Java platforms that are used properly. It is characterized by the first word all lowercase, the next word capitalized, so it looks like a camel's back, there are ups and downs.

(3) Pascal (PASCAL) nomenclature

Pascal (Pascal) nomenclature is similar to the Camel nomenclature, except that the camel nomenclature is the first lowercase letter, while the Pascal nomenclature is the first letter of the word.

(4) Underline naming method

Another popular naming convention is called underline naming, where each word is separated by an underscore. The underline is popular with the advent of the C language, in environments such as Unix/liunx, and in the GNU code.

For a number of reasons, the Java design began with the hump nomenclature, and the vast majority of Java programmers follow this specification in their code.

The hump naming method has the following specifications:
    • Using a meaningful English word or abbreviation , It is not recommended to use pinyin, meaningless words, single letters, numeric numbers
    • Variables are made up of multiple English words , with no gaps in underline, dashes, dots, or other symbols
    • The first word is all lowercase, starting with the second word, capitalized to break each word
    • Variable words should be able to clearly and accurately express the meaning and function of this variable

For example, now we need to use a variable to represent the population of China, it can be named: Populationofchina. If the gross national product of Henan Province is Gdpofhenan, it is here to make it clear that the GDP is lowercase. Sometimes it can even be a word: iftheinternetisaccessible to represent a Boolean variable that the network is up to state.

2. Code writing

Good code writing habits can enhance the readability of your code. In the process of team development, the consistency of code writing specification is the precondition of effective communication and cooperation.

(1) Indent and line feed

Indenting and wrapping can enhance the readability of your code. The principle of indentation:

    • If the previous line is an opening curly brace ("curly brace"{), this line is typically indented in a single unit relative to the position of the brackets.
    • If the previous behavior is if,for,while, else, do, and the next line is not a curly brace, the next line must be indented.
    • Indents Use the TAB key (on the left side of Q on the keyboard) and should not use spaces. This is not only convenient, but also to achieve a uniform distance, the number of spaces is difficult to control, and can not be recognized by the editor as indentation.

The principle of line wrapping:

    • Wrap after a semicolon
    • Wrap a line after the curly brace that represents the block: {wrap-wrap} Wrap Line
    • If,for,while, else, do after line wrapping
(2)Space

The use of spaces can make a statement look less crowded, and each variable symbol can be clearly distinguished.

The use of spaces is different, the following methods are for reference only:

    • In =,+,-,*,/, before and after a space, the number should be preceded by a space, such as:
b;
(3) empty Line

Empty lines play a role in dividing the area, making it easier to read code, and to see which parts of the code are made up.

(4) brackets

Parentheses are critical for the correctness of code writing. To ensure that the parentheses are not missing, you can use the following method:

Method One: Each time the parentheses are written with a pair of parentheses at the same time, so there is no forgetting to write the end of the parentheses problem. Method Two: Using the IDE's brace matching and auto-completion function, in Eclipse, just write the next parenthesis, the program can automatically fill the other half, very convenient.

(5) Notes

Annotations Help the program to be better understood, so clear, concise, and understandable annotations are the embodiment of a good code style.

The information conveyed by the note includes:

    • The author, date, and version of the Code are introduced at the beginning
    • Describe the respective roles of each part
    • Describe the process of solving the problem

There are two types of annotation methods in Java that are inherited from C + +.

The first is a line comment, a comment method used by C + +, with two slashes to represent the contents of the following until the end of the line is the comment content.

Comments can be placed at the beginning of the document//at the beginning of each line with a double slash helloword{ //can also be placed in the middle of the Code main(args[]) {//can be placed at the end of the C16>system.  Out.  println("Hello world!" ); //Use tab to align } }              

There are a number of reasons why this annotation method looks more "modern": it is easy to enter and is not prone to error.

The second is the area annotation, the C language used by the annotation method,/* and/* and the middle of the content will be as a comment, when unable to pair,/* is considered non-comment, and/* After the entire content is considered a comment.

 /******************* * usually people at the beginning of the document *  * make a note *  *******************/public class helloWorld{/* can also be placed in the middle of the code * * public static void Span class= "NF" >main (string args[]) { /* can be placed at the end of the line */system.. ( "Hellow world!" /* with tab alignment */} }         

One of the drawbacks of this annotation is that when you accidentally delete only one side of a pair of annotation symbols, you can create an error. In addition, the input is also very troublesome.

Learn Java "3" from the beginning--the method 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.