"Crazy Java Handout (3rd edition)". (Li Gang)--java naming rules and coding specifications

Source: Internet
Author: User
Tags abstract language function definition naming convention

1. Naming rules:

Here to draw on the other people's information, more comprehensive, easy to understand learning.

    1. The Java source file name of the Java source must be the same as the class name of the class defined in the source file.
    2. The first part of the package's named package name should be a lowercase ASCII character and be one of the top-level domain names, usually com, edu, gov, mil, net, org, or a national unique flag code defined by ISO standard 3166 and 1981. The subsequent parts of the package name are determined by the organization's internal naming conventions, which specify the directory name of each component, the department name, the project name, and so on.
    3. The named class name of the Class/interface should be the first letter capitalized noun. The naming should be concise and descriptive. The name of the exception class should end with exception. The interface naming convention is the same as class.
    4. The letters of a constant's named constant name should all be capitalized, the different words are connected by an underscore, and the name combination should give meaning.
    5. Name of the variable
      5.1 Normal variable The first letter of the ordinary variable name is lowercase, and the first letter of each other word is capitalized. It should be named with a brief and specific meaning, with a clear and concise presentation of its intended use to the user.
      5.2 Convention variables, called contract variables, are those temporary variables that can be discarded (throwaway) after they are used. Usually I, J, K, M, and n represent integer variables, and C, D, and e represent character variables.
    6. The first word of the method's named method name should be a verb, and the first letter is lowercase, with the first letter of each word capitalized.
    7. The name of the method parameter should be chosen as the parameter name for the method. If possible, select the same name as the field you want to assign the value to.

2. Supplementary information:

1. The first letter of the class name should be capitalized. The first letter of an attribute (member variable), method, object variable, and all identifiers (such as formal arguments, actual arguments, local variables) should be lowercase, and all words contained in it should be enclosed together, with the first letter of the middle word capitalized. For example:
Class Name: Thisisaclassname property or method Name: Thisismethodorfieldname object variable: thisisaclassvariable

2. Java package is a special case, they are all lowercase letters, even if the middle of the word is also true. For the global package, reverse your Internet domain name and add the package name. For example:
Cn.edu.usst.dingyuewei.package.
In addition, the package line is to be preceded by the import line, and the standard pack name in import is preceded by the local package name, and is sorted alphabetically. If the import row contains a different subdirectory in the same package, it should be handled with *. For example:

package hotlava.net.stats; import java.io.*; import java.util.Observable; import hotlava.util.Application; 

Here java.io.* is used instead of InputStream and OutputStream.

3. Interface (Interface): Use full English descriptors to describe the interface encapsulation, the first letter of all words capitalized. Traditionally, the name is appended with the suffix able, ible or ER. For example: Contactable,prompter.

4, the component (Component) uses the complete English description to explain the use of the component, the end Bell the component type. For example: Okbutton,customerlist,filemenu.

5, the name of common methods in the class:

A) The method of obtaining a class (typically with a return value) is generally required to be accessed by the method name using the field name, preceded by a prefix get, such as Getfirstname (), Getlastname ().

b) The method of judging the Boolean type of the class generally requires that the method name be prefixed with the word is, such as ispersistent (), isstring (). Or use words that have logical meanings, such as equal or equals

c) The method of setting the class (the general return type is void): prefix the accessed field name with set, such as Setfirstname (), Setlastname (), Setwarpspeed ()

D) General methods of the class generally use the full English description of the member method function, the first word as far as possible to use a vivid verb, the first letter lowercase, such as openFile (), AddAccount ().

e) The construction method should be written in an incremental way (for example: the number of arguments is written in the back). For example:
Public CounterSet () {}
public counterset (int size) {this.size = size;}

F) ToString method: In general, each class should define the ToString method in the format: public String tostring () {...}

g) You should generally consider placing a main () method that contains the code used to test that class, and if it contains the main () method, it should be written at the bottom of the class.

6. Static constant fields are generally all uppercase letters, and the words are separated by underscores (there are exceptions, such as the Java class Library about color constants are not strictly all uppercase letters). such as Min_balance,default_date.

7. Cyclic counting variables usually use letters I,J,K or counter. The array should always be named in the following way: objecttype[] or byte[] buffer.

3. Code specification

1. Indent
Indent suggestions are in 4 spaces. It is recommended that the block ident for the editor page in the Tools/editor Options be 4,tab Size 8. Preprocessing statements, global data, headings, additional instructions, function descriptions, labels, etc. are all shelf written. The statement block's "{", "}" pairs are aligned and aligned with its previous line, and the statement indentation of the statement block class suggests that each "{", "}" should have a separate row for the pair. The default way in JBuilder is to start with "{" Not a single line, and it is recommended to change to the above format (select Braces as Next in the Project/default Project Properties setting in the Code Style).
2, Space
In principle, variables, classes, constant data, and functions are appropriately spaced between their types, decorated names, and aligned according to circumstances. Keyword principle above a lattice, such as: if (...) and so on. The spaces for the operators are defined as follows: "::", "-", "[", "]", "+ +", "–", "~", "!", "+", "-" (The Sign), "&" (reference), and so on the two sides without spaces (where the monocular operator refers to the side connected to the operand), Other operators (including most two-mesh operators and three-mesh operators, "?:") Add a space on both sides, and can be used as a function definition when the glyd is justified, but not when the function is implemented. The "," operator is empty only after the next, and it is not empty or multiple spaces when it is required to be aligned. Whether or not there is a parenthesis, apply the appropriate spaces to the statements that are added to the statement line, separating them from the statement and aligning as possible. Personally, this can be determined according to the personal habits of the decision to follow or not.
3. Align to
In principle, a closely related line should be aligned, and the alignment includes parts such as type, adornment, name, parameter, and so on. The other line should not be longer than the screen too much, if necessary, wrap the line as appropriate, and whenever possible, at the "," or operator, the line is preferably preceded by an operator, and the following lines are indented with the first line of the statement, but the statement is still subject to the indentation of the first line, i.e. its next behavior "{" should be aligned with the first line.
Variable definitions are best aligned by adding spaces, and variables of the same type are best placed together. As shown in the following example:
int Value;
int Result;
int Length;
Object Currententry;
Personally, this can be determined according to the personal habits of the decision to follow or not.
4, empty line
There must be no irregular empty lines, such as 10 consecutive blank lines. program file structure between the parts of empty two lines, if not necessary also can only empty line, the function is generally empty two lines between the implementation, because each function also has a function description comment, it is usually only a blank line or not empty, but for no function description of the situation should be at least a blank line. The function that writes to oneself, the suggestion also adds "//--" to do separates. There should be at least one line between the function's internal data and the code, where the code should be empty, and it is recommended that the variable declaration appear in the code before it is empty. At least one line between the four "P" in the class, and the data in it should be blank between the functions.
5. Comments
Annotations are the specific manifestation of software readability. The program annotation amount generally occupies 20% of the program code, software engineering requirements of not less than 20%. Program annotations can not be used in abstract language, similar to "processing", "loop" such as the computer abstract language, to accurately express the program's processing instructions. For example: "Calculating net demand", "calculating the working hours of the first operation" and so on. Avoid using annotations for each line of the program, and you can add a comment to the front of a program with explicit processing logic.
Annotations are necessary, but should not be too much, do not passively write comments for writing notes. Here are four essential notes:
A. title, additional instructions.
B. A description of the function, class, and so on. For almost every function should be properly described, usually added before the function implementation, in the absence of the function implementation part of the case is added to the function prototype, its content is the function of functions, purposes, algorithms, such as description, parameter description, return value description, if necessary, some such as special hardware and software requirements and other instructions. The Declaration of common functions and common classes must be annotated with instructions on how to use them and design ideas, and choosing the proper naming format will help you to explain things more clearly.
C. There must be a certain description in the code that is not clear or not portable.
D. And a small amount of other annotations, such as comments on custom variables, code writing time, and so on.
Note there are block comments and line comments, respectively, refers to: "/**/" and "//" proposed for a with block comments, D with line comments, B, C, as the case may be, but should be unified, at least in one unit, the type B annotation should be unified. Specific comments on the different files and structures are explained later in detail.
6. Code length
For each function it is recommended to control as much as possible the code length of 53 lines, more than 53 lines of code to reconsider splitting it into two or more than two functions. The function splitting rule should be based on the original algorithm without destroying it, while the split part should be reusable. For repetitive code that is used in multiple modules or forms, it is entirely possible to become a function of a common nature and put it in a common module.
7, Page width
The page width should be set to 80 characters. The source code generally does not exceed this width and results in a full display, but this setting can also be flexibly adjusted. In any case, an extra-long statement should be wrapped around a comma or an operator. When a statement is wrapped, it should be indented 2 characters longer than the original statement.
8, line number
General integrated programming environment, each screen probably can only show no more than 50 lines of the program, so this function is about 5-6 screen display, in some circumstances to 8 screen around to display. This makes it difficult to read a program or modify a program. It is therefore advisable to extract the block that completes the comparison of independent functions as a single function. A block that accomplishes the same or similar function is extracted independently of a sub-function. It can be found that the simpler the upper-level function is, the more specific work is done by invoking a few sub-functions, the more the underlying function completes. This is a sign of a good program. In this way, we can easily control the logic of the whole program in the upper function, and focus on the realization of some function in the lower function.

"Crazy Java Handout (3rd edition)". (Li Gang)--java naming rules and coding specifications

Related Article

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.