How to Use PMD(Static Code Analyzer)

What is PMD :

 

PMD is a static rule-set based Java source code analyzer that identifies potential problems like:

·       Possible bugs—Empty try/catch/finally/switch blocks.

·       Dead code—Unused local variablesparameters and private methods.

·       Empty if/while statements.

·       Overcomplicated expressions—Unnecessary if statements, for loops that could be while loops.

·       Suboptimal code—Wasteful String/StringBuffer usage.

·       Classes with high Cyclomatic Complexity measurements.

·       Duplicate code—Copied/pasted code can mean copied/pasted bugs, and decreases maintainability.

 

 

Steps for Installing PMD in Eclipse :-

 

1.     Go to EclipseMarket place

2.     Search for PMD

3.     Click on install and Restart your Eclipse

4 . Now Click on any Existing project in Eclipse and right click and you can see PMD as icon

4.     Click on icon you can have five Option

 

 

 

 

                                                  

 

 

 

Above picture says it all

You can Check code and check your violation of code and also you can check copy paste code and all and take a corrective action against violations.

 

 

1.

String x = "foo";

        if (x.equals(null)) { // bad form
            // doSomething();
        }

        if (x == null) { // preferred
            // doSomething();
        }

   

2.

//bad one

public static boolean isBarEqualTo(int x) {
        int bar = 5;
        if (bar == x) {
      return true;
      } else {
          return false;
          }
        }

//good one and preferred

public static boolean isBarEqualTo (int x) {
        int bar = 5;
        return bar == x; // can be replaced with this 
    }

 

SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment