Java language Programming Format Advanced Specification _java

Source: Internet
Author: User
Tags abstract language class definition function prototype int size lowercase naming convention one more line


As a developer, you have to have a strict code specification. For this I have summed up some code specification cases.



Directory



1. Foreword



2. Trial scope



3. Java Naming conventions--



3.1 Public engagements



3.2 Java files, packages



3.3 Class, Interface naming specification



3.4 Method Naming specification



3.5 Constants



3.6 Variables and parameters



3.7 Components/Parts



3.8 Collection



3.9 Mysterious Numbers



3.10 Other



3.11 Java exception



3.12 Array naming



3.13 database table naming rules



3.14 database field naming rules



3.15 jsp file naming



3.16 Servlet class naming



4. Writing format specification



4.1 Indent



4.2 Spaces



4.3 alignment



4.4 Blank Lines



4.5 Notes



5. code file Style






1. In order to ensure the readability of the system source program and enhance the maintainability of the system, the following programming specification is developed to standardize the programming of the system. Source programs in other resources that are inherited by the system should also be modified as appropriate for this specification.
2. Trial scope This document will be used as the programming format specification of Hebei post application system software (Java language). In the process of coding, testing and maintenance of the system, strict compliance is required.
3. Java Naming conventions
3.1 Public engagements naming refers to the name of the source file name, method, directory, package, database table name, database field, etc. in the system. 
The names used in the system are expressed in English abbreviations. 
The name is composed of the prefix +{named morphemes}, prefix to express the purpose of naming, such as database tables. 
The named prefix is determined by the project team. 
Names are all lowercase except for objects other than the system's public maintenance objects. One way is to use "_" as a connector, and the other is to concatenate the first letter of the morphemes directly together. In this specification, the database name, database field name takes the first approach, and the rest takes the second way. 
Use a complete English descriptor that accurately describes the variable/field/class/interface/package. For example, a name like Firstname,listallusers or Corporatecustomer is strictly prohibited to use Hanyu Pinyin and unrelated word naming, although Java supports Unicode naming, but this specification provides for packages, classes, interfaces, methods, variables, fields, such as the use of Chinese characters and so on named 
Use case-by-case blending to improve the readability of the name. You should generally use lowercase letters, but the first letter of the name of the class and interface, and the first letter of any middle word should be capitalized. Package name all lowercase. 
As little as possible with abbreviations, but if you must use, when using common abbreviations and abbreviations, such as implementation (implement) can be indented written Impl, Manager (manager) can be indented written MGR, see the table below, strictly prohibited misuse of abbreviations.  Avoid using long names (preferably no more than 25 letters).



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



3.2 Java files, package 
File names are in the same class as their classes, and all words have the first letter capitalized. 
Package name is generally named after the project or module name, less abbreviations and long names, all lowercase. 
Basic Package: Com.czpost, all packages, files are subordinate 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 a class directly under the base package, and all the classes, interfaces, and so on in the project are defined in the respective project and module packages.



3.3 Class, interface naming convention all words first letter capitalized. Use words that respond exactly to the class, interface meaning, function, and so on. Nouns are generally used. 
The interface can be prefixed with the I prefix or able, ible, er, and so on.



3.4 Method naming the canonical method should be named in a complete English descriptor, mixed 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 and sets the value class to use the set prefix to determine that the class uses the IS (has) prefix. Example: GetName () Setsarry () Islogon () method parameter suggested order: (Operator, action content, Operation flag, other ⋯) example: public void replace (string sourcestr, String oldstr, St Ring newstr) {...}



3.5 Constants with a complete English capital word, in the word and the word with the underline connection, such as: Default_value



3.6 variables and parameter variables recommended Hungarian nomenclature, morpheme rules see "Public agreement" 
Integer variables (including int,short,long): Beginning with N, for example: long ncrednum; 
Floating-point variables (including float,double): Beginning with F, for example: double Ftranamt; 
Character variables: Starting with C, for example: Char Cpageflag; 
Date-type variable: Starting with D, for example: Date dtoday;  Boolean variable: Beginning with IS, for example: Boolean isflag; 
String variable: Starting with S, for example: Char spageno[2+1]; 
Global variables: The type of data that starts with G, for example: Char gsbankno[10+1]; 
Static variables: Static variables are also global variables in the file, naming the rules with global variables. 
System-Level variables: System-level variables are also global variables, with the start of ' _ ' combined with the type of data.
For example: Char _sbankno[10+1] has no ' g '.
Temporary variables are usually named I,j,k,m and N, and they are generally used for integers; c,d,e, which are generally used for character types.



3.7 Components/parts should be in full English descriptor naming components (interface parts), following the Hungarian naming laws such as: Btnok,lblname.  button variable btn+xxxxxxx For example: Btnsave, btn Exit, Btnprint, etc.  title variable lbl+xxxxxxxx For example: Lblname, Lblsex and other object type prefixes Canvas CVS CheckBox chk Image img LST Choice chc Dialog dlg Event evt Frame frm Menu menu Panel pnl TextArea txa TextField TXF



3.8 Set together a set, such as arrays and vectors, should be named in the plural to represent the types of objects that are stored in the queue. Name should be a complete English descriptor, the first letter of all non-beginning words should be capitalized, the appropriate use of the set abbreviation prefix. For example: vector vproducts = new vector (); Product vector Array aryusers = new Array (); User List



3.9 The mysterious number of programs often use a number of, it has a specific meaning, the mystery of the number defined as a constant. Note that the name of the constant should be able to express the meaning of the number, and should all be capitalized to distinguish it from the identifier corresponding to the variable. For example, the number 50 can be defined as a constant that is 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



A 3.12 array named array 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 noun by an underscore.  name is made up of lowercase letters. 
If the noun element is composed of a single word, it can be fully spelled, and if it is composed of many words, it is abbreviated. Use an underscore between abbreviations. For example:  Library table: With ' t_ ' opening view: With ' v_ ' opening of the Library table index: With ' T_ ' at the end of ' _idx ' the unique index of the library table: with ' T_ ', ending with ' _uidx '



3.14 database field naming rules 
If the noun element is composed of a single word, it can be fully spelled, and if it is composed of many words, it is abbreviated. Use an underscore between abbreviations.  name is made up of lowercase letters.  no prefix.



3.15 JSP file name using the complete English description of the functions of JSP completed, as far as possible, including a vivid verb, the first letter lowercase, such as: viewmessage.jsp, edituser.jsp or forumchooser.jsp.



3.16 servlet class naming generally corresponds to the service of the object plus suffix services to name, such as: Userservice,tradeservice.



4. Writing format specifications Strict requirements of the writing format is to make the program neat and beautiful, easy to read, style unity, programmers on the need for normative writing must have a clear understanding. It is recommended that the source program be developed using the Eclipse tool, which is set in advance in the tool.



4.1 Indentation is recommended in 4 spaces. Preprocessing statements, global data, headings, additional instructions, function descriptions, labels, etc. are below written. Statement blocks of "{", "}" paired aligned, and aligned with the previous line, statement block class statement indentation recommended that each "{", "}" a separate line for the pair. The default method in Sclipse is that the start "{" is not a single line and is recommended to be changed to this format.



4.2 Spaces in principle variables, classes, constant data, and functions are appropriately delimited between their type, decorated names, and are aligned according to the situation. A grid of keyword principles, such as: if (...). The space of the operator is specified as follows: ":", "->", "[", "]", "+ +", "--", "~", "!", "+", "-" (sign), "&" (reference), and so on, with no spaces on either side of the operator (where the single eye operator is the one side of the operand), Other operators, including most two-mesh operators and the three-mesh operator "?:" Both sides of a space, in the definition of the function can also be a case of multiple empty or not empty cells alignment, but in the function implementation can not. The "," operator is only empty after that, and can be aligned without blank or multiple spaces. Whether or not you have parentheses, apply the appropriate spaces to the comments that are added to the statement line and separate the statements and align as much as possible. The individual believes that this can be determined according to personal habits or not.



4.3 Alignment in principle, the lines that are closely related to each other are aligned, including types, adornments, names, parameters, and so on. The length of the other row should not exceed the screen too much, if necessary, the appropriate line, when changing lines as possible in the "," or operator, it is best to start with an operator after a newline, and the following lines are indented with the first line of the statement, but the statement is still indented with the first line, as if the next behavior "{" should be aligned with the first line. A variable definition is best done by adding a space or a TAB key, and the same type of variable is best placed together. As shown in the following example: int nvalue; int nresult; int nlength;



4.4 Empty lines must not have a blank line without rules, such as 10 consecutive empty lines. There is a blank line between the parts of the program's file structure, and because each function also has a function description annotation, it is usually just a blank line or not, but there should be at least one more line for situations where there is no function description. For the functions you write, it is recommended that you also add "//------" to separate them. There should be at least one line between the internal data of the function and the code, and the appropriate place in the code should be empty, and it is recommended that you have a blank line before the variable declaration appears in your code. There is at least one row between the four "P" in the class, where the data and function should also be blank.



4.5 annotation annotation is the concrete embodiment of software readability. The program annotation volume generally occupies 20% of the program code amount, the software engineering requirement is not less than 20%. program annotation can not use abstract language, similar to "processing", "loop" such as computer abstract language, to accurately express the process of processing instructions. Avoid using annotations for each line of programs, and you can add a note to the front of a program with clear processing logic. Annotations are essential, but should not be excessive, and do not passively write comments for comments. Here are four of the necessary comments:  Title, additional instructions.  A description of the function, class, and so on. You should have a proper description of almost every function, usually added before the function implementation, in the absence of the function implementation part of the case is added before the function prototype, its main content is function, purpose, algorithm and other instructions, parameter description, return value description, if necessary, some such as special hardware and software requirements and other instructions. Public functions, common class declarations must be explained by the annotations of their use and design ideas, of course, choose the appropriate naming format can help you to explain things more clearly. 
There must be some explanation for the code that is not clear or not portable. 
and a small number of other annotations, such as comments for 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, avoiding the use of hand-made header files/implementation files. package/import package row to the import line, the standard package names in import are preceded by the local package name, and are sorted alphabetically. If the import row contains a different subdirectory in the same package, it should be treated with *.
Package hotlava.net.stats; Import java.io.*; Import java.util.Observable; Import hotlava.util.Application; Here java.io.* are used to replace InputStream and OutputStream. 
File header Note File header annotation is mainly to indicate some information about the file, is the general description of the program, can enhance the readability and maintainability of the program. File header comments are generally located after the Package/imports statement, before Class description. Requires at least the filename, creator, creation time, and content description to be written out. The format should be as restrictive as possible:/** *title: Determine the mouse position class * Description: Determine which job field the mouse is currently in and return the job Number * @Copyright: Copyright (c) 2002 * @Company: * @a Uthor:rivershan * @version: 1.0 */class Next is the annotation of the class, which is generally used to explain the class. /** *a class representing A set of packet and byte counters * It is observable to allow it to being watched, but only * repor TS changes when the current set is complete/Next is a class definition that contains extends and implements public class CounterSet extends in different rows Rvable implements Cloneableclass Fields








Next is the class's member variable:/** *packet Counters * * protected int[] packets; The member variable for public must generate a document (JAVADOC). member variables defined by proceted, private, and package if the name is unambiguous, you may not have a comment.  The access method is followed by a method of accessing the class variable. It is simply used to assign a value to a variable of a class, and it can be written on a single line. (personally think as far as possible branch write)



/** *get The counters * @return An array containing the statistical data. This array has been * freshly allocated and can is 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 methods do not write on one line  the constructor is followed by a constructor, which should be written in an incremental way (for example, with more than one parameter written in the back). Access type ("Public", "private", etc.) and any "static", "final" or "synchronized" should be on one line, and the methods and arguments are written in a separate line, making methods and parameters easier to read.



public counterset (int size) {



this.size = size;



}



Cloning method if this class can be cloned, 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 to begin writing the class method below:



/** *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 are of equal size//if (r1.length!= r2.length | | r1.length!= r3.length | | r1.length!=-R4.LENGT h) 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 the 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; 
 


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


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.