Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Genne23v
Genne23v

Posted on

     

For Contributors to my OpenSSG

LikePrettier andESLint inJavascript, I needed to add Java formatting and linting tools. For formatting, I looked atgoogle-java-format, codestyle, and spotless. Since I'm not using Java framework, I can only use plugin to format my code. Althoughgoogle-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 usegoogle-java-format in MacOS, go toIntelliJ IDEA ->Preference (Windows:File ->Setting) and search "plugin" menu. Then, findgoogle-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 thedocumentation. To format your code, go toCode menu and selectReformat code orReformat file. It will format your code.

Image description

To lint my code I foundSpotbugs andSonarLint. I tried them both in my project. Each one found different types of errors.SpotBugs found potentialnull cases in my code more thanSonarLint whileSonarLink 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 addSpotbug in myCONTRIBUTING.md. InstallingSpotbug is same as other plugins. After installation, go toView ->Tool Windows. Then you will seeSpotbugs menu (Follow same forSonarLint). 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 onSpotBugs bug description.

First I had to deal withreliance 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 withInputStreamRead andOutputStreamWrite as below.

FilehtmlFile=newFile(filename);FileOutputStreamfs=newFileOutputStream(htmlFile);varfileWriter=newOutputStreamWriter(fs,"UTF-8");
Enter fullscreen modeExit fullscreen mode

I addedUTF-8 inOutputStreamWriter otherwise it will use platform default encoding which could produce an unpredictable bug.

Then I need to add somenull check as perSpotBugs suggestions. I usedPaths.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 calledPossible null pointer dereference.

I also hadMay expose internal static state by storing a mutable object in myDOMNode 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 addingdefault case toswitch 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 myOpenSSG, 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 addpre-commit hooks after referencing to the repo that I found.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Toronto, ON
  • Education
    Seneca College
  • Joined

More fromGenne23v

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp