Java Coding Specification

Source: Internet
Author: User

Refer to Sun's Java coding specification, semi-translation semi-comprehension.

  1. Use blank lines to split blocks of code and add comments to code blocks;
  2. A file a class (or interface) that places a private class behind a common class that references that class;
  3. Source file composition: The declaration of a file comment, Package/import, class, or interface;
    1. /*
      * Class Name
      * Version Info
      * Copyright Notice
      * Program Description
      */

  4. Use 4 spaces as indentation, avoid using the TAB key (Tab key does not have a uniform standard, there are 4 spaces, some 8 spaces);
  5. Control the length of each line of code with a maximum of 80 characters;
  6. When the code breaks, the following rules apply:
    1. Break a break after a comma;
    2. Disconnect before the operator;
    3. Use a higher level of abstraction to break a break;
    4. When the line is broken, the logical processing level of each row is at the same level.
    5. If the above principle causes the code to be confusing, a new line will start directly from the 8th space at the beginning of the previous line;
  7. two major categories of annotations, implementation comments (/*...*/and//) and Documentation Comments (/**...*/); four small types of annotations, block annotations (for file descriptions, methods, data structures, and algorithms), Single line comment, line end comment, and end line comment;
      1. Annotations are used to describe abstractly the code outline and information that cannot be obtained directly from the code (such as business logic). Comments should be used to give overviews of code and provide addtional information that's not readily available in the C Ode itself.
      2. Comments should contain only information that helps to read and understand the program. Comments should contain only information that's relevant to reading and understanding the program.
      3. Comments should be specific to the implementation of the Code, and will not be obsolete because of code optimization. Avoid comments that is likely to get out of date as the code evlves.
      4. Adding comments is not a good way to improve the readability of your code, first of all to make the logic of the code itself clearer. If you feel compelled to add a comment, consider rewriting the code to make it clearer.
  8. A row of a variable or method declaration, variable type and variable name interval between a space;
  9. Place the statement as far ahead as possible;
  10. initialization of variable declarations;
  11. Class and interface declarations are subject to the following rules:
    1. There is no space between the opening parenthesis of the method name and the parameter name;
    2. Method declaration The opening curly brace is the same line as the method name;
    3. Method declaration ends with a single row of closing curly braces;
    4. A blank line between the method declarations;
  12. One sentence at a line;
  13. Use "{...}" Constrained function blocks, especially if-else
  14. The return statement avoids the use of the method unless the returned result of the method is very clear;
  15. if-else Logical block notation:
    1. if (condition) { statements; }
      if (condition) { statements; } else { statements; }
      if (condition) { statements; } else if (condition) { statements; } else if (condition) { statements; }

  16. for logical block notation:
    1. /tr>
      for ( initialization ;  condition ;  update     statements }

  17. while logical block notation:
    1. /tr>
      while ( condition " Span style= "font-size:9pt") { < Span style= "font-size:9pt" >     statements ; }

  18. Do-while Logical block notation:
    1. Do { statements; } while (condition);

  19. Switch Logical block notation:
    1. switch (condition) { Case ABC: statements; / * Falls through * /Case DEF: statements; Break ;Case XYZ: statements; Break ;Default: statements; Break ;}

  20. try-catch Logical block notation:
      1. /tr>
        try {     statements ; } catch ( Exceptionclass e) {     statements ; }

  21. Use of Blank lines:
    1. Use two empty lines where: different source code blocks; Different classes and interfaces are defined
    2. Where a blank line is used: A different method, a variable declaration and use in the same method, a block or a single line of comments before the start of a comment, and a different logical block of the same method
  22. Use of spaces:
    1. Between the keywords and the parentheses;
    2. After the comma in the argument list;
    3. Except for "." All two-dollar operators except;
    4. for (EXPR1; expr2; expr3);
    5. After forcing type conversion
  23. naming rules:
    1. interface Name: class name;
    2. method name: verb or verb group First letter lowercase;
    3. variable: single word, all lowercase, multiple word combinations, The first letter is lowercase; the temporary variable generally uses a single letter;
    4. constant: Use underscores to connect multiple words ; all uppercase letters
  24. Other:
    1. Use fixme and xxx;
    2. Use parentheses to separate simple logical operations, such as "(x >= 0)? ×:-X "and" if ((a = = b) && (c = = d)) "
    3. Avoid combining assignments to make logic clear and simple! such as "a = B = C" and "if (c + + = d++) {...}"
    4. Use constants or variables instead of using numbers directly;

Java Coding Specification

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.