code-chronicles-code / leetcode-curriculum

https://code-chronicles-code.github.io/leetcode-curriculum/
MIT License
9 stars 12 forks source link

Add Java linter #184

Open miorel opened 4 months ago

miorel commented 4 months ago

For example https://github.com/diffplug/spotless

utsavatwork commented 2 months ago

@miorel, Do we need to build our own java linter or just want to integrate an external linter plugin/extension and configure it for our project?

Also, do we want live linting (that is real-time feedback during development) like sonarlint

miorel commented 2 months ago

Building our own Java linter would be a very interesting project and I'm sure we'd learn a lot from it! But for now the scope of this issue is to integrate an already existing linter.

I'm not familiar with what's the latest and greatest in Java linting, so I think the first step would be to research some of the options, and then try one. The minimum requirement is being able to run it from the command line to report issues. Editor integration would be a bonus but I don't think we should necessarily assume what editor people are using.

utsavatwork commented 2 months ago

Building our own Java linter would be a very interesting project and I'm sure we'd learn a lot from it! But for now the scope of this issue is to integrate an already existing linter.

I'm not familiar with what's the latest and greatest in Java linting, so I think the first step would be to research some of the options, and then try one. The minimum requirement is being able to run it from the command line to report issues. Editor integration would be a bonus but I don't think we should necessarily assume what editor people are using.

Got it. I'll start with the researching available options for java linters.

utsavatwork commented 2 months ago

@miorel,

I've been looking into Java linters and explored a few options besides Spotless, such as Checkstyle, SpotBugs, and PMD.

I'm thinking we can combine Spotless with either PMD / SpotBugs or Checkstyle for our setup.

Spotless will primarily handle code formatting, as its static analysis capabilities are limited (e.g. Removing unused imports and couple of others). However, it's perfect for enforcing consistent formatting, like adhering to the Google Java Style Guide. We can use ./gradlew spotlessCheck to check for any formatting violations and ./gradlew spotlessApply to automatically format the code based on the configured rules.

For in-depth static analysis, we can rely on PMD. It has a comprehensive list of rules for linting Java code. We can customize this ruleset according to our needs (see Making rulesets). When running ./gradlew pmdMain, a main.html report will be generated in the /build/reports/pmd folder, detailing any rule violations. Here's an example of what the main.html file might contain:

`PMD

PMD report

Problems found

#FileLineProblem
1 /home/utsav/leetcode-curriculum/workspaces/adventure-pack/goodies/java/src/digits_int/AP.java 32 Avoid using literals in if statements
1 /home/utsav/leetcode-curriculum/workspaces/adventure-pack/goodies/java/src/simple_iterator/SimpleIterator.java 40 Assigning an Object to null is a code smell. Consider refactoring.
1 /home/utsav/leetcode-curriculum/workspaces/adventure-pack/goodies/java/src/union_find/UnionFind.java 10 Field components has the same name as a method
1 /home/utsav/leetcode-curriculum/workspaces/adventure-pack/goodies/java/src/virtual_list/VirtualList.java 8 Field size has the same name as a method
` [SpotBugs](https://spotbugs.github.io/) also seems like a good option for static analysis, and it's highly configurable. One key difference between PMD and SpotBugs (based on research, though I’m not entirely certain of its accuracy) is that PMD focuses more on code quality (style, best practices), while SpotBugs targets bug detection in bytecode. You can find SpotBugs rulesets [here](https://spotbugs.readthedocs.io/en/stable/analysisprops.html). For Checkstyle, you can explore its available rulesets [here](https://checkstyle.sourceforge.io/checks.html). Let me know your thoughts on this approach.