Java programming Specification

Source: Internet
Author: User
  • Exit ()
    In addition to being called in main, exit should not be called in other places. This does not give any code Code Opportunity to intercept and exit. A program similar to the background service should not exit because a database module decides to exit.
  • Exception
    The declared error should throw a runtimeexception or a derived exception.
    The top-level Main () function should intercept all exceptions and print (or record in the log) on the screen.
  • Garbage Collection
    Java uses mature background garbage collection technology to replace reference counting. However, this will cause a problem: You must clear the job after using the instance of the object. For example, a prel programmer may write as follows:

    ...{FileOutputStream fos = new FileOutputStream(projectFile);project.save(fos, "IDE Project File"); }...

    Unless the output stream is closed once it is out of scope, non-reference counting program languages, such as Java, cannot automatically complete variable clearance. It must be written as follows:

    FileOutputStream fos = new FileOutputStream(projectFile);project.save(fos, "IDE Project File"); fos.close();
  • Clone
    The following is a useful method:

      implements Cloneable  public    Object clone()    {      try {        ThisClass obj = (ThisClass)super.clone();        obj.field1 = (int[])field1.clone();        obj.field2 = field2;        return obj;      } catch(CloneNotSupportedException e) {        throw new InternalError("Unexpected CloneNotSUpportedException: " + e.getMessage());      }  }
  • Final class
    Never define a class as final for performance reasons (unless required by the framework of the program)
    If a class is not ready to be inherited, it is best to specify it in the class document rather than define it as final. This is because no one can guarantee that she will be inherited for any reason.
  • Member variables of the category class
    Most of the class member variables should be defined as protected to prevent the inherited class from using them.
    Note that you must use "int [] packets" instead of "int Packets []". The last one will never be used.

    public void setPackets(int[] packets) { this.packets = packets; }  CounterSet(int size){  this.size = size;}
Typographical specifications
 
 
  1. Add spaces between keywords and operators.
  2. Empty lines are added between relatively independent blocks.
  3. Long statements and expressions should be divided into multiple lines for writing.
  4. The new lines must be indented to make the layout neat and the statement readable.
  5. A long expression divides a new line at the lower-priority operator, which is placed at the beginning of the new line.
  6. If a long expression or statement exists in statements such as loop and judgment, appropriate division is required.
  7. If the parameters in a function or process are long, you need to divide them appropriately.
  8. Multiple phrase sentences cannot be written in one row, that is, only one statement can be written in one row.
  9. The code in the statements such as the start of a function or process, the definition of the structure, and the cycle and judgment must adopt the indent style.
  10. C/C ++ uses braces '{' and '}' to define a block. When compiling a block, '{' and '}' should each have an exclusive row and be in the same column, at the same time, it is left aligned with the statements that reference them. At the beginning of the function body, the definition of the class, the definition of the structure, the definition of enumeration, and the programs in the IF, for, do, while, switch, and case statements must adopt the above indent method.
Annotation Specification

The purpose of defining this specification is to make all the documents in the Project look as written by one person, increase readability, and reduce the loss caused by substitution in the project team. (These specifications are not always strictly observed, but must make the program readable ).

 

Java syntax is similar to C ++. Do you know how many Java annotations are there? Yes?

// Comment a row

/* ...... */Annotate several rows

Not completely correct. In addition to the above two types, there is also the third type. document notes:

/**... */Annotate several lines and write them into the javadoc document.

  
  
  1. Annotations must be simple and clear.

    String username = NULL; // User Name

  2. Write and annotate the code, modify the code, and modify the corresponding comments to ensure the consistency between the comments and the code.
  3. Note where necessary, the amount of comments should be moderate. The content of the comment must be clear, clear, and accurate.
    Comments. Keep comments adjacent to the code described by them, that is, the proximity principle of comments.
  4. Comments to the code should be placed in adjacent locations above it, and should not be placed below. Comment on the data structure should be placed in
    The adjacent positions above the structure cannot be placed below; comments to each field in the structure should be placed on the right of the field;
    Annotations for different domains in the same structure must be aligned.
  5. Comments of variables and constants should be placed on the adjacent positions or the right side above them.
  6. A global variable must have more detailed comments, including its functions, value ranges, and which functions or processes access it
    And precautions for access.
  7. Note the necessary information in the header of each source file, including the file name, version number, author, and generation date.
    Module function description (such as the function, main algorithm, the relationship between the internal parts, the file and other
    Main functions or process list and historical modification records of this file.

    /*** Copy right information: Neusoft IIT * Project
    : Etrain * JDK version used
    : Jdk1.3.1 * comments
    : Config path * version
    : 1.01 * modification history
    : 2003.5.1 * srdate modified by why & what is modified * 1.2003.5.2 Kevin Gao new **/

  8. Comment information necessary before each function or process, including: function or process name; Function Description
    Description; Description of input, output, and return values; Description of call and called relationships

    /*** Description: checkout withdrawal
    * @ Param hashtable cart info
    * @ Param orderbean order info
    * @ Return string */
    Public String checkout (hashtable htcart,
    Orderbean)
    Throws exception {}

  9. Javadoc annotation label syntax

    @ Author the description of the class indicates the author who developed the class module.
    @ Version the description of the class indicates the version of the class module.
    @ See references to classes, attributes, and methods, that is, related topics
    @ Param: Description of a parameter in the other method
    @ Return description of the method description of the return value of the Method
    @ Exception description of methods description of possible exceptions thrown by methods

 

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.