JAVA中的基础----pmd代码规范

JAVA中的基础----pmd代码规范本文结合实例 讲诉如何写出符合 PMD 编程规范的 java 代码 阅读前需要有一个共识 优雅的代码一定是最让人喜欢的 下面附上代码规范的 pmd xml 文件的下载地址 https github com jitongming pmdForDev 目录 第一类 basic 第二类 unnecessary 第三类 braces 第四类 codesize

大家好,我是讯享网,很高兴认识大家。

本文结合实例,讲诉如何写出符合PMD编程规范的java代码。

阅读前需要有一个共识:优雅的代码一定是最让人喜欢的。

下面附上代码规范的pmd.xml文件的下载地址:https://github.com/jitongming/pmdForDev


目录

第一类:basic

第二类:unnecessary

第三类:braces

第四类:codesize

第五类:controversial

第六类:coupling

第七类:design

第八类:empty

第九类:finalizers

第十类:imports

第十一类:j2ee


讯享网

第十二类:logging-java

第十三类:naming

第十四类:optimizations

第十五类:strictexception

第十六类:strings

第十七类:unusedcode


------------------------------我是可爱的分割线-----------------------------------

第一类:basic

1)ReturnFromFinallyBlock【不要在finally代码块中使用return】

  • Avoid returning from a finally block, this can discard exceptions.
  • 当在finally块中使用return时,编译器不会再对try、catch块中的非运行时异常进行检查,JVM不会再去捕获try块、catch块中的异常,程序的输出以finally块为准,即finally块的返回值或者finally块中抛出的异常。
public void test(){ try{ //......do something }catch(Exception e){ //......do something }finally{ return "在这进行返回"; //避免在这里写return } }

讯享网

2)UnconditionalIfStatement【if语句判断条件,不能永远成立,必定是true或false】

  • Do not use "if" statements whose conditionals are always true or always false.
  • 当在if代码块执行前,会先检查判断条件是否成立,若条件必然成立,那没有必要用if语句。
讯享网public void test(){ if(0<1){ //避免以绝对成立的条件作为判断依据 //....do something } }

第二类:unnecessary

1)UnnecessaryConversionTemporary【不要浪费一个object进行基础数值的字符串转换,使用包装类直接处理】

  • Avoid the use temporary objects when converting primitives to Strings. Use the static conversion methodson the wrapper classes instead.
  • 不要申请一个临时的对象去将“基本类型数值”转换为“字符串”,直接使用包装类处理。创建一个对象的开销是比较大的,尽量不要创建不必要的对象。
String a = new Integer(x).toString();//1.避免写法:浪费了一个object String a= Integer.toString(x);//2.推荐写法

2)UnnecessaryFinalModifier【删除不必要的final修饰符】

  • When a class has the final modifier, all the methods are automatically final and do not need to betagged as such.
  • 如果class已经用final修饰过了,所有方法将自动默认为final型的,不需要再多此一举给方法加final修饰符。
讯享网public final class Test{ public final String method1(){//1.避免写法 return "test"; } public String method2(){//推荐写法 return "test"; } }

第三类:braces

1)ForLoopsMustUseBraces【for循环必须用{}包含起来】

  • Avoid using 'for' statements without using curly braces. If the code formatting or indentation is lost then it becomes difficult to separate the code being controlled from the rest.
  • for循环必须使用括号:避免在for循环时不使用{}。加上{}的好处:便于代码的理解和维护。
int a=0; for(int i=0;i<100;i++) //1.避免写法 a+=i; for(int i=0;i<100;i++) {//2.推荐写法 a+=i; } 

2)IfElseStmtsMustUseBraces【if..else..语句必须用{}包含起来】

  • Avoid using if..else statements without using surrounding braces. If the code formatting or indentation is lost then it becomes difficult to separate the code being controlled from the rest.
  • if…else…块必须使用括号:避免使用if…else…块时不使用{}。好处同上。
讯享网int a=1; int b=2; if(1==a) //1.避免写法 b=a; else b+=a; if(1==a){//2.推荐写法 b=a; }else{ b+=a; }<
小讯
上一篇 2025-02-11 08:17
下一篇 2025-03-24 07:28

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/132946.html