1. Java naming agreement
In addition to the following special examples, naming should always adopt a complete English descriptor. In addition, lowercase letters should generally be used, but the class name, interface name, and the first letter of any non-initial word should be capitalized. 1.1 General Concepts Try to use the full English descriptor 1. Use the term applicable to the relevant field 2. Adopt case-write mixing allows the name to read 3. Try to use less abbreviation, but if used, you should use it, and use throughout the project Unified 4. Avoid using long name (less than 15 letters is a good idea) 5. Avoid using similar names, or just case different names 6. Avoid using underscore (except static constants, etc.) 1.2 demonstration 1. Package (Package) adopts a complete English descriptor, which should be composed of lowercase letters. For global packages, turn your Internet domain and connect your name, for example: java.awt.com.ambysoft.www.Persistence. 2. Class (Class) adopts a complete English descriptor, all the first letters of all words. Customer, Savingsaccount. 3. Interface (Interface) adopts a complete English descriptor to explain the interface package, all the first letters of all words. It is used to add suffix able, ible or ER back, but this is not required, Contactable, Prompter. 4. Component / Components Use a complete English description to illustrate the use of components, and the end should be connected to the component type. Okbutton, Customerlist, FileMenu. 5. Exception is usually an abnormality using the letter E. The E class variable field uses a complete English description, the first letter lowercase, the first letter of any intermediate word. 6. Naming Rules of the Command / Parameters with the Field / Properties. Public void setfirstname (String firstname) {this.firstname = firstname;} 7. The naming rules of the local variable with the field / attribute are as follows: Get the member function to be accessed the front plus prefix Get. GetFirstName (), getlastname () Boolean acquisition member function All Boolean acquisition functions must be prefixed with word IS. ISPERSISTENT (), Isstring () Sets the member function to the front plus prefix SET in front of the field name. SetFirstName (), setlastname (), setWarpspeed (). 8. Ordinary member functions use a complete English description instructions for member function, the first word uses a vivid verbs as much as possible, the first letter lowercase. OpenFile (), addaccount (). 9. Static Final All use uppercase letters, and the words are separated by underscore. MIN_BALANCE, DEFAULT_DATE. 10. Circular counter usually accepts letters I, J, K or Counter can be accepted. I, J, K, Counter. 11. A array array should always be named: ObjectType []. Byte [] buffer.
2. Java annotation agreement
A well-compliant experience is: ask yourself, if you have never seen this code, you need to effectively understand this code in a reasonable time, what information do you need.
2.1.
One
Concept 1. Note Should increase the clarity of the code. 2. Keep the comment is concise. 3. Write a comment before writing the code. 4. Note Why did you do something, not just what is done. 2.2. Demonstration 1. Document comments commented in front of the interface, classes, members functions, and field declarations. / ** Customer: Customers are people or institutions sold by our services and products. * / C language style uses C language style notes to remove the code that is no longer used, but you still want to keep. Still want to keep it because users will change their ideas, or they want it to temporarily invalid during debugging. / * This part of this code is replaced by its previous code, from B.Gustafsson, 1999
Year 6
The 4th is released. If it is not used for two years, it is deleted. (Source code) * / 2. The single line of comments are used in the member function, and the statement of business logic, code segment, and temporary variables will be clear. The comment "//" must be followed by a space, then the comment information.
/ / Follow Sarek's regulations, give all
// More than $ 1000 shipping
// Hit 5% discount. Let go
// Dimensioned in February 1995. 2.3. Notes which part of this 1. The purpose of the class class, that is, the function completed by the class, commenting the invariant amount. 2. The purpose of the interface setting interface should be used and how it is not used. 3. Member Function Note For settings and acquisition members, if the member variable has been explained, it can not be added; 4. Ordinary member function requires what functionality is done, what is the meaning of the parameter meaning; 5. Ordinary member function The internal annotation control structure, what is done and why do this, the order of handling, etc. 6. Real parameter / parameter parameter meanings, and other constraints or prerequisites 7. Field / Property field description 8. None of local variables without special meaning
3. Java file style agreement
All Java (*. Java) files must comply with the following style rules: 1. Copyright information must be on the beginning of the Java file, such as: / ** * Copyright? 2000 Shanghai XXX Co. Ltd. * All Right Reserved. * / Other information that does not need to appear in Javadoc can also be included here. 2. Package / Imports Package Dow To the IMPORT row, the standard package name in the Import is before the local package name and in the alphabetical order. If the IMPORT line contains different subdirectory in the same package, you should use * to process. 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. Class Next is the annotation of the class, usually used to interpret the class. / ** * a class representing a set of packet and byte counters * it is observable to allow it to be watch the extends and implements public class CounterSet extends Observable implements Cloneable {......} 4 class Fields the next class member variables: / ** * Packet counters * / protected int [] packets; public member variables must generate a document (JavaDoc. ). Member variables defined by Proceted, Private and Package If the name meaning is clear, you can not comment. 5. Access method (class settings and acquisition member functions) Next is the method of accessing the class variable. It is just simple to get values to assign values of the variables of the class. You can simply write on a row, such as the class member variable already comment, and the access method of class variables can be not commented. Public int [] getpackets () {return this.packets;} public void setpackets (int [] packets) {this.packets = packets;} ... require that for collection, join member functions to insert and delete items; Another other way should not be written on one line. 6. The constructor is next to construct a function, which should be written in an incremental manner (such as: written behind). Public counterset (int size) {this.size = size;}. 7. Cloning method If this class can be cloned, then the next step is Clone method: public object clone () {Try {...} catch (clonenotsupportedException E) {...}}. 8. Class Method (Ordinary Member Function of Class) Next: / ** * SET The Packet Counters * Param R1 - ... * Param R2 - ... * ... * / protected final void setAtray (int ] R1, INT [] R2, INT [] R3, INT [] R4) throws illegalgumentException {// EnSure the arrays area of equal size ......}. 9. TOSTRING method Under normal circumstances, each class should define the TOSTRING method: public string toString () {...}. 10. MAIN method The normal class, considering the placement of a Main () method, which contains code for testing the class, if included, should be written at the bottom of the class. 4. Java encoding other conventions
1. Documentation must use javadoc to generate a document for class. Not only because it is standard, this is also a method recognized by various Java compilers. Using the @Author tag is not recommended because the code should not be owned by the individual. 2. The indental indentation should be 2 spaces or 4 spaces per line. Do not save Tab characters in the source file, the Tab character will extend to different widths due to the different source code management tools. If you use Ultredit as your Java source editor, you can prohibit saving Tab characters by using Ultredit, using the length of the length of the tab used in the ULTREDIT, then use the Format | Tabs to Spaces menu Convert TAB to spaces. 2. Page wide page width should be set to 80 characters. Sourcecodes generally do not exceed this width and causing unable to display, but this setting can also be flexible. In any case, the long statement should be folded after a comma or an operator. After a statement is folded, 2 characters should be retracted more than the original statement. 4. {} The statement in {} should be separate as a row. For example, the first line below is wrong, the second line is correct: if (i> 0) {i }; // error, {and} in the same row IF (i> 0) {i }; // correct, separately, a row, no spaces between parentheses and the latter characters; the same, there should be no space between the right brackets and the previous characters. The following example shows the errors of parentheses and spaces: CallProc (APARETER); // Error CallProc (APARETER); // Do not use meaningless parentheses in the statement, parentheses should only be present in a certain purpose Source code. 6. JSP file naming adopts a full English description description of the functions completed by JSP, including a vivid verb, first letter lowercase, such as: viewMessage.jsp, edituser.jsp or forumchooser.jsp, etc. 7. servlet class named general corresponds to the service to add suffix service to name, such as UserService, TradeService, etc. 5. Suggestions to write Java programs
1. Use the StringBuffer object to use the StringBuffer class when processing String, and the StringBuffer class is the basis for constituting the String class. The String class encapsulates the StringBuffer class, (at the expense of more time) provides a secure interface to developers. When we are constructing a string, we should use StringBuffer to implement most of the work, and convert the StringBuffer object to the required String object when the work is completed. For example, if there is a string that must be constantly adding a lot of characters to complete the construct, then we should use the StringBuffer object and its append () method. If we use the String object instead of the StringBuffer object, you will spend a lot of unnecessary CPU time to create and release the object. 2. Avoid too much use of synchronized keywords to avoid unnecessary use of keywords synchronized, should be used when necessary, this is a good way to avoid deadlocks. When you must use, you should also control the range, preferably on block level control. 3. Avoid using Java.util.Vector classes because "Unlike the New Collection Implementations, Vector IS Synchronized.", So using Java.util.Vector classes will be reduced. 4. Try to use the interface instead of a specific class, give a SQL statement, return a list of objects, implemented in the implementation of java.util.ArrayList, so the definition method is: public java.util.ArrayList getObjectItems String SQL) There is a problem with the method, and the external class must be changed accordingly when GetObjectItems is implemented in GetObjectItems. A better way is to define the return value for java.util.abstractList: Public Java.util.AbstractList getObjectItems (String SQL) This eliminates the corresponding changes even if the implementation is changed, the external class does not have to make the corresponding changes. 5. Avoid using an index to call the result set of database intermediate layer components, such as: for (int i = 1; i <= dt.getrowcount (); i ) {string field1 = dt.getfield (i, 0) .tostring ); ...} and apply field name to access result set: for (int i = 1; i <= dt.getrowcount (); i ) {string field1 = Dt.Getfield (i, "field1"). Tostring ); ... This does not affect the execution of the program when the SQL statement of the database design changes or the SQL statement is changed. 6. Use the container collection in JKD1.4, expand the original collection concept, if you write a Java language using JDK1.4 or higher, it is recommended to use list to replace the original Vector, replace the original HashTable with HashMap.