An overview of the

Today’s introduction to plug-ins is mostly about coding specifications. Ambitious programmers, often have code cleanliness, to minimize the “bad smell” of code.

There are many types of code static checking, such as cyclomatic complexity, repetition rate, and so on. The industry provides many plug-ins for static checking to identify non-compliant code and help improve project quality. One of the better known products is SonarQube, which provides an “access control” platform that integrates many static inspection checks. Next time, I will introduce the construction of this platform.

This article introduces a good plugin for Java static checking in IDEA:

  • Alibaba Java Coding Guidelines provide plug-ins based on their Java specification
  • Checkstyle-idea Checks whether the format of the code conforms to the specification
  • Findbug-idea checks your code for common bugs

Alibaba Java Coding Guidelines

Alibaba Java coding guide plug-in support.

Let’s start with Alibaba’s P3C project. Its Github page is github.com/alibaba/p3c. Alibaba has previously opened source a Java development manual. Manual from the programming protocol, exception log, unit test, MySQL database, engineering structure, design protocol and other perspectives, introduced Ali’s Java development specifications, this for Java novice help is very big, can learn a lot of things. There are some pits where older drivers may also roll over.

When developers do Coding, they may forget the specification, and the code will still taste bad. This is where the Alibaba Java Coding Guidelines plug-in comes in handy. It checks your code against the Java development specification above, notifies you of code that doesn’t meet the specification, and suggests changes. Ali, as a major Java manufacturer in China, based on the experience of thousands of engineers, I believe that the specification suggestions given are relatively reliable.

Last chestnut:

  String str = "hello";
  for (int i = 0; i < 100; i++) {
      str = str + "world!";
  }
Copy the code

Without looking down, try to analyze where this code can be optimized.

In fact, the results of keystroke scanning not only have this problem, but also the “magic number” problem. The usage of the plug-in is shown in the screenshot below.

In addition, it is recommended to check the check item button provided by IDEA when submitting code, so that if there is any non-compliant code to submit, it will remind you to modify:

CheckStyle-IDEA

Project homepage: github.com/jshiell/che…

Once installed, go to Settings and check the default check criteria you want to use:

Large factories often have their own language specifications, which can be imported and selected:

Right-click the menu and select Check Current File to Check whether the Current File conforms to the coding specification:

Inspection Results:

If there are a lot of non-conforming writing methods, rectification will be very painful, at this time how to do? There’s a feature called Reformat Code, and the shortcut is:

  • Mac:Command+Option+L
  • Win:Ctrl+Shift+L

If you want to format it according to the rules you specify, you can customize it as follows:

FindBugs-IDEA

FindBugs is an old Java static checking plug-in. Its function is similar to that of Ali P3C plug-in, but it has a long history, a little international. It can also scan code for possible bugs and make recommendations.

To start FindBugs, from the right-click menu, FindBugs provides several options:

  • Analyze Current File: Checks the current file
  • Analyze Class uner Cursor: Checks the class at the cursor
  • Analyze Package Files: Check the package file
  • Analyze Modul Files: Check the Module file
  • Analyze Project Files: Check the project files
  • Analyze Scope Files: checks files in the specified range
  • Analyze All Modified Files: Check all modified files
  • Analyze changelist files: Checks the files in the change list

Inspection Results:

The inspection results are classified as follows:

  • Bad practice: bad practices, code that violates accepted best practice standards;
  • Malicious code vulnerbility: malicious code vulnerabilities;
  • Correctness: may be incorrect, such as a bad cast;
  • Performance: Potential performance problems;
  • Security: security;
  • Dodgy code: Bad code, which the FindBugs team believes has a high probability of causing bugs;
  • Experimental: the experiment;
  • Multithreaded correctness: focuses on synchronization and multithreading issues;
  • InternationalizationInternationalization of:

Scan results how to read, website have special one page introduction FindBugs Bug Description findbugs.sourceforge.net/bugDescript…

In addition to the above right-click menu to start the check, you can also start from the corresponding folder right-click menu:

As for the severity of the check, it is actually adjustable. It is suggested to change it to low, so that code fragments with potential bugs can be scanned as much as possible:

conclusion

These are some of the most commonly used static code checking tools in the workplace today, and while they don’t necessarily translate into good code, they do make it easier for you to step on the shoulders of others. For example, you can follow ali’s check key and check it against their Java development manual to see why they have such a protocol. Aren’t high-rise buildings built one brick at a time? The foundation of small knowledge point hit solid, ability go higher!

Due to domestic network problems, I have uploaded the latest version of “Alibaba Java Development Manual (Taishan version).pdf” to the cloud disk in advance. If you need it, you can get the download link by replying to taishan version.

It’s a

Last week I wrote an article to share in the wechat group, some of the group members pointed out that the title had a typo, it was really a bit embarrassing! As a matter of fact, I’m pretty casual with my writing, and I’ve only recently started Posting at least one post a week. But if you write it, you have to be as good as you can be. Take it as a warning, we should be serious in the future!

The current content is mainly development environment, efficiency tools, etc. I have been interested in this aspect of the article, so there has been relevant accumulation before. In the future, I hope I can share some reading summaries through reading. I also hope that the students concerned can leave more comments on the background and give your suggestions. Thank you

Life, more than toss! Pay attention to “Coder Magic Court”, wish you Niubilitiy!

reference

  • FindBugs: juejin.cn/post/684490…