Excellent programming style (Java article)--high salary must look

Source: Internet
Author: User
Tags finally block

Today, a whim, coding habits and programming style is very interested in, so, find a bit about the programming style (Java article) of the information, I hope to love coding or start to learn the code of the students have help!

From "The Elements of Java style"-"Java programming style," a book worth reading, will let you save a lot of time in detail, cooperation between more enjoyable!

The benefits are not much to say, but there are several principles as follows :

1. Keep the original style

2. Adhere to the principle of least surprises

3. Do it right the first time

4. Record all non-canonical behavior

Format specification:

1. Indent Nested code: Add indentation to each code block and nest, indent code, and enhance readability. These places include "class definition, inner class definition, method definition, static block, for Loop statement, If-else statement, try, catch and finally block, anonymous inner class, while statement, Do-while statement"

2. Break a long sentence: first, if a comma is included, then a line after each comma, each expression after the comma is aligned with the first letter of the expression preceding the comma. Second, you should break before the operator with the lowest priority.

3. Use blank: between the keyword and the left parenthesis, the closing parenthesis and the keyword immediately followed, except for "." Between the operator and the expression before and after it is separated by a space. Each logically separate method and code snippet, defining a class or interface between members, should include a blank line between each class and interface.

4. Do not use the tab control directly: different environments have different interpretations of the tab control.

Naming conventions:

1. The name should have a practical meaning

2. Use names that people are familiar with

3. Careful use of long names, you can use concise general abbreviations

4. Try to keep the vowel letters

5. Capitalize the first letter of an abbreviation

6. Do not use uppercase and lowercase names to differentiate

Package naming:

1. Use the reverse lowercase form of your organization's domain name as the root qualifier for the package

2. Use a separate lowercase word as the root name of each package

3. The package can use the same name only when the old and new versions are binary compatible, otherwise, use the new name

Type name:

1. The first letter of each word in the class and interface name is capitalized

Class naming:

1. Naming classes with nouns

2. Class names with group-related attributes, static services, or constants use plural form

Interface naming:

1. Naming interfaces with nouns or adjectives

Method Name:

1. The first word in the method name is lowercase, then the first letter of each word is capitalized

2. Using a verb to name a method

3. Follow the JavaBean named Property access function method: Set,get,is

Variable name:

1. Variable hit the first word in lowercase, followed by the first letter of each word capitalized

2. Naming variables with nouns

3. Set reference names to be in plural form

4. Resume for unimportant temporary variables and use a set of standard names

Field naming:

1. Use the This field variable to partition local variables

Parameter naming:

1. Constructor or "Set" method assigns a value to the field assignment parameter, the parameter name should be the same as the field name

Constant Name:

1. Each word of the constant is capitalized, and the words are connected using an underscore

Document conventions:

1. Writing documents for people who use and maintain your code

2. Note and code to synchronize

3. Use a positive tone and omit useless words

Note Type:

1. Describe the programming interface with documentation annotations

2. Hide code in standard annotation format without deleting them

3. Explain implementation details with a single-line comment

Documentation notes:

1. Describe the programming interface before writing code

2. Create documents for public, protected, package, private members

3. Write a summary and overview for each package

4. Write an overview for each application or group of packages

annotation Style :

1. Use a uniform format and organizational structure for all document annotations

2. Keywords, identifiers and constants should be placed in the <code>...</code> tab

3. Put the code in the <pre>...</pre> tab

4. Use {@link} tags when identifiers first appear

5. Label your CV for Javadoc and use a fixed set of sequences

6. Use the form of a third person narrative

7. Preparation of an independent overview

8. Omitting the subject of actions and services in the overview

9. Omitting objects and verbs in the overview of things

10. Use this instead of the to refer to an instance in the current class

11. The method name or constructor name does not require parentheses unless you want to highlight a particular signature

Comment Content:

1. Write an overview of each class, interface, field, and method

2. Complete description of each method's signature

3. Include examples

4. Writing documents for pre-, post-, and invariant conditions

5. Writing documentation for known deficiencies and deficiencies

6. Writing documents for synchronization syntax

Internal notes:

1. Add only internal comments that will help you understand your code

2. Describe why the code did it, not what it was doing

3. Avoid using end-of-line annotations

4. Interpreting local variable declarations with line-end annotations

5. Establish and use a set of keywords to identify unresolved issues

6. Mark the nesting end position in a highly nested control structure

7. If there is no break statement between the two case tags, add the "Fall-through" comment in the middle

8. Mark Empty Statements

Programming Conventions:

1. Declaring a class that represents the underlying data type as the final type

2. Establish a specific type by local type and other specific type

3. Define small classes and small methods

4. Define subclasses so that any place using the superclass can use subclasses

5. Make all fields private

6. Use polymorphism to replace instanceof

Type safety:

1. Java.lang.Object packaging General class, provide static type check

2. Encapsulating an enumeration type as a class

3. Use generics as much as possible

Statements and Expressions:

1. Replace repetitive, complex expressions with equivalent methods

2. Expressions that use block statements instead of control flow structures

3. Use parentheses to clarify the order of operations

4. Use the break statement in the last case body in the switch statement

5. Use Equals () instead of = = to detect the peer relationship of an object

Structure:

1. Objects with valid construction status

2. Do not call the non-final method from the constructor

3. Eliminate redundant code with nested constructors

Exception handling:

1. Use unchecked, run-time exceptions to report serious, unidentified errors that may have occurred in the program logic

2. Use check exceptions to report errors that can occur and rarely occur while the normal program is running

3. Report a predictable state change with a return code

4. Only convert exceptions to add information

5. Do not dispose of runtime or error exceptions privately

6. Releasing resources with a finally statement block

Assertion:

1. Programmed according to Convention

2. Implementing assertions with a useless code elimination mechanism

3. Capturing logic errors in your code with assertions

4. Preconditions and post conditions for the assertion detection method

Concurrent:

1. Use threads only where appropriate

Synchronous:

1. Avoid synchronization

2. Synchronize the interface with a synchronous wrapper

3. If the method contains several important operations that do not require synchronization, do not synchronize the entire method

4. Avoid unnecessary synchronization when reading and writing instance variables

5. Use Notify () instead of Notifyall ()

6. Use double check mode for synchronous initialization

Efficiency:

1. Initialize with lazy

2. Avoid creating unnecessary objects

3. Reinitialize and re-use objects, try not to create new objects

4. Keep the optimization work in the future

Packaging Conventions:

1. Put in the same package the types that are frequently used, changed, published, or interdependent

2. Common closure principle

3. Reuse/Release Equivalence principle

4. The principle of non-ring dependence

5. Isolate the unstable classes and interfaces in a separate package

6. Easy-to-modify packages do not depend on packages that are difficult to modify

7. Maximizing abstraction and maximizing stability

8. The high-level design and architecture as a stable abstraction, organized into a stable package

These are just a few simple list of rules, recommend reading this book.

Excellent programming style (Java article)--high salary must look

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.