File organization
1. source files with more than 2000 lines of code will be hard to read and should be avoided.
2. Each Java source file contains a single public class or interface. If private classes and interfaces are related to a public class, you can put them in the same source file. The public class must be the first class or interface in the file.
3. The content of the JAVA source file should follow the following sequence:
Annotation Package Description and Import Statement at the beginning, such:
import java.applet.Applet;import java.awt.*;import java.net.*;
Class and interface declaration 4. Start annotation all source files should have a c-language style annotation at the beginning, listing the author, date and copyright statement, the purpose of the program is briefly described. For example:
/* *Classname * *Version info * *Copyright notice */
5. Package declaration and import statement the first non-annotation Statement of most Java source files is a package declaration statement, followed by the Import Statement. As follows:
package java.awt;import java.awt.peer.CanvasPeer;
6. Class and interface declaration The following table describes each part of the class and interface declaration and the order in which they appear.
| |
Class/interface declaration |
Remarks |
| 1 |
Class/interface document comment |
For more information about the annotations, see the following document annotations. |
| 2 |
Class/interface declaration |
|
| 3 |
Class/interface implementation annotation (/*... */), if necessary |
This comment should contain all information related to the class or interface, which is not suitable for comments in the cooperation document. |
| 4 |
Class (static) Variable |
First, it is the public variable of the class, then the protected variable, and then the private variable. |
| 5 |
Instance variables |
The first is public, followed by protected, followed by private ). |
| 6 |
Constructor |
|
| 7 |
Method |
These methods should be grouped by functions rather than scope or access permissions. For example, a private class method can be located between two public instance methods to facilitate code reading and understanding. |
Indent
7. Generally, four spaces are used as a unit of indentation. The specific structure (whether it is a space or a tab) used for indentation is not specified. A tab is equivalent to eight spaces (not four ). 8. line length. Avoid a line of code exceeding 80 characters. Because they cannot be processed well in many terminals or tools. Note: The Example line length in the document should be shorter, generally up to 70 characters. 9. line feed when a statement cannot be written in one line, you can break the sentence according to the following general principles: after a comma, disconnect before an operator. Higher-Level disconnections take precedence over lower-level disconnections. A new line should be aligned with the beginning of the previous expression. If the above rules cause code confusion, or the code is stacked on the right side, which can be indented with 8 spaces. The following are some examples of method call disconnection:
function(longExpression1, longExpression2, longExpression3, longExpression4, longExpression5);var = function1(longExpression1, function2(longExpression2, longExpression3));
The following two examples illustrate how to disconnect an arithmetic expression. The first one is better, because the disconnection is outside the bracket expression, and it is a higher level of disconnection.
longName1 = longName2 * (longName3 + longName4 - longName5) + 4 * longname6; // PREFERlongName1 = longName2 * (longName3 + longName4 - longName5) + 4 * longname6; // AVOID
The following is an example of indentation of two method declarations. The first example is the general situation. In the second example, if the regular indentation method is used, it will make the code in the second and third lines too far to the right, so it is replaced by an indentation of 8 spaces.
// Regular indent someMethod (int anArg, Object anotherArg, String yetAnotherArg, Object andStillAnother ){...} // Replace with 8 spaces to avoid too much indentation. private static synchronized horkingLongMethodName (int anArg, Object anotherArg, String yetAnotherArg, Object andStillAnother ){...}If statement line breaks should usually use eight spaces, because regular indentation (with four spaces) will make the statement body more difficult to read. For example:
// Do not use this indent if (condition1 & condition2) | (condition3 & condition4) |! (Condition5 & condition6) {// bad wraps doSomethingAboutIt (); // make this line easy to miss} // use THIS indent TO replace if (condition1 & condition2) | (condition3 & condition4) |! (Condition5 & condition6) {doSomethingAboutIt ();} // or this indent if (condition1 & condition2) | (condition3 & condition4) |! (Condition5 & condition6) {doSomethingAboutIt ();}
The following are three feasible methods for formatting a three-object expression.
alpha = (aLongBooleanExpression) ? beta : gamma;alpha = (aLongBooleanExpression) ? beta : gamma;alpha = (aLongBooleanExpression) ? beta : gamma;
Note10. Java programs have two types of Annotations: Implementation annotation and document annotation. Implementation comments are comments defined by/*... */and/in the C ++ code. Documentation annotations are unique to Java and are defined using. The javadoc tool can be used to extract document comments into HTML files. Implementation annotations are used to explain the code or to describe the implementation in detail. The document comments describe how to use the code from the perspective of freedom. It is read by programmers who do not need source code.
Annotations are usually used to give an overview of the Code and provide additional information that is not easily obtained from the Code itself. The comment should only contain information related to the reading comprehension program. For example, how the corresponding package is created or where it is located, such information should not be included in the comment. Note: too frequent comments sometimes reflect poor code quality. When you feel you have to add a comment, consider rewriting the code and make it clearer. Annotations should not be enclosed in a large box drawn with asterisks or other characters. Comments should never contain special characters such as tabs or escape characters.
11. The implementation annotation program can have four types of implementation Annotations: block annotation, single line annotation, tail annotation, and end annotation.
12. Block comment block comments are generally used to provide descriptions of files, methods, data structures, and algorithms. Block annotations should be used at the beginning of each file and before each method. They can also be used in other places, such as internal methods. Comments within a function or method should have the same level of indentation as the code they describe. There should be a blank line before the block comment to separate it from other code. Except for the first line, each line starts with an asterisk.
/* * Here is a block comment. */
A block annotation can start with/*-, so that indent (1) can recognize it as the beginning of a block annotation without reformatting it.
/* * Here is a block comment with some very special * formatting that I want indent(1) to ignore. * * one * two * three */
Note: if you do not use indent (1), you do not need to use/*-in your code, or run indent (1) on your code for others) make other concessions.
13. A single-line short comment can appear in a row, and has the same indentation level as the code that follows. If a comment cannot be written in one line, the block comment format should be used. There should be an empty line before a single line comment. The following is an example of a single line comment in Java code:
if (condition) { /* Handle the condition. */ ...}
14. Very short comments in the end can appear in the same line of the Code described by the end, but there should be enough blank space to separate the code and comments. If more than one short comment appears in a large piece of code, it should be indented to the same tab setting (not translated, the original Article is they shocould all be indented to the same tab setting, which probably means indent to these comments to align ). Avoid adding the Assembly Language-style comments with a tail annotation to each line of executable code. The following is an example of a comment on the end of the Java code:
if (a == 2) { return TRUE; /* special case */} else { return isprime(a); /* works only for odd a */}
15. comments at the end of a line start with the separator. It can be used to comment out a part of an entire line or a row. It is generally not used for text comments with multiple consecutive lines, but can be used to comment out code segments with multiple consecutive lines. The following are examples of all three styles:
if (foo > 1) { // Do a double-flip. ...}else return false; // Explain why here.//if (bar > 1) {//// // Do a triple-flip.// ...//}//else// return false;
16. for more information about document Comments, see "How to Write Doc Comments for Javadoc", which contains the information marked by the document Comments (@ return, @ param, @ see): http://java.sun.com/products/jdk/javadoc/writingdoccomments.html for more information about document comments and javadoc, see javadoc home page: http://java.sun.com/products/jdk/javadoc/
This document describes Java classes, interfaces, constructors, methods, and fields. Each document comment is placed in the comment separator/**... */, and each API corresponds to a comment. This comment should only appear before declaration:
/** * The Example class provides ... */class Example { ...
Note that the class and interface are not indented, and its members are indented. The first line (/**) of the document comment of the class and interface is not indented, each of the subsequent document comment lines has a space indent (vertical alignment of asterisks ). The member, including the constructor. the first line of the comments in the document is the indentation of four spaces, followed by five spaces. If you need to provide information about classes, interfaces, variables, or methods that are not suitable for appearing in the document, you can use block or single line annotations immediately after the declaration. For example, the details of a class implementation should be in the block comment after the class declaration, rather than in the document comment of this class. Document comments should not be placed in the definition blocks of methods or constructors, Because Java associates the document comments with the first declaration.