Java Writing specification

Source: Internet
Author: User
Tags class definition xml parser

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,
is usuallyCOM, 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 URL, HTML)
such as:FileDescription
3, the name of the interface (Interface)
Basic andThe naming conventions for Class are similar. On the basis of satisfying the CLASSD naming rules, ensure that the first letter of the beginning is "I",
easy to do with the ordinaryClass differs from Open. 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
Basic andThe naming conventions for Class are similar. On the basis of satisfying the CLASSD naming rules, guarantee the first letter "E" at the beginning,
easy to do with the ordinaryClass differs from Open.
such as:Euserrole

5. Naming of exceptions (Exception)
Exception (Exception) usually uses 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. The return type isBoolean methods are generally 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 constant Field (Static final) 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 tops my nose and asks why our Java code indentation Format it has to be this way, not him, he likes his own, so he writes 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 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, derived from the Unix BSD programmer Eric Allman--, who 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 GNU Emacs in the source program:

if (<cond>)

{

<body>

}

so in What kind of good is 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?

turns out his indentation didn't pay attention to a style, in some places with Tab key, and some places to play space. On some different platforms, the TAB key width is not the same.

said here, I believe you 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

Habits MFC's people like to adopt 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

This provides a Hungarian notation prefix naming habits:

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

Another: This article is only for it enthusiasts to communicate

This article from the Baidu Library, if there is a similar, purely coincidental, the final interpretation of the right to this blogger owned.

Java Writing specification

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.