DEV Community

WangGithub0
WangGithub0

Posted on

1

Adding Static Analysis tools

In the open source, using static analysis tools is a good way to help us maintain the quality of our source code. So this time I tried to add them to my ConvertTxtToHTML project. I created a CONTRIBUTING file to help other contributors fix formatting issues, spot suspicious coding constructs, and alert the common errors.

Step 1: I using the [contributing generator](https://generator.contributing.md/) to generate a general contributing md file, and add the basic install steps.

Step 2: Add a Source Code Formatter
Since my project using Java, I using google-java-format which is a program that reformats Java source code to comply with Google Java Style.

Image description

I can run it in my cmd java -jar google-java-format-1.18.1-all-deps.jar -r ./src/application/*, and it automatically format and overwrite my java file, here is what I got. There were many lines had been revised, most of them are caused by space.

Image description

Step 3: Add a Linter
At first, I wanted to add spotBugs which is popular used.

Image description

But I couldn't find how to run it in cmd in it's github README, so I read it official website, still can't find the detail install cmd. At last, I found it's doc and know I can add the textui to run the command line user interface.

Image description

After setting all of these, I can run and get the version, but I met the error "Resource not found: java/lang/Object.class". I found a same error reported before on github, but the resolution didn't work for me...
I have to search other java linter, I searched and many tools only support the IDE. at last, I tried to use pmd which has a fantastic live demo, I did according to the demo and figure it out.

Image description

At last, I got my pmd result by using ./pmd-bin-7.0.0-rc4/bin/pmd check -R quickstart.xml -d src/ConvertTxtMdToHtml.java -f text --cache pmd.cache --report-file jdk-report.txt and got the result:

Image description

Image description

I got many errors which I didn't know before. I corrected my errors:

  • Position literals first in String comparison:
    False: if (args.length == 0 || args[0].equals("-h") || args[0].equals("--help")) {
    // Code implementation
    }

    True: if (args.length == 0 || "-h".equals(args[0]) || "--help".equals(args[0])) {
    // Code implementation
    }

  • ControlStatementBraces: This statement should have braces
    False:if (fileName.endsWith(".md")) title = convertLinks(title);
    True:if (fileName.endsWith(".md")) {
    title = convertLinks(title); // convert links in MD file
    }

  • UnnecessaryLocalBeforeReturn: Consider simply returning the val
    False:String newLine = line.replaceAll("---", "<hr>");
    return newLine;

    True:return line.replaceAll("---", "<hr>");

Step 4: Editor/IDE Integration
In order to using formatter and linter while we are writing code, I integrate them with my vscode, so I created .vscode folder and add the Java formatting and linting according to the vscode doc

At last, I have a stronger ConvertTxtToHTML project now!

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay