For Contributors to my OpenSSG
Tags: java, opensource, statcisitegenerator
Published: 2022-11-05 00:33:18

Like *Prettier* and *ESLint* in *Javascript*, I needed to add Java formatting and linting tools. For formatting, I looked at *google-java-format, codestyle, and spotless*. Since I'm not using Java framework, I can only use plugin to format my code. Although *google-java-format* does not support configurability, I just chose to follow Google Java format as I believe they have most common language format standard. To use *google-java-format* in MacOS, go to `IntelliJ IDEA` -> `Preference` (Windows: `File` -> `Setting`) and search "plugin" menu. Then, find `google-java-format` using search bar. Now all you need to do is to install the plugin. I didn't have to enable it, I think it needs to be enabled in some cases. You can refer to the [documentation](https://github.com/google/google-java-format). To format your code, go to `Code` menu and select `Reformat code` or `Reformat file`. It will format your code. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jonf4uuroq7um819zl1m.png)

To lint my code I found *Spotbugs* and *SonarLint*. I tried them both in my project. Each one found different types of errors. *SpotBugs* found potential `null` cases in my code more than *SonarLint* while *SonarLink* advised me to reduce complexity. I had a bad smell that I have very complex logic that might need to be more concise. I have three logics with high number of complexity. I know it's good time to fix the smell, I just leave it in my to-do-list to fix later. So I decided to add *Spotbug* in my `CONTRIBUTING.md`. Installing *Spotbug* is same as other plugins. After installation, go to `View` -> `Tool Windows`. Then you will see `Spotbugs` menu (Follow same for `SonarLint`). Run it to see the result of linting your file and you will see the error messages on your console. In case you don't understand the error message, you can find more details on [SpotBugs bug description](https://spotbugs.readthedocs.io/en/stable/bugDescriptions.html). 

First I had to deal with `reliance on default encoding in FileWriter and FileReader`. It means that I need to specify encoding method for file read and write. So I updated my code with `InputStreamRead` and `OutputStreamWrite` as below. 

```java
File htmlFile = new File(filename);
FileOutputStream fs = new FileOutputStream(htmlFile);
var fileWriter = new OutputStreamWriter(fs, "UTF-8");
```
I added `UTF-8` in `OutputStreamWriter` otherwise it will use platform default encoding which could produce an unpredictable bug. 

Then I need to add some `null` check as per *SpotBugs* suggestions. I used `Paths.get(file).getFilename()` in many places to get the file name to add it to the link or to a part of text in html files. It's called `Possible null pointer dereference`. 

I also had `May expose internal static state by storing a mutable object` in my `DOMNode` class. This error is from a shallow copy of mutable object that could produce unintended changes in private property. So I updated my copy constructor and setter to create a new object for deep copy. 

Rest of errors are minor issues such as replacing a string in multiple places with constant variable, removing useless variable and modules, initializing variables, and adding `default` case to `switch` statement. I found it good practise to keep these error in mind while coding. It's very helpful to write a better code. 

I wish I could go back to where I started my *OpenSSG*, so that I could create a package to make things automated. I know Java syntax and methods, but more importantly I didn't know Java environment. There aren't many Java CLI tools, so I couldn't find good articles how to start Java CLI programming. I will add `pre-commit` hooks after referencing to the repo that I found.