Java Coding Specification

Source: Internet
Author: User
Tags naming convention tag name

Coding specifications are important for developers, for several reasons:

1. In the life cycle of a software, 80% of the cost is maintained

2. Virtually no software, throughout its lifecycle, is maintained by the initial developer

3. The coding specification can improve the readability of the software, allowing programmers to understand the new code as quickly and thoroughly as possible.

4. If you publish the source code as a product, you need to make sure it is well packaged and clear, like any other product you have built

5. Provide standard code formats for different project groups or individuals.

6. Increased legibility.

I have to go to Java development after two years of working without Java, so I sort out the following coding specifications to follow in the future.

A naming convention

1) Package: Names should all be nouns or noun phrases, all lowercase, between words with "." The general use of the company/organization website domain name reverse followed by the specific software internal module name

Package Naming example: Packages Com.sun.java; com.mycompany.db;

2) class: noun or noun phrase, each word capitalized; generally do not use abbreviations, unless their abbreviations are more general and easy to understand, such as HTML; When you want to differentiate between interfaces and implementation classes, you can add "Impl" to the class, for example: interface container and Class The Containerimpl;exception class uses "Exception" as the end of the class name, for example: datanotfoundexception; abstract class begins with "abstract" as the name of the class, For example, the Abstractbeandefinition,abstractbeanfactory;test class should be able to use "Test" as the end of a class name. Example: Containertest

Class Naming example: Class person; class Springrain

3) Interface: Naming rules with "class"

Example of interface naming: interface Runner;

4) Methods: verb or moving part of speech phrase, first letter lowercase, second and later words capitalized;

Method Naming example: Run (); Displayinformation ();

5) Variable: noun or noun phrase, first letter lowercase, second and subsequent words capitalized;

Do not advocate the use of the following line "_" and the dollar symbol "$" as a variable at the beginning of the word can be separated by the line, the variable name should not be too long but also meaningful, unless it is temporarily used (for example, only a few lines of code) is discarded after the case, do not recommend using a single letter variable name Commonly used temporary variable names include i,j,k,m,n that represent integers and C that represent letters, and E, which represents the exception object.

Example of variable naming: int age; int student_age; String studentname; Exception e;

6) Constant: Each word should be capitalized, the words are separated by "_".

Example of constant naming: final static min_width = 4;final static default_container_size = 20;

7) Name of the file: the Java source program file ends in. java, and the compiled file ends with a. Class. For example: Container.java,container.class.

Summary: The first letter of the class name should be capitalized. The first letter of the field, method, and object (handle) should be lowercase. For all identifiers, all the words contained in it should be close together, and uppercase the first letter of the middle word. If a constant initialization character appears in the definition, all letters in the static final base type identifier are capitalized. This can be used to mark the constants that they belong to the compile period. The Java package is a special case: they are all lowercase letters, even if the middle word is the same. For domain name extension names, such as Com,org,net or EDU, all should be lowercase.

Ii. Organization of documents

1. A file consists of a paragraph separated by a blank line and an optional comment identifying each paragraph. A program with more than 2000 lines is difficult to read, so the number of lines of code in a Java program file cannot exceed 2000 lines, unless there are special reasons. Each Java source file contains a single public class or interface. If private classes and interfaces are associated with a common class, they can be placed in the same source file as the public class. The public class must be the first class or interface in this file.

The Java source file also follows these rules, which specify the order of the Java program paragraphs:

-Opening Notes

-Package and Introduction statements

-Class and interface declarations

2. In a Java class, the order of the parts:

1) Attribute declaration

2) Construction Method declaration

3) Static statement

4) General method declaration

5) Main method declaration

6) Declaration of the Inner class

Summary: Generally a complete Java file content is as follows:

1) Definition of package

2) Impot Class (Enter the order of the package, avoid using *) input package should follow the Java.*.*,javax.*.*,org.*.*, com.*.* Order import, in the import should not use * (for example: java.util.*)

3) Definition of class or interface

4) static variable definition, according to Public,protected,private order

5) instance variable definition, by public,protected,private order

6) Construction method

7) method definition order according to public method (class own method), implement interface method, overloaded public method, protected method, package scope method and private method.

Note: The number of lines of code for each method in a generic class does not exceed 100 lines.

Three separators and indents

1. Use blank lines

Use a single line of blank lines to separate them in the following cases:

1) between the methods

2) between the logical paragraph subsections of the method's internal code

3) After a local variable is declared in the method, the specific Java statement precedes

4) before the comment line

5) separate multiple classes/interfaces defined in the same source file using two rows of blank lines

2. Using whitespace

In addition to the normal components separated by space characters (such as data types and variable names), a space character should be used to delimit the following cases:

1) between operators and operators, such as: C = a + B;

2) after the comma in the argument list, such as: void M1 (int year, int month) {}

3) After if,for,while for statements such as: if (a > B) {}

3. Use line break

It is not recommended to write multiple statements on a line, the length of a statement is generally more than 80 characters, should be wrapped;

When a row of expressions cannot be displayed on a single line, the following order is required to split the lines:

1) after the "(" or "=" symbol, split the line

2) in "," to split the line

3) split the line after an operator

4) indent concurrent splits at the same level

5) If you encounter "(" when splitting again in the split line, the newly split line should be placed at a further indent level

4. Use parentheses

You can use parentheses to dictate the order of operations

5. Use curly Braces

The start position can be at the end of the current content, or another row

6. Using indentation

Apply indentation in the following cases

1) composition in the class

2) Composition in the method body or statement block

3) Non-starting line when swapping lines

Reduction is generally based on the first level of the composition of the indentation of four spaces, and Eclipse default format is 1 tab, you can change the following way:window--> preferences......--> java--> Code styl E--> Formatter Point to the right of the "Edit ..." button, switch to the first tab "indentation", in the general Settings group to change the tab policy from Table only to Space only, while the I Ndentation size and Tab size to 4, and finally the top profile name to take a name, any name, then click OK button on it. Pressing the TAB key later or Ctrl + Shift + F automatically takes 4 spaces as a level indent. In the Eclipse development tool, after writing the code, you can use the shortcut key: CTRL + SHIFT + F to automatically format the code.

Four Declaration statements

1. Variable declaration: Each row declares a variable and adds a comment. For example:

int count; Number of containers

int count, size; AVOID this!

Array declarations should be prefixed. For example:

Int[] table;

String[] args;

2. class or interface declaration

1) The "{" and declaration statements are on the same line.

2) If it cannot be displayed on the same line, then "extends" or "implements" are split and placed after two indent levels.

3) The "}" symbol should be on its own line.

For example:

public class Chiefexecutiveofficer

Extends Manager

Implements person {

...

}

3. Method declaration

The "{" and the declaration statements are on the same line.

The "}" symbol should be on its own line.

Summary: Each row declares a variable and, if possible, initializes it while declaring the variable, unless its initial value is not yet determined. Local variables should be declared at the beginning of the method or block of statements in which they reside, and should not be accompanied by declarations.

Five notes

Java has two types of annotation methods. "/* This is a comment */" or "//This is a comment"

The first should be used to write Javadoc, and all begin with "/**".

The second one is suitable for commenting on part of the code, but only for the very short content.

In addition, for HTML, XML, CSS, JavaScript I want to abide by the encoding specifications are as follows:

1,html, XML because of the level of indentation is more, it is recommended to use two spaces as a layer of indentation;

2,css and JavaScript, in order to work with HTML, also use two spaces as a layer of indentation;

3,html, XML tag name, attribute said all use lowercase letters, HTML as far as possible to conform to XHTML specifications, avoid using XHTML deprecated tags, such as: font, applets and so on;

The attribute values of 4,html are defined by double quotation marks;

5,javascript use standard syntax, objects, and so on, variable declarations can be added with var. string constants uniformly using single quotation marks

Java Coding 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.