1, named article
- Avoid using misleading names, such as a
List
type of the variable will be namedaccountList
; do not use lowercase and uppercase letters O L named variables, because they will be confused and numbers 1 and 0 - The use of named variables have to distinguish between the meaning of words. For example,
ProductInfo
andProductData
there is no distinction;Info
andData
likethe
,a
,an
as is the confusion of nonsense; the variable name does not appearVariable
, the table name does not appearTable
- Class name does not appear
Manager
,Processor
,Data
,Info
this type of class name; class name must be a noun - Use the factory to create new objects than
new
the object better, the constructor may beprivate
of suchComplex.fromRealNameNumber(23.0)
rationew Complex(23.0)
is better - Each concept uses only one word. For example,
fetch
,retrive
andget
represent a meaning, as far as possible do not occur simultaneously a plurality of
2, function papers
- Function should be short, capping the best 20 lines
- Function should only do one thing, if a function can continue to split, it indicates that the function to do more than one thing
switch
If the sentence is too long, consider using多态
instead- Do not be afraid of long function name, it is best to use a descriptive name, and can express the meaning of such functions just fine
- Function parameters:
- Function parameters as possible, preferably not more than three, three or more may be packaged as an object
- For functions of a single parameter passing, a common role is to use this parameter to do something else, and the other is the operating parameter itself. This situation is best parameter names can distinguish between the two forms, for example
String transform(StringBuffer in)
, tell the reader the desired input and output types. - If you need to pass a Boolean value function, we may consider the use of function is divided into two functions
- The best function name from the verb nouns, such as
write(name)
not as good aswriteFiled(name)
a good, well aware of the lattername
isfiled
- Function name that clearly describes the functions of all the things done to avoid the accident to the caller bring chaos
- Use exceptions instead of returning an error code. Error codes are asking the caller immediately processing error, exception handling can be unified behind
- Using a
try...catch
code block is preferably pulled out of a single function, and then call him, to avoid confusion the main flow - Elimination of duplicate code
3, article comments
Notes the role that make up our encounter with the intent expressed in the code fails, so that the comment is a failure!
If you encounter a situation needs to write a comment, you can prioritize if you can use a variable or method name to express, such as the following, the second expression would be better:
// 判断员工是否合法,并且年龄大于65岁 if(employee.flag && employee.age > 65) if(employee.isEligibleForFullBenefits())
Good use of comments:
- Show law, author information
- Provide useful information, such as comments on the abstract function of a regular expression matching the desired format such as comments
- Explanation, a parameter more difficult to understand or return value is described
- Warning, some important code alerts, to prevent others to modify the code for
// TODO
Unfinished work annotate- Public API Javadoc
Bad annotation instances:
Surplus notes, such as whether the comments are obvious intent of the code
Misleading comments, which do not meet the actual behavior and code
Unruly type annotation, such as requiring all functions must have Javadoc comments
When can a function or variable name, do not use Notes
After the brackets behind the notes, the intention is good, but the fundamental solution would be to shorten the length function
while(xxxx){ ....... if(xxx){ ...... } // if } // while
Do not delete the code commented out, do not be afraid brought back
4, format papers
Statement used between packets, each import statements and function blank lines spaced improved code visuals
- Quality relationship "intimate" concept should be mutual close , rather than separated by an empty line
- Declare variables should be close to the location of use
- Class member variables should be declared at the top of the class, a loop variable should be declared in parentheses
- Correlation function , such as function calls A function B, then A and B together preferably, A and B on top
Each line length is preferably not too long, such as preferably 80 characters, or the characters 100 to 120
5, data structures, objects and articles
Class private variables If a value and evaluators, it is still exposed to the
Demeter's Law: internal module should not have to understand the situation it operates objects. That is, the object does not change its internal variables exposed accessor. More precisely, Demeter Law says, class
C
methodsf
can only be called the following objects方法
:- C
- Objects created by f
- Passed as a parameter to the object f
- Object member variables of C
That is, the method should not call any method function returns an object, only dialogue with friends, do not talk to strangers
6. Other
- Do not return control function, avoid the use of check
- Do not pass null arguments
- If a large number of third-party API throws an exception, you might consider using the following package
Description: This article is compiled of the book point of view, some personal point of view I feel a bit harsh is not practical, some sections directly skipped. Want to know more details, please refer to the original.