Saturday, May 8, 2010

How to find Cut and Paste Code in Eclipse Using PMD ?

PMD is is great tool for finding code violation and many other defects before product release( If you have not installed then follow this link How to install PMD in Eclipse ? ). One of that feature is find the repeated cut and paste code. Cut/copy/paste is very bad programming practice. Repeated (Cut&Paste) code generally indicates poor planning or team coordination. Therefore we should be refactor our code to remove that bad practice.Refactoring classes with repeating code should be given a high priority. PMD is very effective to find out such classes by scanning the code in a way similar to PMD violation checks. The number of lines of similarity (the metrics used by PMD to match code patterns) is 25 by default and can be set in PMD's Preferences page.
  1. Start Eclipse
  2. First Go to the package explorer and Right Click on your project and select PMD | Find Suspect Cut And Paste
            
                  




  3. PMD creates a folder reports under your project and stores the result text file



  4. Select Window | Show View | Navigator
  5. In Navigator view click on Your Project and then on the reports folder
  6. The report file cpd-report.txt should look like this:
    Found a 5 line (15 tokens) duplication in the following files:
    Starting at line 21 of G:\besthowtodo\workspace\HelloDynaWeb\src\com\besthowtodo\tutorials\dynaweb\DemoPMDThread.java
    Starting at line 25 of G:\besthowtodo\workspace\HelloDynaWeb\src\com\besthowtodo\tutorials\dynaweb\DemoPMDThread.java
          public void WRITE_SOMETHING_WITH_COPY_PASTE(String INPUT_PARAMETER) {
                 System.out.println(INPUT_PARAMETER);
          }
          public static void main(String[] args) {


No comments:

Post a Comment