Transfer (Huawei Java programming military regulations, quarterly code acceptance criteria)

 

Huawei Java programming military regulations, quarterly code acceptance criteria

 

introduction:

This standard is to measure the defects of the code itself, and also to measure the value of a developer.

 

Military Regulation 1: [Avoid using devil numbers in the program, and must use meaningful constants to identify them.

Military Regulation 2: [Clarify the function of the method, and a method only completes one function.

Military regulation 3: [Method parameters cannot exceed 5]

Military Regulation 4: [Try not to return null for method calls, instead throw an exception, or return a special case object (SPECIAL CASE object, SPECIAL CASE PATTERN); for methods that use a collection or array type as a return value, replace it with an empty collection or 0-length array.

Military Regulation 5: [When performing database operations or IO operations, you must ensure that resources are released after use, and you must ensure that the release operation is performed in finally.

Military Regulation 6: [Do not directly catch (Exception ex) for exception capture, and handle exceptions in subdivisions.

Military Regulation 7: [For the conditional judgment of if „else if „ (there may be multiple else if ...), an else branch must be included at the end to avoid errors caused by branch omission; each switch-case statement must Guaranteed to have default to avoid branch omission and cause errors.

Military Regulation 8: [When overriding the equals() method of an object, the hashCode() method must be overridden at the same time.

Military Regulation IX: [It is forbidden to create new threads in the loop, and try to use the thread pool as much as possible.

Military Regulation 10: [Avoid using float and double when performing precise calculations (for example: currency calculations). Floating point calculations are imprecise and must use BigDecimal or convert floating point operations to integer operations.

 

Military regulations

Military Regulation 1: [Avoid using devil numbers in the program, and must be identified with meaningful constants.

Explanation: Whether it is a devil number is based on the principles of easy reading and global replacement. When 0 and 1 are enumerated values ​​of physical quantities in a certain professional field, constants must be defined, and "devil constants" similar to NUMBER_ZERO are strictly prohibited.

 

Military Regulation 2: [Clarify the function of a method, and a method only completes one function.

Note: There are too many methods and functions, which will increase the complexity and dependencies of the method, which is not conducive to program reading and continuous maintenance in the future. Both method and class design should comply with the single responsibility principle.

 

Military Regulation 3: [Method parameters cannot exceed 5 ]

Note: Too many parameters affect the reading and use of the code. In order to reduce the parameters, the rationality of these parameters must first be considered, the method function should be kept single, and the method design should be optimized. If the parameters cannot be reduced, multiple parameters can be encapsulated into a class (object ), and consider adding corresponding behaviors to new classes (objects) in order to be more in line with OOP .

 

Military Regulation 4: [Try not to return null for method calls , instead throw an exception, or return a special case object (SPECIAL CASE object, SPECIAL CASE PATTERN); for methods that use a collection or array type as the return value, replace it with an empty collection or 0-length array.

Note: Returning null will increase unnecessary null pointer judgments, and omission of judgments will also lead to serious NullPointerException errors.

 

Military Regulation 5: [When performing database operations or IO operations, you must ensure that resources are released after use, and you must ensure that the release operation is performed in finally.

Note: Objects that need to be closed for database operations, IO operations, etc. must be close() in the finally of try-catch-finally. If there are multiple IO objects that need to be closed, you need to try-catch the close() method of each object separately. Prevents one IO object from failing to close and other IO objects are not closed. The recommended practice is 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, " Exception Description", e);

                }

           }

           if (jdbcConnection != null)

           {

                try

                {

                    jdbcConnection.close();

                }

                catch (SQLException e)

                {

                    logger.log(Level.WARNING, " Exception Description", e);

               }

           }

       }

 

Military Regulation 6: [Do not directly catch (Exception ex) for exception capture  , and handle exceptions in subdivisions.

Description: The result of catch (Exception ex) will capture RuntimeException exception. RuntimeException is a runtime exception, an exception thrown by the program itself without careful consideration, and a bug of the program, such as invalid parameters, array out of bounds, division by zero, etc. The program must Make sure that RuntimeException cannot be thrown, and it is not allowed to explicitly catch RuntimeException in order to easily find program problems during testing.

 

Military Regulation 7: [For  the conditional judgment of if „else if „ (there may be multiple elseif...), an else branch must be included at the end to avoid errors caused by branch omission; each switch-case statement must ensure that There is default to avoid branch omission and cause errors.

 

军规八:【覆写对象的equals()方法时必须同时覆写hashCode()方法。】

说明:equals和hashCode方法是对象在hash容器内高效工作的基础,正确的覆写这两个方法才能保证在hash容器内查找对象的正确性,同时一个好的hashCode方法能大幅提升hash容器效率。

 

军规九:【禁止循环中创建新线程,尽量使用线程池。】

 

军规十:【在进行精确计算时(例如:货币计算)避免使用float和double,浮点数计算都是不精确的,必须使用BigDecimal或将浮点数运算转换为整型运算。】

说明:浮点运算在一个范围很广的值域上提供了很好的近似,但是它不能产生精确的结果。二进制浮点对于精度计算是非常不适合的,因为它不可能将0.1——或者10的其它任何次负幂精确表示为一个长度有限的二进制小数。

敏捷测试团队,不再仅仅是在coding之后。而是和研发人员贯穿在需求分析、规格说明、自动化单元测试、自动化验收测试、静态代码分析、技术债等环节中。所以敏捷项目必定在将来效率的趋势下成为主流。
 
 
标签:  java华为开发
好文要顶  关注我  收藏该文   
0
0
 
 
 
posted @  2014-01-10 14:19  一十一. 阅读( 4146) 评论( 4编辑  收藏

Guess you like

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