Java Language Programming format specification

Source: Internet
Author: User
Tags abstract language function prototype

Directory

                    1. 1. Preface
                    2. 2. Scope of trial
                    3. 3. Java Naming conventions--
                    4. 3.1 Public conventions
                    5. 3.2 Java files, packages
                    6. 3.3 Class, interface naming conventions
                    7. 3.4 Method Naming conventions
                    8. 3.5 Constants
                    9. 3.6 Variables and parameters
                    10. 3.7 Components/Parts
                    11. 3.8 Integrated
                    12. 3.9 Mysteries of the number
                    13. 3.10 Other
                    14. 3.11 Java Exceptions
                    15. 3.12 Array naming
                    16. 3.13 database table naming rules
                    17. 3.14 database Field Naming conventions
                    18. 3.15 jsp file naming
                    19. 3.16 Servlet class naming
                    20. 4. Writing format specification
                    21. 4.1 Indent
                    22. 4.2 Spaces
                    23. 4.3 alignment
                    24. 4.4 Blank Line
                    25. 4.5 Notes
                    26. 5. code file Style

1. Preface
In order to ensure the readability of the system source program, thereby enhancing the maintainability of the system, the following programming specifications are developed to standardize the programming of the system. Source programs in other resources that are inherited by the system should also be modified according to this specification.
2. Scope of trial
This document will act as a programming format specification for the development of Hebei post application system software (Java language). Strict adherence is required during the coding, testing and maintenance of the system.

3. Java Naming conventions
3.1 Public engagements? Naming refers to naming the source file name, method, directory, package, database table name, database field, and so on in the system. The names used in the system are expressed in English abbreviations.
? The name is composed of the prefix +{named word}, the prefix to express the purpose of naming, such as database tables. The named prefix is determined by the project group.
?
The names are all lowercase except for objects other than the system public maintenance object. One way to use "_" as a connector, the other way is the first letter of the morphemes are directly connected together. In this specification, the database name, database field name in the first way, the rest of the second way.
?
Use full English descriptors that can accurately describe variables/fields/classes/interfaces/packages, etc. For example, using a name like Firstname,listallusers or Corporatecustomer, it is forbidden to name Hanyu Pinyin and unrelated words, although Java supports Unicode naming, but this specification provides for packages, classes, interfaces, methods, variables, fields, etc. must not be named using Chinese characters etc.
? Use mixed case to improve the readability of the name. In general, lowercase letters should be used, but the first letter of the name of the class and interface, and the first letter of any intermediate word, should be capitalized. Package name all lowercase.? Minimize the use of abbreviations, but if you must use, when the use of public abbreviations and customary abbreviations, such as implementation (implement) can be reduced to write impl, managers (manager) can be MGR, etc., see the table below, strictly prohibit the misuse of abbreviations. Avoid the use of long names (preferably no more than 25 letters).

Avoid using numbers, but use 2 instead of to, use 4 instead of for, such as: go2jsp.

3.2 java files, packages? The file name is exactly the same as its class, with all words capitalized in the first letter. The package name is usually named after the project or module name, with fewer abbreviations and long names, all lowercase.  Basic Package: Com.czpost, all packages, files belong to this package. The package name consists of the following rules: [Basic Package]. [Project name]. [Module name].  [Sub-module name] ... such as: Com.czpost.eims Com.hepost.eims.until "? You must not define the class directly under the base package, and all classes, interfaces, and so on in the project are defined in the respective project and module packages.

3.3 Class, interface naming specification? Capitalize all words in the first letter. Use words that respond exactly to the class, the meaning of the interface, the function, and so on. Generally use nouns.?  Interface can be prefixed with I or able, ible, er and other suffixes. 3.4 Method Naming specification methods should be named with the full English descriptor, mixed in case: capitalize the first letter of all intermediate words. The first word of the French name often uses a verb with a strong action color.  The value class uses the Get prefix, which uses the set prefix to determine that the class uses the IS prefix.  Example: GetName () Setsarry () Islogon () method parameter suggested order: (by operator, action content, Operation Flag, others?) Example: public void replace (string sourcestr, String oldstr, String newstr) {...}

3.5 Constants Use a complete English capital word, which is connected by an underscore between words and words, such as: Default_value

3.6   variables and parameters   variables The Hungarian nomenclature is proposed, and the morpheme rules are shown in the "Public agreement"? Integer variables (including Int,short,long): Start with N, for example:long  ncrednum; Floating-point variables (including float,double): Starting with F, e.g.:double  ftranamt;? Character variable: starts with C, for example: Char cpageflag;? Date Variant: Starts with D, for example: Date dtoday;? Boolean variable: Starts with IS, for example: Boolean Isflag;? String variable: Start with S, for example: Char spageno[2+1];? Global variables: The type of data that begins with G, for example: Char gsbankno[10+1];? Static variables: Static variables are also global variables in the file, naming rules are the same as global variables. System-Level variables: System-level variables are also global variables, with ' _ ' beginning with the type of data. For example: Char _sbankno[10+1]  does not have ' G '. Temporary variables are usually named I,j,k,m and N, which are generally used for integer types; c,d,e, which are generally used for character types.

3.7 Components/components should be in full English descriptor naming components (interface parts), followed by the Hungarian nomenclature such as: Btnok,lblname. Button variable Btn+xxxxxxx For example: Btnsave, btn Exit, btnprint, etc? Title Variable lbl+xxxxxxxx For example: Lblname, Lblsex, etc. object type prefix Canvas CVS CheckBox chk Image img List LST Choi Ce chc Dialog dlg Event evt Frame frm Menu menu Panel pnl TextArea txa TextField t Xf

3.8 Sets a collection, such as arrays and vectors, that should be named in complex numbers to represent the types of objects held in the queue. The name should be in full English descriptors, the first letter of all non-beginning words in the name should be capitalized, appropriate use of the set abbreviation prefix. such as: vector vproducts = new vector (); Product vector Array aryusers = new Array (); User List

3.9 Mysterious number programs often use some amount, it has a specific meaning, the mysterious number is defined as a constant. Note that the name of the constant should be able to express the meaning of the number and should be capitalized to distinguish it from the identifier corresponding to the variable. For example, 50 this number can be defined as a constant named Num_of_employees instead.

3.10 Other names should use complex numbers to indicate that they represent multiple values. such as: OrderItems.

The 3.11 Java Exception class name consists of words and exception that represent the exception type, such as Actionexception. Exception instances generally use E, ex, etc., in multiple exceptions using the exception name or shorthand plus e,ex, such as: Sqlex Actionex

3.12 array-named arrays should always be named in the following way: byte[] buffer; Rather than byte buffer[];

3.13 database table naming rules? The prefix is separated from the named morphemes by an underscore. The name is made up of lowercase letters. If the noun is composed of a single word, it can be fully spelled, and if it is made up of multiple words, use abbreviations. Underline between abbreviations. For example:? Library table: Start with ' T_ '? View: Start with ' v_ '? Library Table index: Start with ' t_ ' and End With ' _idx '? Library Table Unique index: Start with ' t_ ' and End With ' _uidx '

3.14 database field naming rules? If the noun is composed of a single word, it can be fully spelled, and if it is made up of multiple words, use abbreviations. Underline between abbreviations. The name is made up of lowercase letters. No prefix.

3.15 jsp file naming uses the full English description to explain the functions of JSP, including as far as possible a vivid verb, the first letter lowercase, such as: viewmessage.jsp, edituser.jsp or forumchooser.jsp, etc.

3.16 servlet class naming generally corresponds to the service of the object to be named after the suffix service, such as: Userservice,tradeservice.

4. Writing format specification Strict requirements Writing format is to make the program neat and beautiful, easy to read, style unified, the programmer should have a clear understanding of the need for standard writing. It is recommended that the source program be developed using the Eclipse tool, which is set up in advance in the tool.

4.1 Indentation Indentation is recommended in 4 spaces. 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 in Sclipse is that the start of "{" is not a single line, and it is recommended to change to the above format.

4.2 Spaces 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 space for the operator is 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.

4.3 Alignment 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 or tab keys, and variables of the same type are best placed together. As shown in the following example: int nvalue; int nresult; int nlength;

4.4 Empty lines must not have an empty line without rules, such as 10 consecutive blank lines. There is an empty line between the parts of the program file structure, and since each function also has a function description comment, it usually requires only a blank line or no space, but at least one line should be empty for cases where there is no function description. It is also recommended to add "//------" to the function you write. 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.

 4.5   comment Annotations are a concrete 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. 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:? Title, additional instructions.? 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, but choosing the right naming format will help you to explain things more clearly. There must be some description in the code that is not clear or not portable. and a small amount of other comments, such as comments on custom variables, code writing time, and so on.

5.  code file style all Java (*.java) files must adhere to the following style rules:? File generation for canonical JAVA derived classes, try to use the Eclipse tool to generate file formats, avoid the use of hand-crafted header files/implementation files. Package/import Package line before the import line, the standard pack name in import is to be preceded by the local package name, and in alphabetical order. If the import row contains a different subdirectory in the same package, it should be handled with *.   Package hotlava.net.stats; Import java.io.*; Import java.util.Observable; Import hotlava.util.application;   here java.io.* used instead of InputStream and OutputStream.? File Header Comment File header comment is mainly to indicate that the file is some information, is the general description of the program, can enhance the readability and maintainability of the program. File header comments are typically placed after the Package/imports statement, before the Class description. Requires that at least the file name, creator, creation time, and content description be written out. The format should be as restrictive as possible:/** *title:  determines the mouse position class * Description: Determines which job field the mouse is currently in and returns the job Number * @Copyright: Copyright (c) 2002 * @Company: HI T * @author: Rivershan * @version: 1.0 * *? Class is followed by the comments of the classes, which are generally used to interpret classes.  /** *a class representing A set of packet and byte counters * It is observable-allow it-be watched, but only& nbsp;  * Reports changes when the current set is complete  */  Next is the class definition, which contains the extends and implements  on different rows public class CounterSet extends Observable iMplements cloneable ? Class fields

Next is the member variable of the class:/** *packet Counters */protected int[] packets; The member variable for public must generate a document (JAVADOC). The member variables defined by the proceted, private, and package can have no comments if the name has a clear meaning. The access method is then the method of accessing the class variable. It is simply used to get the value of the class variable assignment, which can be simply written on one line. (personal opinion as far as possible branch writing)

/** *get The counters * @return An array containing the statistical data. This is an array with been * freshly allocated and can be modified by the caller. */Public int[] Getpackets () {

return Copyarray (packets, offset);

} public int[] GetBytes () {

Return Copyarray (bytes, offset);

}

Public int[] Getpackets () {

return packets;

} public void Setpackets (int[] packets) {

This.packets = packets;

}

Other ways not to write on a line?  The constructor is followed by a constructor, which should be written in an incremental way (for example, the number of arguments is written in the back). Type of access ("public", "private", etc.) and any "static", "final" or "synchronized" should be in one row, and the method and parameter write another line, which makes the method and parameters easier to read.

public counterset (int size) {

this.size = size;

} ?

Cloning method if the class can be cloned, then the next step is the Clone method: Public Object Clone () {

try {

CounterSet obj = (counterset) super.clone (); Obj.packets = (int[]) packets.clone (); obj.size = size; return obj;

} catch (Clonenotsupportedexception e) {

throw new Internalerror ("Unexpected clonenotsupportedexception:" + e.getmessage ());

}

}  ?

Class method below begins the method of writing the class:

/** *set the packet counters * (such as when restoring from a database) */protected final void SetArray (int[] R1, int[] R 2, int[] R3, int[] R4) throws IllegalArgumentException {

Ensure the arrays is of equal size//if (r1.length! = R2.length | | r1.length! = R3.length | | r1.len Gth! = r4.length) throw new IllegalArgumentException ("Arrays must be of the same size"; System.arraycopy (R1, 0, R3, 0, R1.length); System.arraycopy (R2, 0, R4, 0, r1.length); } ? ToString method In any case, each class should define a ToString method: Public String tostring () {

String retval = "CounterSet:";

for (int i = 0; i < data.length (); i++) {

retval + = data.bytes.toString ();

retval + = data.packets.toString ();

}

return retval;

} ?

The main method if the main (string[]) method is already defined, it should be written at the bottom of the class.

Java Language Programming format specification

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.