Home  >  Article  >  Java  >  JAVA code specifications

JAVA code specifications

一个新手
一个新手Original
2017-10-10 09:32:391382browse

1. Naming specifications

1. Identifier: unity, expressiveness, conciseness

Unification: There are many ways to express a word. We do not seek the best, but seek unity; for example: Supplier can be either supplier or provider, choose one to use uniformly;

Expression: clearly express its meaning, correct and rich; example: the difference between newSupplier and supplier1, service2;

Simplicity: Under the premise of unity and conveying the meaning, be concise; if the meaning cannot be conveyed, please abandon the simplicity;

Camel Rule: Do not use special symbols to separate, but use the first letter to capitalize; Example: SupplierName , addNewContract;

English and Pinyin: Try to use simple English or use all Pinyin, reject mixed use of English Pinyin;

2. Package name: lowercase, link

Package Name: all lowercase; do not use special symbols to separate;

3. Class name

Class name: the first letter is capitalized, and each word is capitalized

Suffix: Service , Impl, Inter, Dao, Action, listener, Event, Servlet, Factory, Job..

4, method name

Method name: first The first letter of a word is lowercase, and the first letter of subsequent words is capitalized

Prefix: create, delete, add, remove, initialize, destroy, open, close, read, get, copy, modity, send, find..

2. Spaces and empty lines

Use of spaces (1 immediately following, 2 before and after): 1 when separating; 2 operators; 1 comma; example: call(a, b, c);b - d = e;return a == b ? 1 : 0;if (a > b)

Use of blank lines: semantic operations; between methods; more than ten lines;

3. Code Comments

The code is confusing and no amount of comments can make up for it; expressiveness + unity + conciseness + clear structure == fewer comments

Comments: less but more precise; Code meaning comments; non-code management; svn deletion era

Use // for a single line, /* .. */ for multiple lines; process-surrounding; process-curly brackets;

4. Best practices

 1. Lazy programmers must keep the optimal code every time they save and submit; each time they save must be the most beautiful code;

 2. Log More ratings make it better than println;

3. Add parentheses after if while for;

4. //TODO lets everyone know what you have not done;

 5. Use ";" or "//nothing ti do" to explain that you deliberately do not need to do anything;

 6. Reduce nesting levels; reduce true/false judgments; reduce the role of variables Domain; reduce the use of methods that return results; put variable declaration, initialization, and usage together as much as possible;

The above is the detailed content of JAVA code specifications. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn