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.
- Start Eclipse
- First Go to the package explorer and Right Click on your project and select PMD | Find Suspect Cut And Paste
- PMD creates a folder reports under your project and stores the result text file
- Select Window | Show View | Navigator
- In Navigator view click on Your Project and then on the reports folder
- 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