Java Program Writing specification
Naming conventions
1. General concept
1. Use full English descriptors as far as possible
2. Adoption of terminology applicable to related fields
3, the use of mixed case to make the name readable
4, to minimize the use of abbreviations, but if used, must conform to the entire project of the unified definition
5. Avoid using long names (less than 15 letters for normal selection)
6, avoid the use of similar names, or just the case of different names
7. Avoid using underscores (except static constants, etc.)
2. Identifier type description
1. Naming of packages (package)
The package name should be in full English descriptors, which are made up of a lowercase word. And the prefix of the package name is always a top-level domain,
Usually com, edu, gov, mil, net, org, etc.;
such as: Com.yjhmily.test
2. Naming of classes (class)
The class name should be a noun, in a mixed case, capitalized by the first letter of each word. Try to ensure that the class name is concise and rich in description.
Use full words to avoid abbreviations (unless the project has a uniform abbreviation specification or the abbreviation is more widely used, like URLs, HTML)
such as: filedescription
3, the name of the interface (Interface)
Similar to the naming conventions of Class. On the basis of satisfying the CLASSD naming rules, ensure that the first letter of the beginning is "I",
Easy to distinguish from normal class. Its implementation class name takes the second letter of the interface name to the last, and satisfies the naming specification of the class name;
such as: Imenuengine
4. Enumeration (enum) naming
Similar to the naming conventions of Class. On the basis of satisfying the CLASSD naming rules, guarantee the first letter "E" at the beginning,
Easy to distinguish from normal class.
such as: Euserrole
5. Naming of exceptions (Exception)
Exceptions (Exception) typically use the letter E to indicate an exception, and for a custom exception class, the suffix must be Exception
such as: businessexception
6. Naming Methods (method)
The method name is a verb, in mixed case, the first letter in lowercase, followed by the first letter of the word capital.
The method name describes the action behavior of the method as much as possible. A method that returns a Boolean of type is typically preceded by "is" or "has"
such as: Getcurrentuser (), AddUser (), hasauthority ()
7, the name of the parameter (Param)
The first letter is lowercase, followed by the first letter of the word. The parameter number name does not allow an underscore or dollar sign to start,
Although this is syntactically permissible. Parameter names should be short and descriptive.
such as: Public UserContext getloginuser (String loginName);
8, the name of the constant field (Constants)
Static constants fields (static final) are all uppercase letters, and the words are separated by underscores;
such as: public static final Long FEEDBACK;
public static Long User_status;
1. The style must be consistency (consistent)
A fellow man with my nose asked, why does our Java code indentation format have to be this way, but not him, he is like his own, so he wrote the code is always in his own custom style. The results were killed in code review and ordered to be modified. So he was very much disobedient. Is the consistency of style. In fact, his style, there is no problem, but in the project, and other programmers of the style of the program, it seems shut, there is a problem. For example, the indentation, such as the variable name method, different classes, different methods, each different, this program is very difficult to see. So once you've chosen a style, be sure to keep it consistent. If a project has a style, even if it does not conform to your own habits, it must be consistent, should not be unconventional.
2. Indent style (indent)
Since the indentation is speaking, the first thing to say is the indentation style; In general, Class C, like Java, uses indentation. And commonly used, there are four kinds of
A.k&r style
This is the first indentation style of the C program, by the inventor of C Ritchie and his collaborators Kernighan pioneered the use of:
if (<cond>) {
<body>
}
It is characterized by curly braces and if judged on the same line. Typically, indents are 8 spaces or a TAB key, but in C + + and Java, 4 spaces are often indented. Some people like to use two spaces, stealing thought bad, not obvious.
B. BSD Style
Also known as Allman Style, from the UNIX BSD programmer Eric allman--he wrote many programs for BSD:
if (<cond>)
{
<body>
}
Features: curly braces and conditional judgments are divided into two lines.
C. Whitesmith Style
This style stems from Whitesmith C:
if (<cond>)
{
<body>
}
D. GNU Style
This style is only in the GNU Emacs source program:
if (<cond>)
{
<body>
}
So what kind of good is it in Java? It is recommended to use a or B only. Sun has a Java Code Name convention, which is recommended by a.
3. tab or space (Tabs vs space)
or indentation issues. How much distance should one indent be? Is it eight spacebar, or is it a TAB key?
Java has a feature that is cross-platform. However, a cross-platform refers to its class that can run on a virtual machine on a different platform. Java's source programs, sometimes not cross-platform. What the? The source program can also not cross platform? Yes. Once a programmer sent some code, open in my environment, the program ugly Mess, there is the indentation of the place, no indentation, and some are indented a large section, like a rock, rugged and uneven. This kind of code quality can not! Asked the programmer to ask, replied that he was very beautiful, he also devoted time to beautify them?
Originally his indentation did not pay attention to a style, in some places with the TAB key, some places to play space. On some different platforms, the TAB key width is not the same.
Said here, I believe we are more clear, in the indentation, try not to tab, but with a space. Fortunately, many editors can define the TAB key on your keyboard as a few spaces. Go and define it quickly!
How many spaces do you use each time? 4 of them. 2 Too few are not obvious, 8 are too much to occupy space.
4. Line width
Said 8 space bar space, then I have 320 columns, 8 blank what? Stop! Please do not exceed 80 columns in one line. Many terminals a screen can only display 80 columns for them to think about bar. If your statement is too long, try to break it and write it in a few columns.
5. Variable naming
SUN Java Code Convention defines the naming conventions for package, class, method, and common variables in Java, so I don't have to waste any more bytes. Here are some caveats and some of the ways that Sun didn't mention but everyone used to.
A. Naming variables with meaningful names
First, name your variables with a complete English word or a conventional shorthand, such as:
FirstName
ZipCode
If English is not good enough, at least with other people can understand the pinyin named, such as
Zhuzhi (Address)
A random shorthand, or a random name, no one can understand the meaning of your variable:
Fn
Zc
Zz
B. Constants are named with all caps and draw lines
The constant in Java is the static final:
Static final smth_bbs= "bbs.tsinghua.edu.cn";
C. Naming collection class variables with complex numbers
Collection include arrays, vectors, and so on. When naming, use the plural:
Customers
Classmates
It can also be named with some modifiers:
Somestudents
Alldepartments
D. Cyclic variables
Generally everyone uses I, J, K and so on to do the cyclic variable.
E. Stream variable
General habits are used in and out as stream variables, corresponding to InputStream, OutputStream
Class. If you read and write the iostream, you can use InOut.
F. Naming conventions for variables
People who are accustomed to MFC prefer to use the Hungarian nomenclature (Hungarian Notation). If you're used to this, use it, but be careful to be consistent with project people. Some people are accustomed to C + + in the next line of the way, can also be used.
Hungarian Notation:
Sfirstname
Under Score Style:
_FirstName
Here is a prefix naming habit 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, here is an example:
A. File header description (none available)
B. Package definition
C. Blank Line
D. import statement
E. Empty lines
F. Class definition
Such as:
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
Disorderly order, looks very disagreeable. The classes that you want to import should be categorized in the order listed:
A. Java standard Class (java.*)
B. Java extension Class (javax.*)
C. Third-party classes
D. Your application's class
Also note that they are annotated in third-party classes, stating their origin:
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 declarations
C. Fields declaration
D. Blank Line
E. Constructors
F. Empty line
G. Methods (not including main)
H. Empty line
I. Inner class
J. Blank Line
K. Main ()
Cases:
/**
* This was 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
Please follow the following order:
A. Public constants
B. Public variables
C. Protected constants
D. Protected variables
E. Package Constants
F. Package variables
G. Private constants
H. Private variables
Java Writing specification