"Code tidy way" finishing

1, named article

  1. Avoid using misleading names, such as a Listtype of the variable will be named accountList; do not use lowercase and uppercase letters O L named variables, because they will be confused and numbers 1 and 0
  2. The use of named variables have to distinguish between the meaning of words. For example, ProductInfoand ProductDatathere is no distinction; Infoand Datalike the, a, anas is the confusion of nonsense; the variable name does not appear Variable, the table name does not appearTable
  3. Class name does not appear Manager, Processor, Data, Infothis type of class name; class name must be a noun
  4. Use the factory to create new objects than newthe object better, the constructor may be privateof such Complex.fromRealNameNumber(23.0)ratio new Complex(23.0)is better
  5. Each concept uses only one word. For example, fetch, retriveand getrepresent a meaning, as far as possible do not occur simultaneously a plurality of

2, function papers

  1. Function should be short, capping the best 20 lines
  2. Function should only do one thing, if a function can continue to split, it indicates that the function to do more than one thing
  3. switchIf the sentence is too long, consider using 多态instead
  4. 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
  5. Function parameters:
    1. Function parameters as possible, preferably not more than three, three or more may be packaged as an object
    2. 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.
    3. If you need to pass a Boolean value function, we may consider the use of function is divided into two functions
  6. The best function name from the verb nouns, such as write(name)not as good as writeFiled(name)a good, well aware of the latter nameisfiled
  7. Function name that clearly describes the functions of all the things done to avoid the accident to the caller bring chaos
  8. Use exceptions instead of returning an error code. Error codes are asking the caller immediately processing error, exception handling can be unified behind
  9. Using a try...catchcode block is preferably pulled out of a single function, and then call him, to avoid confusion the main flow
  10. Elimination of duplicate code

3, article comments

  1. Notes the role that make up our encounter with the intent expressed in the code fails, so that the comment is a failure!

  2. 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())
  3. Good use of comments:

    1. Show law, author information
    2. Provide useful information, such as comments on the abstract function of a regular expression matching the desired format such as comments
    3. Explanation, a parameter more difficult to understand or return value is described
    4. Warning, some important code alerts, to prevent others to modify the code for
    5. // TODOUnfinished work annotate
    6. Public API Javadoc
  4. Bad annotation instances:

    1. Surplus notes, such as whether the comments are obvious intent of the code

    2. Misleading comments, which do not meet the actual behavior and code

    3. Unruly type annotation, such as requiring all functions must have Javadoc comments

    4. When can a function or variable name, do not use Notes

    5. 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
    6. Do not delete the code commented out, do not be afraid brought back

4, format papers

  1. Statement used between packets, each import statements and function blank lines spaced improved code visuals

  2. Quality relationship "intimate" concept should be mutual close , rather than separated by an empty line
  3. Declare variables should be close to the location of use
  4. Class member variables should be declared at the top of the class, a loop variable should be declared in parentheses
  5. Correlation function , such as function calls A function B, then A and B together preferably, A and B on top
  6. 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

  1. Class private variables If a value and evaluators, it is still exposed to the

  2. 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 Cmethods fcan only be called the following objects 方法:

    1. C
    2. Objects created by f
    3. Passed as a parameter to the object f
    4. 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

  1. Do not return control function, avoid the use of check
  2. Do not pass null arguments
  3. 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.

Guess you like

Origin www.cnblogs.com/moongeek/p/11373468.html