Arc-05-22- Codepro 代码复审及自动化工具

无论如何,代码在最终封箱打包前,一定要复审一下下才放心。

自己复审自己的代码,基本上是个笑话;交给同事复审自己的代码,太多沟通代价;交给上司复审自己的代码,比高考还焦虑。

交给工具软件才是正途,CodePro Analytix 被 Google 收购后,就让生活变得美好许多了。

网上 K 的一段规则:

==============================================

注意:

在用CodePro检查代码前最好在代码中用ctrl+shift+f 让eclipse自动为代码格式化一遍。

(灰色部分为暂时不执行部分)

1. Append String:

1) String literal can be replaced by a character literal:单个字符用’ ’来取代使用” ”

错误:sb.append(“a”);

正确:sb.append(‘a’);

2.Badly Located Array Declarators:

1) Badly located array declarators: 

File filesNodeB[] = ServiceAccess.getSystemSupportService().getFiles(fileName) 

数组的声明符放在类型后面不要放在变量后面

正确:File[] filesNodeB = ServiceAccess.getSystemSupportService().getFiles(fileName) 

3. Boolean Method Naming Convention:

1) Invalid method name: "isWriteAccess" should not be prefixed with 'is':

自定义的非Boolean方法名,前缀建议不要命名为is开头,is开头的方法名是为Boolean方法保留的

错误:public int isWriteAccess(Object obj)

正确:public int writeAccess(Object obj)

4. Caught Exceptions:

1) Disallowed exception caught: Exception 

catch (Exception e) 这样的异常最好能定义成更加详细的异常

5. Constant Field Naming Convention:

1) Invalid constant name: "NeInfoAttrNames" contains lowercase letters

用finall修饰的是常量 名字要全大写

错误:private final static String[] NeInfoAttrNames

正确:private final static String[] NEINFOATTRNAMES

6. Constants in Comparison :

1) Constant on right side of comparison: 在比较的时候,不变的部分应放在比较的右边。

错误:if  (this.listeners  ==  null) 

正确:if  (null  ==  this.listeners)

7. Dangling Else:

1) Dangling else clause 不规范的else写法,if,else 中只有一条语句也要加{}.

错误:else

    return null;

正确:else {

       return null;

    }

8. Define Load Factor:

1) Define the load factor of Hashtable instances:hashtable支持线程同步相对来说比较耗系统资源 在定义的时候最好把它的大小和装载因子都定义出来;

例子:filterMap = new Hashtable(32, 0.75f);

9. Declared Exceptions:

1) Disallowed exception declared: Exception

   public void init() throws Exception这样的异常最好能定义成更加详细的异常

10. Define Initial Capacity:

1) Define the initial capacity of StringBuffer instances:

在定义ArrayList,HashMap, HashSet, Hashtable, Vector, WeakHashMap, 'StringBuffer的时候最好定义初始的容量。

错误:StringBuffer sb = new StringBuffer();

正确:StringBuffer sb = new StringBuffer(32);

11. Document Closing Braces:

1)  Undocumented closeing brace:在if ,else等语句结束后应该在”}”后面加 // end if

   正确:if (null == this.listeners) {

    return;

   } // end if

12. Empty Catch Clause:

1) Empty catch clause for exception 空的chatch语句

13. Exlicit“this” Usage:

1) The keyword “this” is unnecessary:  this 在语句中是多余的

14. Field Javadoc Conventions:

1) Missing Javadoc comment for field:缺少JAVA文档注释

15. Hiding Inherited Fields:

1) Field hides field inherited from RuntimeException:

    private static final long serialVersionUID = -2945178819756838045L; 直接删掉这句

16. Import Order:

1) Import out of order: 从未用到导入的类

17. Instance Field Naming Convention:

1)  Invalid field name: " _ _Table" starts with an underscore 错误的命名

   错误:private Hashtable _ _Table = new Hashtable(200);

   正确:private Hashtable _Table = new Hashtable(200);

18. Method Javadoc Conventions:

1)  Empty @return tag in getValues 在getValues方法的注释中@return标记的内容为空

2)  Missing Javadoc comment for method:缺少方法的注释

19. Missing Block:

1) Missing block in if clause:if 语句缺少 {}

错误:if(null == item)

   return 0;

正确:if(null == item){

   return 0;

  }

20. Modifier Order:

1) Incorrect modifier order: "static" should appear before "final": static应放在final的前面

错误:private final static String SViewName = "SnmpMocView";

正确:private static final String SViewName = "SnmpMocView";

21. Platform Specific Line Separator:

1) Use of a platform specific line separator:

并不是所有系统都支持“\n”使用System.getProperty("line.separator") 来取代它

22. Questionable Assignment:

1)  Questionable method parameter assignment : 可疑的参数分配

23. Statement Creation:

1) Invalid SQL statement creation:创建createStatement()应该用预编译语句

错误:stm = con.createStatement();

正确:stm = con.prepareStatement();

24. Synchronized Method:

1) Use of synchronized modifier: synchronized:修饰苻不要放在方法名前面 容易让人忽略修饰苻的存在,最好是放在要修饰的语句快中。

错误:public synchronized String getName() {

      ...

  }

25. Static Field Naming Convention:

1) Invalid field name: "segService" starts with lowercase letters。静态变量名第一个字母为大写

错误:private static SnmpExtGuiService segService;

正确:private static SnmpExtGuiService SegService;

26. Return Boolean Expression Value:

1) An if-statement always return true or false:

错误:if (index >= 0)

  {

   return true;

  } else

  {

   return false;

  }

正确:return index >= 0;

27. Questionable Name:

1)  Short name found: 命名太短

28. Type Javadoc Conventions:

1) Missing @version tag for type BaseSnmpSyntag:在注释中缺少@version 标记

2) Missing @author tag for type :在注释中缺少@author标记

29. Unnecessary Exceptions:

1)  Unnecessary declaration of Exception:不必要的异常

30. Unnecessary Import Declarations:

1)  unnecessary Import 多余的Import语句

31. Use equals() Rather Than ==:

1) Should not compare values using the equals (==) operator 

比较值的时候用“==”不是总是准确的(根据具体情况来执行equals()方法效率没==高)

错误:if(cls = = G828.class)

正确:if(cls.equals(G828.class))

32. Unnecessary Return Statement Parentheses:

1) Unnecessary parentheses around the expression in a return statement: 

在return语句中没有必要的”()”

错误:return (obj != null);

正确:return obj != null;

33. Variable Usage:

1)  Variable assigned to but not referenced:从没读取过的变量(建议小心删除)

34. Use Compound Assignment:

1)  Use compound assignment: 

错误:Pos = Pos + 1;

正确: Pos += 1;

这样写可以使代码简单化

猜你喜欢

转载自cnmqw.iteye.com/blog/1405225