Java coding specification (2)
Translation: Wang Shi Yong
(Reprinted please keep the author, thank you)
There are many more versions than the previous version, and the layout is rushed. I hope I can replace the previous article.
1 Introduction
1.1. Why do you want to code?
Why is the coding specification is important? There are some reasons:
l A software 80% life cycle is the maintenance period
l It is hard to say that his entire life cycle is maintained by his original author.
l Code specification to improve software readability, so that software engineers fully understand the new code becomes very fast.
l If you want to release your original code as a product, you need to make sure that he is as clean and packaged like your other products.
In order to work according to the specification, the coding specification must be observed when each person is written. Remember, everyone!
1.1.1. Acknowledgment
This book reflects the Java Language Specification for Java language coding specifications. Here to focus on Peter King, Patrick Naughton, Mike Demoney, Jonni Kanerva, Kathy Walrath, and Scott Hommel thank you.
2. File name
This section lists most of the file names and suffixes used in this book.
2.1. File suffix
.java java source file suffix
.CLASS JAVA byte code file suffix
2.2. Common file name
The file names that are often used include the following:
Gnumakefile preferred Makefile's name, we use gnumake to build our software.
Readme, those preferred names that specifically outline the file of the specific folder content
3. Organization of the file
The individual parts of a file should be spaced apart from the space, and one optional annotation should be used to indicate each different portion.
The file is more than 2,000 lines, which is very cumbersome, should be avoided.
As for examples of the correct format of Java programming, see "Java Source File Example (Java Original File Example)" on page 18.
3.1. Java source code file
Each Java source file includes a unique public class or interface. When private classes and interface are related to this public class, you can put them in the source file of this public class. This public class or interface should be the first class or interface of this file.
The Java source file has the following order:
l The beginning of the file (see "Beginning Comments (beginning)")
l Declare the statement and load statement.
Laws with Class L and Interface (see "Class and Interface Declarations") of Page 3)
3.1.1.
All source files should begin with a C language style. This annotation should list class name, version information, date, and copyright statement:
/ *
* ClassName
*
* Version Information
*
* Date
*
* CopyRight Notice
*
* /
3.1.2. Statement package statement and import statement
The vast majority of Java source files should be the statement of a statement package. Since then, it is followed by an Import statement. E.g:
Package java.aw;
Import java.awt.peer.canvaspeer;
3.1.3. Category and Interface
The following table describes the declaration of the partial class and interface, and they should follow the order of the table. See "Java Source File Example" on page 18. Particular / Interface Declaration Note Class / Interface Document Note / ** ... * / How to do this type of annotation, please see "Documentation Comments" or interface declaration
Class / Interface Implementation (/ 9.//), if necessary, this annotation should include any content that is not suitable in the class / interface document annotation within the entire class or interface. Class (static) variable is first a PUBLIC class variable, then the protected class variable, then Friendly (Package Level, the default). Then it is a private variable. Instance variables (translation: entity variable? Will not translate, meaning that is ordinary variable.) First is public, second protected, then Package Level. Finally, the private variable. Class constructor
Method, (Translation: That is, the class "function) These methods should be standard, organized in one, rather than seeing their roles and accessibility. For example: a private class method (translation: "Private Static Method) can be placed in the middle of two public instance methods (translation: meaning, called public method). Its purpose is to increase in code readability and understandability.
4. Ind retain
The indent accurate structure should not be detailed in detail with the minimum unit of four spaces. Tabs must be accurately designated as 8 spaces (rather than 4).
4.1. Length of the code line
The length of the line should be avoided more than 80 characters, as many terminals and tools do not support more than 80 characters. (Demolition: Personally, it is now generally supported more than 80 characters, but due to more than 80 characters, generally want to scroll, so do not exceed this limit.)
Note: The longness of the example in the document should be shorter, generally no more than 70 characters.
4.2. Wrapping Lines (Broken Method)
When an expression is not suitable for a single line, it is broken in accordance with the following general principles:
l After the comma is broken
l Broken road before the operation operator
l On the higher level, breaking is better than on the low level.
l Aligns the beginning of the expression of the new line and the expression of the previous line.
l If the previous rule causes the chaotic or code of the code to be out of normal limits. Then just replace it with 8 spaces.
There are some discourse examples of calling methods:
SomeMethod (LONGEXPRESSION1, LONGEXPIESSION2, LONGEXPRESSION3,
LONGEXPIPRESSION4, LONGEXPRESSION5);
Var = SomeMethod1 (LONGEXPRESSION1,
SomeMethod2 (LONGEXPRESSION2,
LONGEXPIESSION3)))
Below is an example of a broken line of two arithmetic expressions, the first one is better than the second, because its breakstream occurs outside of the parentheses, which is over the high level.
Longname1 = longname2 * (longname3 longname4 -longname5)
4 * longname6;
Longname1 = longname2 * (longname3 longname4
- longname5) 4 * longname6; // avoid (should avoid this)
Below is an example of a broken line declared by two ways, the first is an example of a specification. Second, if you want to use the indentation of the specification, it will cause the second and third rows to indent the right right. So just use 8 spaces to replace. // Normative indentation
SomeMethod (int Anarg, Object Anothererarg, String Yetanotherarg,
Object andstillanother {
...
}
// indent in 8 spaces to avoid distraction
Private static synchronized horkinglongmethodname (int Anarg,
Object Anotherarg, String Yetanothererarg,
Object andstillanother {
...
}
LINE WRAPING FORE should generally use 8 space rules for the IF statement because the standard (4 spaces) indented makes the body part of the IF understand. E.g:
/ / Do not use this indentation
IF (Condition1 && Condition2)
|| (Condition3 && Condition4)
|| (Condition5 && Condition6)) {/ poor packaging
DOSMETHINGABOUTIT ();
}
// instead of this indentation mode
IF (Condition1 && Condition2)
|| (Condition3 && Conditin4)
||! (Condition5 && condition6)) {
DOSMETHINGABOUTIT ();
}
/ / Or use
IF (Condition1 && Condition2) || (Conditin3 && Condition4)
||! (Condition5 && condition6)) {
DOSMETHINGABOUTIT ();
}
There are three acceptable three-dimensional operators in the indentation format:
Alpha = (AlongBooleaneXpression)? Beta: gamma;
Alpha = (AlongBooleaneXpression)? Beta
: gamma;
Alpha = (AlongBooleaneXpression)
? beta
: gamma;
5. Note (Note)
To be translated.