JAVA writing specification and java writing
Java program writing Specification
Naming rules
1. General Concepts
1. Try to use the complete English Descriptor
2. Use terminology applicable to relevant fields
3. Use a mix of upper and lower cases to make the name readable
4. Use the abbreviations as few as possible. However, if the abbreviations are used, they must comply with the unified definition of the entire project.
5. Avoid using long names (if the number is less than 15 letters, it is normal)
6. Avoid using similar names, or simply name different cases.
7. Avoid using underscores (except static constants)
2. Description of the identifier type
1. Package name
The Package name should use a complete English descriptor, which is composed of lowercase words. And the prefix of the package name is always a top-level domain name,
Usually com, edu, gov, mil, net, org, etc;
Example: com. yjhmily. test
2. Class naming
The class name should be a noun, Which is case-insensitive and the first letter of each word is capitalized. Make sure that the class name is concise and descriptive.
Use full words to avoid acronyms (unless there are uniform abbreviations in the project or the acronyms are more widely used, such as URLs and HTML)
For example, FileDescription
3. Interface naming
Similar to the Class naming rules. On the basis of meeting the Classd naming rules, ensure that the first letter at the beginning is "I ",
It is easy to distinguish it from a common Class. The second letter of the implementation Class Name Interface Name to the end, and meets the class name naming rules;
For example, IMenuEngine
4. Name of enumeration (Enum)
Similar to the Class naming rules. Ensure that the first letter at the beginning is "E ",
It is easy to distinguish it from a common Class.
For example, EUserRole
5. Exception naming
Exception usually uses the letter e to indicate an Exception. For a custom Exception class, its suffix must be Exception.
For example, BusinessException
6. Method naming
The method name is a verb. It is a combination of upper and lower cases. The first letter of the first word is lowercase, And the last letter of the last word is capitalized.
The method name describes the action of the method as much as possible. The return method of a Boolean value generally starts with "is" or "has ".
For example: getCurrentUser (), addUser (), hasAuthority ()
7. Name of the parameter (Param)
The first letter is lowercase, And the last letter is uppercase. The parameter name cannot start with an underscore or a dollar sign,
Although this is allowed in syntax. The parameter name must be brief and descriptive.
For example, public UserContext getLoginUser (String loginName );
8. Constant field (Constants) Name
All static constant fields (static final) are uppercase letters, and words are separated by underscores;
For example, public static final Long FEEDBACK;
Public static Long USER_STATUS;
1. The style must be Consistent (Consistent)
A compatriot asked me why the indentation format of our Java code had to be like this, not him. He liked his own style, therefore, the Code he writes is always in the style he is used. The results were killed in the Code Review and ordered to be modified. Therefore, he was greatly dissatisfied. It is a matter of style. In fact, there is no problem with his style, but in the project, it is different from the style of other programmers, and there is a problem. For example, this indentation, such as the variable naming method, different classes, and different Methods, is difficult to understand. So once you choose a style, you must always implement it. If a style is defined in a project, even if it is not in line with your own habits, you must always implement it and never be unconventional.
2. indent)
Since it starts with indentation, we should first talk about the indent style. Generally, C languages such as Java adopt the indent style. There are four commonly used
A.K & R style
This is the earliest indent style of the C program, which was first used by C inventor Ritchie and his collaborators Kernighan:
If (<cond> ){
<Body>
}
It is characteristic that braces and if are judged on the same line. Generally, it is indented into eight spaces or a tab key, but in C ++ and Java, it is also often indented into four spaces. Some people like to use two spaces. They think it is not good or obvious.
B. BSD style
Allman Style, also known as Allman Style, originated from Eric Allman, a Unix BSD programmer. He has written many programs for BSD:
If (<cond>)
{
<Body>
}
Features: braces and condition judgments are divided into two rows.
C. Whitesmith Style
This style originates from Whitesmith C:
If (<cond>)
{
<Body>
}
D. GNU Style
This style is only available in the gnu emacs source program:
If (<cond>)
{
<Body>
}
So what is better to use in Java? We recommend that you only use A or B. SUN has A Java Code Name Convention. We recommend.
3. Tab or Space (Tabs vs Space)
Or indent. How long should one indent be? Is it eight space bars or a Tab key?
Java has a feature that is cross-platform. However, cross-platform means that its Class can run on virtual machines on different platforms. Java source programs are sometimes not cross-platform. What? Can the source program not be cross-platform? Yes. Once a programmer sent some Code and opened it in my environment, the program was ugly and messy. There was a place to indent it, without indentation, and some were indented, just like a strange stone, zookeeper is involved. This code quality is not good! Ask the programmer, and say that he is very beautiful. Does he still spend time beautifying them?
In the past, he did not pay attention to the style of indentation. In some cases, he used the Tab key and spaces in some places. On some different platforms, the width of the Tab key is different.
Speaking of this, I believe you are clear. When indent, try not to use Tab, but use spaces. Fortunately, many editors can define the Tab key on your keyboard as several spaces. Define it now!
How many spaces are used each time? Four. 2 is too small and not obvious, and 8 is too large to occupy space.
4. row width
8 space occupied by space keys, so I have 320 columns in a row. What is 8 spaces? Stop! Please try not to exceed 80 columns for one row. Many terminals can only display 80 columns on one screen. If your statement is too long, find a way to break it and write it in several columns.
5. variable naming
SUN Java Code Convention defines naming rules for package, class, method, and common variables in Java, so I don't need to waste any more bytes. Here are some precautions and common methods that SUN does not mention.
A. Name variables with meaningful names
First, name your variables with the complete English words or conventions. For example:
FirstName
ZipCode
If the English language is not good enough, at least use the pinyin name that others can understand, such
ZhuZhi (address)
No one can understand the meaning of the variable if it is abbreviated or named randomly:
Fn
Zc
Zz
B. constants should be named in uppercase and in offline format.
The constant in Java is static final:
Static final SMTH_BBS = "bbs.tsinghua.edu.cn ";
C. Name collection variables in the plural.
Collection includes arrays and vectors. Use the plural number for naming:
MERs
ClassMates
You can also use some modifiers to name them:
SomeStudents
AllDepartments
D. Cyclic Variables
Generally, I, j, and k are used as cyclic variables.
E. Stream variable
Generally, in and out are used as Stream variables, corresponding to inputStream and outputStream.
Class. If you are reading and writing ioStream, you can use inOut.
F. Naming Conventions of Variables
People who are used to MFC prefer Hungarian Notation ). If you are used to this, use it, but be sure to be consistent with the people in the same Project. In addition, some people are used to the lower-line method in C ++, which can also be used.
Hungarian Notation:
SFirstName
Under Score Style:
_ FirstName
Here we provide a prefix naming convention for Hungarian Notation:
Int I
Byte B
Char c
Double d
Float f
Long l
Offset off
Length len
Object o
String s (or str)
Arbitray value v
6. Java File Format
There are many ways to define your file statement format. The following is an example:
A. File Header description (optional)
B. Package Definition
C. Empty rows
D. Import Statement
E. Empty rows
F. Class Definition
For example:
Package com. midi;
// Java Classes
Import java. awt .*;
Import java. io .*;
Import javax. swing. event .*;
// WebLogic Classes
Import weblogic. internal .*;
/**
* Blah blah
* @ Author midi
* @ Version 22.2
*/
Public class MyFirst extends JFrame {
...
}
/**
* Foo...
* @ Author midi
* @ Version 38.2
*/
Class Foo {
...
}
7. import Order
The disorganized order looks unpleasant. You should classify the classes you want to import and list them in order:
A. Java standard class (java .*)
B. Java extension class (javax .*)
C. Third-party class
D. Your application class
Note that they are annotated in a third-party class to describe their source:
Import java .*;
Import java. util. Date;
Import java. util. Enumeration;
Import javax. SQL .*;
// Apache Xerces
Import org. apache. xml .*;
Import org. apache. xerces. dom .*;
// Application classes
Import com. midi. util .*;
8. Order of Classes
A. Javadoc comments or other file header comments
B. Class Declaration
C. Fields statement
D. Empty rows
E. Constructor
F. Empty rows
G. Method (excluding main)
H. Empty rows
I. Inner class
J. Empty rows
K. main ()
Example:
/**
* This is a simple DOM tree XML parser...
*...
* @ Author: midi
* @ Version: 0.0.1
**/
Public class MyParser {
// Public constants
Public static final String TITLE = "MyParser ";
Public static final String VERSION = "0.0.1 ";
// Private variables
Private int iSchemaVersion;
/**
* Constructor
*/
Public MyParser (){
ISchemaVersion = 1;
}
/**
* Constructor
* @ Param...
*/
Public MyParser (iSchemaVersion ){
This. iSchemaVersion = iSchemaVersion;
}
/**
* Initialize the parser...
*/
Public void myInit () throws Exception {
....
}
/**
* Start the application
*/
Public static void main (String [] argvs ){
...
}
}
9. Field Definition
Follow these steps:
A. Public constant
B. Public variable
C. Protected constant
D. Protected variable
E. Package constant
F. Package variable
G. Private constant
H. Private variable
In addition, this article is only for IT lovers
This article is based on Baidu Library. If there are similarities, it is a coincidence that the final right of interpretation in this article belongs to this blogger.