Java programming specification, the code acceptance criteria

introduction:

This standard is a measure of the defect code itself, is a measure of the value of R & D personnel themselves.

Army Regulation One: Avoid Using a magic number in the program, it must be identified with a meaningful constant. ]

Two army regulations: function [clear method, a way to accomplish only one function. ]

Army regulations three: Methods parameters can not be more than 5]

Army Regulation IV: [Method Invocation try not to return null, instead to throw an exception, or return exceptions objects (SPECIAL CASE object, SPECIAL CASE PATTERN ); in order for collection or array type as a method return values, or replace it with an empty set 0 length of array. ]

Army Regulation V: [database during operation or IO, we must ensure that resources are released after use, and must ensure that the release operation performed finally in. ]

Army Regulation VI: [exception caught do not directly catch (Exception ex), should be an exception subdivision process. ]

Army Regulation seven: [For if "else if" (follow-up may have multiple else if ...) This type of conditional, must contain a final else branch, branch to avoid errors caused by missing; each switch-case statements must ensure that there is default, to avoid missing branches, resulting in an error. ]

Army Regulation Eight: [override object equals must override hashCode () method when () method. ]

Army Regulation nine: Create a new thread [ban cycle, try to use the thread pool. ]

Ten military regulations: [when performing accurate calculation (e.g.: currency) avoid the use of float and double, floating point calculations are inaccurate, or must BigDecimal to convert the floating-point integer arithmetic operation. ]

Army Regulation Description:

Army Regulation One: Avoid Using a magic number in the program, it must be identified with a meaningful constant. ]

Description: Whether the devil figure is based on the principles of the global easy to read and easy to replace. 0,1 enumeration constants must be defined as the value of certain physical areas of expertise, "the devil constants" of similar NUMBER_ZERO is strictly prohibited.

Two army regulations: [clear function method, a way to accomplish only one function. ]

Description: Method function too, will increase the complexity of the process and dependencies, is not conducive to reading programs and future ongoing maintenance, regardless of the method or class design should be consistent with the principle of single responsibility.

Army regulations three: Methods parameters can not be more than 5]

Explanation: Too many parameters that affect the code to read and use, to reduce the argument, we must first consider the reasonableness of these parameters, keeping method features a single, optimized design method, if the argument does not reduce, the number of parameters can be encapsulated into a class (an object ), taking into account the behavior of the corresponding increase in the new class (objects) in order to be more in line with OOP.

Army Regulation IV: [Method Invocation try not to return null, instead to throw an exception, or return exceptions objects (SPECIAL CASE object, SPECIAL CASE PATTERN); in order for collection or array type as a method return values, or replace it with an empty set 0 length of array. ]

Description: Returns null null pointer add unnecessary judgment, judgment will lead to serious omission NullPointerException error.

Army Regulation V: [database during operation or IO, we must ensure that resources are released after use, and must ensure that the release operation performed finally in. ]

Description: Database operations, IO operation needs to close the object must try -catch-finally in the finally close (), if there are multiple IO object need to be closed, try-catch were required for each object close () method, IO object is closed to prevent a failure of other IO object which is not closed. Recommended Practice as follows:

Connection jdbcConnection = null;
       Statement stmt = null;
       try
       {
            ........
       }
       catch(SQLException e)
       {
            ........
       }
       finally
       {
           if(stmt != null)
           {
                try
                {
                    stmt.close();
                }
                catch(SQLException e)
                {
                    logger.log(Level.WARNING,"异常说明", e);
                }
           }
           if(jdbcConnection != null)
           {
                try
                {
                    jdbcConnection.close();
                }
                catch(SQLException e)
                {
                    logger.log(Level.WARNING,"异常说明", e);
               }
           }
       }

Army Regulation VI: [exception caught do not directly catch (Exception ex), should be an exception subdivision process. ]

Description: catch (Exception ex) will result RuntimeException exception catching, RuntimeException is a runtime exception, the program itself is ill-considered and thrown exception is BUG program, such as invalid parameters, array bounds, division by zero, etc., the program must sure not to throw RuntimeException exceptions, not allowed to display abnormal RuntimeException capture is easy to find in order to facilitate testing procedures.

Army Regulation seven: [For if "else if" (follow-up may have multiple elseif ...) This type of conditional, must contain a final else branch, branch to avoid errors caused by missing; each switch-case statement must guarantee there are default, to avoid missing branches, resulting in an error. ]

Army Regulation Eight: [override object equals must override hashCode () method when () method. ]

Description: equals and hashCode method is the basis for efficient operation of the object in the hash container, right override these two methods to ensure finding objects in a hash container correctness, while a good hashCode method can significantly improve the efficiency of hash container.

Army Regulation nine: Create a new thread [ban cycle, try to use the thread pool. ]

Ten military regulations: [when performing accurate calculation (e.g.: currency) avoid the use of float and double, floating point calculations are inaccurate, or must BigDecimal to convert the floating-point integer arithmetic operation. ]

Description: floating-point operations provides a good approximation on a wide range of range, but it can not yield accurate results. Calculated for binary floating-point precision is not suitable because it is impossible to any other secondary or negative power 0.1-- 10 is an accurate representation of a finite length binary fraction.

Guess you like

Origin blog.csdn.net/bj_chengrong/article/details/83614668