The style of writing code of excellent Java programmers, no longer leave pits for others

Wonderful recommendation in the past   


 

[1] Summary of Java Web technology experience

[2] 15 top Java multi-threading interview questions and answers, come and see

[3] Ten Java interview questions that interviewers like to ask

[4] Speak JAVA from scratch, giving you a clear learning path! Learn what to learn! !

[5] Welfare: 100G Java full set of learning videos are sent for free

 

 

Today, I came across a whim, and I am very interested in coding habits and programming styles, so I found some information on programming styles (Java articles), hoping to help students who love coding or start learning coding!

 

From "The Elements of Java Style" - "Java Programming Style", a book worth reading, it will save you a lot of time on the details, and the cooperation between them is more enjoyable!

 

Not much to say about the benefits, but a few principles are as follows 


 

1. Keep the original style

2. Adhere to the principle of least surprise

3. Do it right the first time

4. Document all non-normative behavior

 

format specification


 

 

1. Indent nested code: Add indentation to each code block and nesting, indent the code, and enhance readability. These places include [class definitions, inner class definitions, method definitions, static blocks, for loop statements, if-else statements, try, catch and finally blocks, anonymous inner classes, while statements, do-while statements]

 

2. Break long sentences: First, if commas are included, start a new line after each comma, aligning each expression after the comma with the first letter of the expression before the comma. Second, the line should be broken before the operator with the lowest precedence.

 

3. Use whitespace: between the keyword and the left parenthesis, between the right parenthesis and the following keyword, and operators other than "." and the expressions before and after it are separated by spaces. A blank line should be added between each logically independent method and code segment, between members of a defined class or interface, and between each class and interface.

 

4. Don't use the Tab controller directly: Different environments interpret the Tab controller differently.

 

naming convention


 

 

1. The name should have practical meaning

2. Use familiar names

3. Be careful with long names and use concise and generic abbreviations

4. Keep the vowels as much as possible

5. Capitalize the first letter of abbreviations

6. Don't use case to distinguish names

 

package naming


 

 

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

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

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

 

type naming


 

 

1. Capitalize the first letter of each word in class and interface names

 

class naming


 

 

1. Name classes with nouns

2. Class names with groups of related properties, static services or constants use plural form

 

interface naming


 

 

1. Name the interface with a noun or adjective

 

method naming


 

 

1. The first word in the method name is lowercase, and the first letter of each subsequent word is uppercase

2. Name methods with verbs

3. Follow the named property access function method in JavaBean: set, get, is

 

variable naming


 

 

1. The first word hit by the variable is lowercase, and the first letter of each subsequent word is uppercase

2. Name variables with nouns

3. Use the plural form of the collection reference name

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

 

field naming


 

 

1. Use this field variable to distinguish local variables

 

parameter naming


 

 

1. The constructor or "set" method assigns parameter assignments to the field. The parameter name should be the same as the field name.

 

constant naming


 

 

1. Each word of the constant is capitalized, and the words are connected by underscores

 

Documentation conventions


 

 

1. Document the people who use and maintain your code

2. Comments and code should be synchronized

3. Use a positive tone and omit useless words

 

Annotation type


 

 

1. Use documentation comments to describe the programming interface

2. Hide code with standard comment formatting without having to delete them

3. Explain implementation details with single-line comments

 

Documentation Notes


 

 

1. Describe the programming interface before writing code

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

3. Write summaries and overviews for each package

4. Write an overview for each application or group of the package

 

Annotation style


 

 

1. Use a consistent format and organization for all documentation comments

2. Keywords, identifiers and constants should be placed in <code>…</code> tags

3. Put the code in the <pre>…</pre> tags

4. Use the {@link} tag on the first occurrence of the identifier

5. Tag your resume for Javadoc and use a fixed set of sequences

6. Use the form of third-person narration

7. Write a standalone overview

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

9. Omit objects and verbs in things overview

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

11. Parentheses are not required for method or constructor names unless you want to highlight a special signature

 

Comment content


 

 

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

2. Completely describe the signature of each method

3. Include examples

4. Document Pre, Post, Invariant Conditions

5. Document known bugs and deficiencies

6. Document the Synchronous Syntax

 

Internal comments


 

 

1. Only add internal comments that help understand your code

2. Describe why the code does what it does, not what it does

3. Avoid end-of-line comments

4. Explain local variable declarations with end-of-line comments

5. Build and use a set of keywords to identify unresolved problems

6. Mark the end of nesting in highly nested control structures

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

8. Mark empty statements

 

programming conventions


 

 

1. Declare the class representing the underlying data type as final type

2. Build Concrete Types from Local Types and Other Concrete Types

3. Define small classes and small methods

4. Define subclasses so that wherever you use the superclass you can use the subclass

5. Make all fields private

6. Use polymorphism instead of instanceof

 

type safety


 

 

1. Wrap generic classes in java.lang.Object to provide static type checking

2. Encapsulate enumeration types in the form of classes

3. Try to use generics

 

Statements and Expressions


 

 

1. Replace repeated, complex expressions with equivalent methods

2. Use block statements instead of expressions for 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 object equivalence

 

structure


 

 

1. Construct an object with a valid state

2. Don't call non-final methods from constructors

3. Eliminate redundant code with nested constructors

 

exception handling


 

 

1. Use unchecked, runtime exceptions to report serious unspecified errors that may go wrong in program logic

2. Use checked exceptions to report errors that may occur but rarely occur during normal program execution

3. Report predictable state changes with return codes

4. Only convert exceptions to add information

5. Do not dispose of runtime or error exceptions without permission

6. Use finally block to release resources

 

Affirmation


 

 

1. Program by convention

2. Implement assertions with useless code elimination mechanisms

3. Use assertions to catch logical errors in your code

4. Detect the preconditions and postconditions of methods with assertions

 

Concurrency


 

 

1. Use threads only where appropriate

 

Synchronize


 

 

1. Avoid Sync

2. Use a synchronous wrapper to provide a synchronous interface

3. If the method contains several important operations that do not need synchronization, then 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-checked mode for synchronous initialization

 

effectiveness


 

 

1. Use lazy initialization

2. Avoid creating unnecessary objects

3. Reinitialize and reuse objects, try not to create new objects

4. Leave the optimization work for later

 

Packaging conventions


 

 

1. Put frequently used, changed, concurrently released or interdependent types in the same package

2. The principle of common closure

3. Reuse/Release Equivalence Principle

4. Acyclic Dependency Principle

5. Isolate unstable classes and interfaces in separate packages

6. Easy-to-modify packages don’t depend on hard-to-modify packages

7. Maximize abstraction to maximize stability

8. Organize high-level design and architecture as stable abstractions into stable packages

 

The above is just a simple list of some rules, it is recommended to read this book.

 

Recommended in the past   


 

[1] Summary of Java Web technology experience

[2] 15 top Java multi-threading interview questions and answers, come and see

[3] Ten Java interview questions that interviewers like to ask

[4] Speak JAVA from scratch, giving you a clear learning path! Learn what to learn! !

[5] Welfare: 100G Java full set of learning videos are sent for free

 

My public number, welcome to pay attention! Get more technical dry goods!


 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325371498&siteId=291194637