As a developer, there must be strict code specifications. For this reason, I summarized some code specification cases.
Table of contents
1. Foreword
2. Trial range
3. Java naming specification-
3.1 Public agreement
3.2 Java files, bags, bags
3.3 Class, interface naming specifications
3.4 Method Naming Specification
3.5 constant
3.6 variables and parameters
3.7 component/component
3.8
3.9 Mysterious Number
3.10 others
3.11 java exception
3.12 array naming
3.13 Database table naming rules
3.14 Database field naming rules
3.15 jsp file name
3.16 Servlet name
4. Writing format specification
4.1 Endix
4.2 space
4.3 Alignment
4.4 empty line
4.5 Note
5. Code file style
1. Foreword to ensure the readability of the system source program, thereby enhancing the system maintenance of the system, and formulate the following programming specifications to regulate the programming of the system. The source programs in other resources inherited by the system should also be modified accordingly according to this specification.
2. Trial scope of this document will be used as a programming format for the development of Hebei Postal Application System Software (Java Language). During the coding, testing and maintenance process of the system, it is required to be strictly observed.
3. Java naming specification
3.1 Naming of the source file name, method, directory, package, database table name, database field, etc. in the name of the source file name, method, directory, package, database table name, database table name, database field. Bleak
The naming used by the system is expressed in English abbreviations. Bleak
The naming method is: prefix+{naming word abbreviation}, prefix expresses the use of naming, such as database tables. Bleak
The naming prefix is determined by the project team. Bleak
Other objects except the system public maintenance objects, all naming uses a lowercase. One way uses "_" as a connection symbol, and the other is to connect the first letters of the word prime directly. In this specification, the database name and database field name adopt the first way, and the rest are in the second way. Bleak
Using can accurately illustrate the complete English descriptor of variable/field/class/interface/bag. For example, using names like FirstName, Listallusers, or CorporateCustomer, it is strictly forbidden to use Chinese Pinyin and unrelated words. Although Java supports Nicode names, this specification stipulates that it is not allowed to use Chinese characters such as packaging, classes, interfaces, methods, variables, fields, etc. Named
Using a lowercase mixing to improve the readability of the name. Generally, lowercase letters should be used, but the first letters of the names and interface names, and the first letter of any intermediate word should be uppercase. All the package names. Bleak
Try to use as little abbreviations, but if you must use it, when you use public abbreviations and habitual abbreviations, such as Implement can be abbreviated as IMPL, managers (manager) can be abbreviated as MGR, etc. For details, please refer to abuse of abuse. (Avoid using long names (preferably not more than 25 letters).
Avoid using numbers, but 2 can be used instead of TO, 4 instead of for, etc., such as: Go2JSP.
3.2 Java file, bag
The file name should be the same as its class, and the first letter of the word is capitalized. Bleak
The package name is generally named after projects or modules, and uses less abbreviations and long names. Bleak
Basic package: COM.CZPost, all packages and files belong to this package.
The package name is composed of the following rules: [Basic Pack]. [Project Name]. [Module Name]. [Sub -module Name] ... For example: com.czpost.eims com.hepost.until
The class must not be defined directly under the basic package. The classes and interfaces in all items should be defined in their respective projects and module packages.
3.3 Category and interface naming specifications All words of all words. Use words that can be exactly the same, interface meaning, function, etc. Generally uses nouns. Bleak
The interface can bring I prefix or ABLE, IBLE, E and other suffix.
3.4 Method Naming Specification Method The Naming of the Method of the Naming Standard should use a complete English descriptors, mixed use of a lowercase: The first letters of all intermediate words. The first word of the method of the method often uses a verb with a strong action color. The value of the value uses the GET prefix, the value class uses the SET prefix, and the judgment class uses IS (has) prefix. Example: GetName () Setsarry () Islogon () Method parameter Sequence: (by the operator, operating content, operating logo, others) Example: Public Void Replace (String SourceStr, String Oldstr, String Newstr) {..... ....}
3.5 The constant uses a complete English capitalized word, and connects to the line between words and words, such as: default_value
3.6 Variables and parameter variables are recommended to use Hungarian naming method.
Platform variable (including int, short, long): Start with n, for example: long ncrednum;
Floating -point variables (including Float, Double): Start with F, for example: Double Ftranamt;
Character variable: start with C, for example: char cpageflag;
Date variable: Start with D, for example: date dtoday; Boolean variable: start with IS, for example: boolean isflag;
String variables: start with S, for example: char spageno [2+1];
Global variables: the type of data with G, for example: CHSBANKNO [10+1];
Static variables: Static variables are also global variables in files, and naming rules are the same as global variables. Bleak
System -level variables: System -level variables are also global variables.
For example: char _sbankno [10+1] There is no 'g'.
Temporary variables are usually named I, J, K, M and N, and they are generally used for integer; C, D, E, they are generally used for character types.
3.7 Components/components shall adopt a complete English descriptor naming component (interface component), followed by Hungarian naming rules such as: BTNOK, LBLNAME. T button variable BTN+xxxxxxx, for example: BTNSAVE, BTN Exit, BTNPrint, etc. Choice CHC DIALOG DLG EVT EVT FRM Menu Menu Panel PNL Textarea TXA TextField TXF
3.8 A collection, such as array and vector, must be named by multiple numbers to represent the type of object stored in the queue. The naming should use a complete English descriptor. The first letters of all non -beginning words in the name should be uppercase, and the collection of epithelial abbreviation prefixes appropriately. Such as: Vector vproduts = New Vector (); // Product vector array Aryusers = New Array (); // User list
3.9 Mysterious number programs often use some quantities. It has a specific meaning and defines the mysterious number as a constant. Note that the naming of this constant should express the significance of the number, and it should be all uppercase, which is distinguished from the identifier corresponding to the variable. For example, the number 50 can be defined as a constant replacement called Num_of_emPloyees.
3.10 Doubles should be used when other naming indicates that they represent multiple values. Such as: Orderitems.
3.11 Java abnormal abnormal category consists of the words that represent the abnormal type and Exception, such as ActionXception.
Abnormal instances generally use E, EX, etc. In multiple abnormalities, use this abnormal name or abbreviation to add E, EX, etc.
3.12 array naming arrays should always be named in the following way: byte [] buffer; not byte buffer [];
3.13 The naming rules of the database table are separated by the lower lines between the prefix and the naming word.字 Name it consists of a lowercase letter. Bleak
If the naming word is composed of a single word, it can be used for full spelling; if it is a multi -word composition and abbreviation. Between abbreviations, use the lower line. For example: 库 例 例: Start with 'T_' View: Start with 'v_' 例 例 例: Began with 'T_', and the only index of the library table at the end of '_idx': 'End
3.14 Database field naming rules
If the naming word is composed of a single word, it can be used for full spelling; if it is a multi -word composition and abbreviation. Between abbreviations, use the lower line.字 Name it consists of a lowercase letter. There is no prefix.
3.15 JSP file naming uses a complete English description to indicate the function completed by JSP, including a vivid verb, the first letters, such as: ViewMessage.jsp, Edituser.jsp or FORUMCHOOSER.JSP, etc.
3.16 The name of the Servlet class generally corresponds to the objects served with the suffix service, such as: UserService, TradeService, etc.
4. Writing format Strictly requires the writing format to make the program neat, easy to read, and unified style. Programmers must have a clear understanding of the necessity of normative writing. It is recommended that the source program develops the Eclipse tool, and the format specifications are pre -set in the tool.
4.1 Innsy and indentation Suggestions are based on 4 empty grids. Pre -processing statements, global data, title, additional description, function description, label, etc. are all written. The "{" and "}" of the sentence block are aligned and aligned with the previous line. The statement of the statement block is recommended to each "{", "}" separate one line, which is convenient for the pair. The default method in Sclipse is the beginning "{" is not a separate line, and it is recommended to change to the above format.
4.2 In principle, variables, classes, constant data, and functions in the space in principle are in its type, the modified name is properly spaced and aligned according to the situation. One can be used in the principle of keywords, such as: if (...) and so on. The space requirements of the operator are as follows: "::", "-> "," ["," "", "++", "-", "~", ""! ","+"-" (Refers to the positive number), "&" (quoted) and other operators such as the two sides of the calculatory not without spaces (of which the single -eye computing symbol system refers to the connected side of the operation), other operators (including most two -purpose operators and operators and operators and "Three -eyed operator"?: "One empty grid on both sides, can be aligned according to the situation when the function is defined, but it can be used when the function is implemented." The grid can be not empty or multi -space when you need to align.
4.3 The close lines of alignment should be aligned in principle, and alignment includes all parts such as types, modifications, names, and parameters. The length of each line should not exceed the screen too much. If necessary, change the line appropriately, and try to be as "at the" place or operational symbol when the bank is changed. Entry, but the statement is still based on the indentation of the first line, that is, if its next line is "{", it should be aligned with the first line. Variable definitions are best to align with adding spaces or TAB keys, and variables of the same type are best together. As shown in the following example: int nvalue; int nresult; int Nlenth;
4.4 There must be irregular empty lines in the empty line, such as ten consecutive empty lines. Each part of the program file structure is empty. Because each function also has a function description annotation, it usually only needs to be empty or not, but at least one line should be empty for the situation without function description. For the functions you write, it is recommended to add "// -------" to separate. At least one line should be empty between the internal data of the function and the code. The appropriate place in the code should be open in the air. It is recommended that when a variable statement appears in the code, it should be empty in front of it. There are at least one line between the four "P" in the class, and the data and functions should also be empty.
4.5 Note Notes are the specific embodiment of the readability of software. Program annotations generally account for 20%of the number of program coding, and software engineering requirements are not less than 20%. Program annotations cannot be used in abstract languages such as "processing" and "cycle" in abstract language. It is necessary to accurately express the processing description of the program. Avoid comments from each row of procedures, you can add a comment in front of a program, with clear processing logic. Comments are essential, but too much should be too much. Do not passively write annotations for writing comments. The following are four necessary notes: Title and additional description.的 Explanation of functions, classes, etc. There should be appropriate explanations for almost every function. Usually adding before the function implementation, it is added before the functional prototype without function implementation. The content is mainly explained by the function, purpose, algorithm, etc. , Return value description, etc., if necessary, there must be some instructions such as special software and hardware requirements. The statement of public functions and public classes must be explained by the method and design ideas. Of course, choosing the proper naming format can help you explain things clearly. Bleak
There must be a certain explanation in the unclear or non -transplant. Bleak
And a small amount of other notes, such as comments of custom variables, code writing time, etc.
5. Code file style All Java (*. Java) files must follow the following style rules:
For the file generation, for the standardized Java derivative class, try to use the Eclipse tool to generate the file format to avoid hand -made header files/implementation files. E Package/Import Package line must be before the Import line, the standard package name in the Import is before the local package name, and arranged in the order of letters. If the Import row contains different sub -directory in the same package, it should be processed with*.
Package Hotlava.net.stats; Import Java.io.*; Import Java.util.observable; Import Hotlava.util.application; TPutstream. Bleak
The head annotation of the file head annotation file mainly shows some information of the file, which is the overall description of the program, which can enhance the readability and maintenance of the program. The file head annotation is generally behind the Package/Imports statement, and the Class description. It is required to write at least file names, creators, creation time and content descriptions. The format should be constrained as much as possible: /*** title: OK to determine the mouse position class* description: Determine which job field is now and returns the job number* @copyright: Copyright (C) 2002* @Company: Hit* @authoror : Rivershan * @Version: 1.0 */ class The next is class comments, which are generally used to explain the class. /** * a class repressenting a set of packet and byte count technus * it is observable to allow it to be watchet, but only * reports changes when the current set is Complet E */ Next is a class definition, which contains different lines EXTENDS and Implements Public Class CounterSet EXTENDS OBSERVable Implements Cloneable Class Fields
Next is a member variable: / ** *Packet Count * / Protected int [] packets; Public member variables must generate documents (Javadoc). If the member variables defined by Procity, Private, and Package are clear, there is no comment.是 The access method is the way to access the variable. It is just to simply use the variables of the class to get the value, which can be simply written on the line. (Personally think that try to write as much as possible)
/** * get the counters * @Return an array containing the statistics data. This array has ben * freshly alloCated and can be modified by the callr. */
Public int [] getpackets () {Return Copyarray (Packets, Offset);} Public int [] getbytes () {Return CopyArray (Bytes, Office) ] Getpackets () {Return Packets;} Public Void Setpackets ( int [] packets) {This.packets = Packets;}
Other methods should not be written on a line of constructor. Next is the constructor. It should be written in an increasing method (such as: more parameters are written behind). Access types ("Public", "Private", etc., and any "static", "Final" or "Synchronized" should be in one line, and the methods and parameters can be written one by one. This can make methods and parameters easier to read.
public counterset (int size) {
this.size = size;
}
If the clone method can be cloned, the next step is the clone method: public object clone () {)
try {
Count object obj = (Countrset) Super.clone (); obj.packets = (int []) packets.clone (); obj.size = size; return obj;
} Catch (ClonenotSupportedException E) {{
Throw New Internet ("UNEXPECTED ClONENOTSUPPORTException:" + E.getMessage ());
}
}
The class method began to write a class method:
/ ** *Set the Packet Count * GALARGUMENTEXCETION {
// Ensure the arrays are of equal size // if (R1.length! = R2.length || R1.length! = R3.length || R1.length! = R4.Length) Ion ("Arrays Must be of the news size "; System.arrayCopy (R1, 0, R3, 0, R1.length); System.arraycopy (R2, 0, R4, R4, R1.length); One class should define the Tostring method:
public string tostring () {string retval = "Countrset:"; for (int i = 0; I <data.Length (); i ++) {retval += data.bytes.tostring (); a.packets. tostring ();} Return Retval;}
If the main method is defined, it should be written at the bottom of the class.