Java programming specification

xiaoxiao2021-03-06  58

Java programming specification

Copyright Notice: 9CBS is this BLOG managed service provider. If this paper involves copyright issues, 9CBS does not assume relevant responsibilities, please contact the copyright owner directly with the article Author.

Defining the purpose of this specification is to let all documents in the project look like a person written, increase readability, and reduce the loss caused by the project group. (These norms are not necessarily abolished, but must have a good readability)

Naming of Package

The name of Package should be composed of a lowercase word.

Class naming

The name of the Class must be made of uppercase of uppercase letters and other letters.

Naming of Class variables

The name of the variable must be started with a lowercase letter. The latter words begin with uppercase letters.

Static final variable name

The name of the Static Final variable should be capitalized and point out the full meaning.

Naming of parameters

The name of the parameter must be consistent with the naming specification of the variable.

Array name

Arrays should always be named in the following ways:

BYTE [] BUFFER

Instead:

BYTE BUFFER [];

Parameters of the method

Name the meaningful parameters, if possible, use the name of the field to assign the value:

SetCounter (int size) {

THIS.SIZE = Size;

}

Java file style

All java (*. Java) files must comply with the following style rules

Copyright Information

Copyright information must begin at the beginning of the Java file, such as:

/ **

* Copyright? 2000 Shanghai XXX Co. Ltd.

* All right reserved.

* /

Others do not need to appear in Javadoc, can also be included here.

Package / Imports

Package Row To be used in the IMPORT, the standard package name is before the local package name and in the alphabetical order. If the IMPORT line contains different subdirectory in the same package, you should use * to process.

Package hotlava.net.stats;

Import java.io. *;

Import java.util.observable;

Import Hotlava.util.Application;

Here Java.IO. * is used instead of InputStream and OutputStream.

Class

Next, the annotation of the class is generally used to interpret the class.

/ **

* A class representing a set of packet and byte counters

* It is observable to allow it to be watch, but only

* Reports Changs WHEN THE CURRENT SET IS Complete

* /

Next is a class definition, including extends and imports in different rows.

Public Class Counterset

Extends Observable

Implements Cloneable

Class Fields

Next is a member variable of class:

/ **

* Packet Counters

* /

protected int [] packets;

Public member variables must generate a document (javadoc). Member variables defined by Proceted, Private and Package If the name meaning is clear, you can not comment.

Access method

Next is a method of access to class variables. It is just simple to get values ​​to assign values ​​to the class variable, you can simply write on one line.

/ **

* Get the counter

.

* /

Public int [] getpackets () {return copyArray (packets, offset);

Public int [] getBytes () {return copyArray (bytes, offset);

Public int [] getpackets () {return packets;}

Public void setpackets (int [] packets) {this.packets = packets;}

Other methods don't write on a line

Constructor

Next is a constructor, it should be written in an incremental manner (such as: writing more).

Access Type ("Public", "Private", etc.) and any "static", "firm" or "synchronized" should be written in one line, and the methods and parameters can be written separately, which makes the method and parameters easier to read.

public

Counterset (int size) {

THIS.SIZE = Size;

}

Cloning method

If this class can be cloned, then the next step is Clone method:

public

Object clone () {

Try {

Counterset obj = (counterset) super.clone ();

Obj.packets = (int []) packets.clone ();

Obj.size = size;

Return Obj;

} catch (clonenotsupportedexception e) {

Throw New Internalerror ("Unexpected ClonyNotSupportedException:" E.GetMessage ());

}

}

Class method

The method of writing classes below:

/ **

* Set the packet counter

* (Such as When Restoring from a database)

* /

Protected final

Void setArray (int [] r1, int [] r2, int [] R3, int [] R4)

Throws IllegalargumentException

{

//

// ensure the arrays area of ​​equal size

//

IF (r1.length! = r2.length || r1.length! = r3.length || r1.length! = r4.length)

Throw New IllegalargumentException ("Arrays Must Be of the Same Size);

System.ArrayCopy (R1, 0, R3, 0, R1.LENGTH);

System.Arraycopy (R2, 0, R4, 0, R1.LENGTH);

}

Tostring method

In any case, every class should define the toString method:

public

String toString () {

String Retval = "Counterset:";

For (int i = 0; i

Retval = data.bytes.toString (); RetVal = data.packets.tostring ();

}

Return RetVal;

}

}

MAIN method

If the main (String []) method is defined, it should be written at the bottom of the class.

Code writing format

Code style

The code should use UNIX's format, not Windows (such as: Enter the carriage return wrap)

Documentation

You must generate a document for class with javadoc. Not only because it is standard, this is also a method recognized by various Java compilers. Using the @Author tag is not recommended because the code should not be owned by the individual.

indentation

The indentation should be 2 spaces per line. Do not save Tab characters in the source file. Tab characters will be extended to different widths due to user settings when using different source code management tools.

If you use Ultredit as your Java source editor, you can prohibit saving Tab characters by using Ultredit, using the length of the length of the tab used in the ULTREDIT, then use the Format | Tabs to Spaces menu Convert TAB to spaces.

Page wide

Page width should be set to 80 characters. The source code will generally not exceed this width and cause the unable to display, but this setting can also be flexibly adjusted. In any case, the long statement should be in a comma or one operator Fold. After a statement is folded, 2 characters should be retracted more than the original statement.

{} Correct

The statement in {} should be alone as a row. For example, the first line below is wrong, the second line is correct:

IF (i> 0) {i }; // error, {and} in the same line

IF (i> 0) {

i

}; // correct, {separate as a line

} The statement is always separate as a line.

If the} statement should be indented to the {that row relatively aligned with it.

brackets

There should be a space between the left brackets and the latter characters. Similarly, there should be a space between the right brackets and the previous characters. The following example shows the incorrect of parentheses and spaces and correct usage:

CallProc (APARAMETER); / / Error

CallProc (aparameter); / / correct

Do not use meaningless parentheses in the statement. Braces should only be in the source code for a certain purpose. The following example illustrates errors and correct usage:

IF ((i) = 42) {// error - parentheses meaningless

IF (i == 42) or (j == 42) Then // correct - indeed parentheses

Program writing specification

exit ()

Exit In addition to being called in main, other places should not be called. Because this does not give any code code opportunities to intercept exit. A similar background service ground program should not be exited because a library module determines to exit.

abnormal

The declared error should throw a runtimeException or derived exception.

The top () function of the top layer should intercept all exceptions and print (or record it in the log) on ​​the screen.

Garbage collection

Java uses mature background garbage collection techniques instead of reference count. But this will result in a problem: You must work after the instance of the object is used. For example, a PREL programmer may write:

...

{

FileOutputStream Fos = New FileoutputStream (Projectfile);

Project.save (FOS, "Ide Project File";

}

...

Unless the output stream is turned off, it is not a reference to the program language, such as Java, is a clearing work of the variable. Must be written as follows:

FileOutputStream Fos = New FileOutputStream (Projectfile); Project.Save (FOS, "Ide Project File);

Fos.close ();

Clone

Here are a useful way:

Implements Cloneable

public

Object clone ()

{

Try {

THISCLASS OBJ = (thisclass) super.clone ();

Obj.field1 = (int []) Field1.clone ();

Obj.field2 = field2;

Return Obj;

} Catch (clonenotsupportedexception e) {

Throw New Internalerror ("Unexpected ClonyNotSupportedException:" E.GetMessage ());

}

}

Final class

Never define the class as final (unless the framework requirements of the program) due to performance

If a class is not ready to be inherited, it is best to indicate in class documents, not to define it to Final. This is because no one can guarantee that it will not be inherited because of what reason.

Member variables accessed

Most of the class member variables should be defined as protected to prevent inheritance classes from using them.

Note, use "int [] packets" instead of "int packets []", the latter is never used.

Public void setpackets (int [] packets) {this.packets = packets;}

Counterset (int size)

{

THIS.SIZE = Size;

}

转载请注明原文地址:https://www.9cbs.com/read-114826.html

New Post(0)