Name
RemoveCommentedCodeRule
Severity
Major
Message
及时清理再也不使用的代码段或配置信息。
Description
说明:对于垃圾代码或过期配置,坚定清理干净,避免程序过分臃肿,代码冗余。
Examples
Positive example: For codes which are temporarily removed and likely to be reused, use /// to add a reasonable note.publicstaticvoidhello(){/// Business is stopped temporarily by the owner.// Business business = new Business();// business.active();
System.out.println("it's finished");}
Name
TestClassShouldEndWithTestNamingRule
Severity
Major
Message
测试类命名以它要测试的类的名称开始,以Test结尾
Examples
publicclassDemoTest{}
12.不容许任何魔法值(即未经定义的常量)直接出如今代码中。
Name
UndefineMagicConstantRule
Severity
Major
Message
不容许任何魔法值(即未经定义的常量)直接出如今代码中。
Examples
Negative example://Magic values, except for predefined, are forbidden in coding.if(key.equals("Id#taobao_1")){//...}
Positive example:
String KEY_PRE ="Id#taobao_1";if(KEY_PRE.equals(key)){//...}
Name
AvoidNegationOperatorRule
Severity
Major
Message
避免采用取反逻辑运算符。
Description
说明: 取反逻辑不利于快速理解,而且取反逻辑写法必然存在对应的正向逻辑写法。
Examples
Negative example:// Use `if (!(x >= 628))` to represent that x is less than 628.if(!(x >=628)){// ...}
Positive example:// Use `if (x < 628)` to represent that x is less than 628.if(x <628)){// ...}
Name
CommentsMustBeJavadocFormatRule
Severity
Major
Message
类、类属性、类方法的注释必须使用javadoc规范,使用/**内容*/格式,不得使用//xxx方式和/*xxx*/方式。
Description
说明:在IDE编辑窗口中,javadoc方式会提示相关注释,生成javadoc能够正确输出相应注释;在IDE中,工程调用方法时,不进入方法便可悬浮提示方法、参数、返回值的意义,提升阅读效率。
Examples
/** * * XXX class function description. * */publicclassXxClassimplementsSerializable{privatestaticfinallong serialVersionUID =113323427779853001L;/** * id */private Long id;/** * title */private String title;/** * find by id * * @param ruleId rule id * @param page start from 1 * @return Result<Xxxx> */public Result<Xxxx>funcA(Long ruleId, Integer page){return null;}}
18.循环体内,字符串的联接方式,使用StringBuilder的append方法进行扩展。
Name
StringConcatRule
Severity
Major
Message
循环体内,字符串的联接方式,使用StringBuilder的append方法进行扩展。
Description
说明:反编译出的字节码文件显示每次循环都会new出一个StringBuilder对象,而后进行append操做,最后经过toString方法返回String对象,形成内存资源浪费。
Examples
Negative example:
String result;for(String string : tagNameList){
result = result + string;}
Positive example:
StringBuilder stringBuilder =newStringBuilder();for(String string : tagNameList){
stringBuilder.append(string);}
String result = stringBuilder.toString();
19.关于基本数据类型与包装数据类型的使用标准
Name
PojoMustUsePrimitiveFieldRule
Severity
Major
Message
关于基本数据类型与包装数据类型的使用标准以下:
1) 全部的POJO类属性必须使用包装数据类型。
2) RPC方法的返回值和参数必须使用包装数据类型。
3) 全部的局部变量推荐使用基本数据类型。
Description
说明:POJO类属性没有初值是提醒使用者在须要使用时,必须本身显式地进行赋值,任何NPE问题,或者入库检查,都由使用者来保证。
Examples
publicclassDemoDO{
String str;
Integer a;}
Name
PackageNamingRule
Severity
Major
Message
包名统一使用小写,点分隔符之间有且仅有一个天然语义的英语单词。包名统一使用单数形式,可是类名若是有复数含义,类名可使用复数形式
Examples
com.alibaba.mpp.util / com.taobao.tddl.domain.dto
Name
AvoidApacheBeanUtilsCopyRule
Severity
Blocker
Message
避免用Apache Beanutils进行属性的copy。
Description
说明:Apache BeanUtils性能较差,可使用其余方案好比Spring BeanUtils, Cglib BeanCopier。
Examples
TestObject a =newTestObject();
TestObject b =newTestObject();
a.setX(b.getX());
a.setY(b.getY());
29.日期格式化字符串[%s]使用错误,应注意使用小写‘y’表示当天所在的年,大写‘Y’表明week in which year。
Name
UseRightCaseForDateFormatRule
Severity
Critical
Message
日期格式化字符串[%s]使用错误,应注意使用小写‘y’表示当天所在的年,大写‘Y’表明week in which year。
Description
日期格式化时,yyyy表示当天所在的年,而大写的YYYY表明是week in which year(JDK7以后引入的概念),意思是当天所在的周属于的年份,一周从周日开始,周六结束,只要本周跨年,返回的YYYY就是下一年。
Examples
Negative example:
SimpleDateFormat format =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Positive example:
SimpleDateFormat format =newSimpleDateFormat("YYYY-mm-dd HH:mm:ss");
30.返回类型为基本数据类型,return包装数据类型的对象时,自动拆箱有可能产生NPE
Name
MethodReturnWrapperTypeRule
Severity
Major
Message
返回类型为基本数据类型,return包装数据类型的对象时,自动拆箱有可能产生NPE
Examples
publicintmethod(){
Integer a = null;return a;}
Name
AvoidCommentBehindStatementRule
Severity
Major
Message
方法内部单行注释,在被注释语句上方另起一行,使用//注释。方法内部多行注释使用/* */注释。注意与代码对齐。
Examples
publicvoidmethod(){// Put single line comment above code. (Note: align '//' comment with code)int a =3;/** * Some description about follow code. (Note: align '/**' comment with code) */int b =4;}
Name
AvoidMissUseOfMathRandomRule
Severity
Major
Message
注意 Math.random() 这个方法返回是double类型,注意取值的范围[0,1)(可以取到零值,注意除零异常),若是想获取整数类型的随机数,不要将x放大10的若干倍而后取整,直接使用Random对象的nextInt或者nextLong方法。
Examples
Negative example:
Long randomLong =(long)(Math.random()*10);
Positive example:
Long randomLong =newRandom().nextLong();
Name
AvoidConcurrentCompetitionRandomRule
Severity
Major
Message
避免Random实例被多线程使用,虽然共享该实例是线程安全的,但会因竞争同一seed 致使的性能降低。说明:Random实例包括java.util.Random 的实例或者 Math.random()的方式。
Examples
Positive example 1:
/** * @author caikang * @date 2017/04/07 */publicclassRandomInThreadextendsThread{private Random random =newRandom();@Overridepublicvoidrun(){long t = random.nextLong();}}
Positive example 2:
/** * @author caikang * @date 2017/04/07 */publicclassRandomInThreadextendsThread{private Random random = ThreadLocalRandom.current();@Overridepublicvoidrun(){long t = random.nextLong();}}
Name
UpperEllRule
Severity
Blocker
Message
long或者Long初始赋值时,必须使用大写的L,不能是小写的l,小写容易跟数字1混淆,形成误解。
Examples
Negative example://It is hard to tell whether it is number 11 or Long 1.
Long warn =1l;
Positive example:
Long notwarn =1L;
46.事务场景中,抛出异常被catch后,若是须要回滚,必定要手动回滚事务。
Name
TransactionMustHaveRollbackRule
Severity
Major
Message
事务场景中,抛出异常被catch后,若是须要回滚,必定要手动回滚事务。
Examples
Positive example 1:
/** * @author caikang * @date 2017/04/07 */@Service@Transactional(rollbackFor = Exception.class)publicclassUserServiceImplimplementsUserService{@Overridepublicvoidsave(User user){//some code//db operation}}
Positive example 2:
/** * @author caikang * @date 2017/04/07 */@ServicepublicclassUserServiceImplimplementsUserService{@Override@Transactional(rollbackFor = Exception.class)publicvoidsave(User user){//some code//db operation}}
Positive example 3:
/** * @author caikang * @date 2017/04/07 */@ServicepublicclassUserServiceImplimplementsUserService{@Autowiredprivate DataSourceTransactionManager transactionManager;@Override@Transactionalpublicvoidsave(User user){
DefaultTransactionDefinition def =newDefaultTransactionDefinition();// explicitly setting the transaction name is something that can only be done programmatically
def.setName("SomeTxName");
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = transactionManager.getTransaction(def);try{// execute your business logic here//db operation}catch(Exception ex){
transactionManager.rollback(status);throw ex;}}}
47.异常类命名使用Exception结尾
Name
ExceptionClassShouldEndWithExceptionRule
Severity
Critical
Message
异常类命名使用Exception结尾
Examples
publicclassCacheDemoExceptionextendsException{}