星期六, 7月 21, 2007

PMD 備忘記


一般在編寫程式的時侯,經驗再豐富的編程員都會範上一些編程的陷阱,
因此產生俗稱為 "smell code" 的代碼.

PMD 是一套不錯的代碼檢查工具,專門檢測 "smell code",
並能找出以下潛在的問題:
Possible bugs - empty try/catch/finally/switch statements
Dead code - unused local variables, parameters and private methods
Suboptimal code - wasteful String/StringBuffer usage
Overcomplicated expressions - unnecessary if statements, for loops that could be while loops
Duplicate code - copied/pasted code means copied/pasted bugs

PMD 能集成於 JDeveloper, Eclipse, JEdit, Jbuilder, BlueJ, CodeGuide,
NetBeans/Sun Java Studio Enterprise/Creator, Intellij IDEA, TextPad,
Maven, Ant, Gel, JCreator, Emacs.

由於太多IDE選擇, 所以本人選擇最熟悉的 Eclipse 作為本次備忘記.


開始備忘記:

下載 pmd-eclipse-site-3.2.2.zip
http://sourceforge.net/project/showfiles.php?group_id=56262

進入 Eclipse:
Eclipse:Help -> Software Updates -> Find and Install -> Search for new feature to install
點選 New Archived Site 瀏覽並選擇 pmd-eclipse-site-3.2.2.zip
按 Finish 開始安裝

開始檢查 smell code:
Eclipse 裡點選需要檢查 smello code 的 project 或 java 檔案.
右鍵選擇 PMD -> Check Code With PMD
( 或 )
右鍵點選 project -> Properties -> PMD
點選 Enable PMD
按 OK 後如下圖所示



從 Problems 裡可以看到所有 smell code.
然後便可更正有問題的 smell code, 養成良好的 coding 習慣.

參考資料:
http://pmd.sourceforge.net/

http://pmd.sourceforge.net/integrations.html#eclipse

沒有留言: