Alibaba Java development specification plug-in p3c detailed tutorial and experience

Alibaba Java Development Manual

Before entering the main topic to introduce this plug-in, let's talk about the "Alibaba Java Development Manual". At the beginning of 2017, the official Java code specification standard manual of Ali released for the first time can be said to have caused a wave of code specification for the whole people (IT industry). I believe that many friends in the industry are familiar with this manual. After all, there is a "big factory halo", and it is difficult to keep a low profile. The vision of this manual is:

"Unified standardization standards will help improve the standardization level of industry coding, help industry personnel improve development quality and efficiency, and greatly reduce code maintenance costs"

Final version

Three versions have been changed so far. On September 25, 2017, the "Alibaba Java Development Manual (Ultimate Edition)" was officially released, and it was also the last PDF version officially released by Alibaba. The latest version download link is here .

Plugin introduction

Since the first version of "Alibaba Java Development Manual", it has attracted much attention in the industry. I believe that many people have a certain understanding of the rules. Many people even hope to implement it in their own teams as soon as possible, so that everyone has a Set of common development guidelines. However, how to better comply with the rules and develop our system according to the manual is not so easy. In order to make it more convenient and fast for developers to promote and implement the specification, Alibaba has developed a system based on the content of the manual. Automated IDE detection plug-ins (IDEA, Eclipse), after scanning the code, the plug-in will display the code that does not conform to the "Manual" in the three levels of Blocker/Critical/Major. Even on IDEA, it also provides an inspection mechanism based on Real-time detection function, can quickly find the problem while writing code. For historical code, some rules implement the function of batch one-click repair, improve code quality, and improve team R&D efficiency.

The code is open source, GitHub: https://github.com/alibaba/p3c

Let's experience it!

Plug-in simple tutorial (based on IDEA)

Search plugin

Open IDEA, select Preferences - Plugins - Browse repositories , enter "alibaba" in the input box, and you can see that "Alibaba Java Coding Guidelines" is in the returned result.
search
When I started thinking about this article a week ago, it had 16340 downloads, it should be more now, like it.
install

Install the plugin

Click the "install" button in the plugin details and follow the prompts to complete the installation. After the installation is complete, you need to restart IDEA.

Functional experience

After the installation is complete, you need to restart IDEA. After restarting, you can see its function buttons in the menu bar. There are three ways to scan the code:

  • Right-click on the project directory will also appear these two function buttons, click the green button to start scanning the code, or right-click on the project directory will also appear the detection function button.
    check
  • If you don't want to scan all, but only scan the currently edited file, right-clicking in the current file panel will also appear this function button.
    check2
  • The two methods mentioned above are manual detection. The plug-in also provides the function of real-time monitoring. This function is enabled by default. Once enabled, it will be monitored in real time when you write code. Once there is a statement that does not conform to the specification, an error message will appear. .
    check3
    Due to the irregularity of curly brackets, ifthere is an error red line below.
    check4
    There is a quick fix shortcut key in idea: option/alt+enter, and the curly brackets can be automatically added after clicking.

After scanning the code, the code that does not conform to the specification will be displayed in the lower panel according to the three levels of Blocker/Critical/Major, as shown in the figure:
check-result
The left side is the scanned code that does not conform to the specification. Click in order to see how many lines of code it is. The specification problem and which specification problem arises, and the right side is the detailed description of the specification and the example code.
critical
Not only that, there is also a quick fix button on the right side, you can directly correct the code after clicking, but not all problems have this button, and some problems still need to be modified manually.

Supported version

IDEA official warehouse: minimum supported version 14.1.7, JDK1.7+
Eclipse version plugin: support 4.2 (Juno, JDK1.8+) and above

Check for updates

You can check the new version of the plugin through Help >> Check for Udates

Plugin uninstall

Click "uninstall" in the plugin panel.

experience feelings

I did not organize this article when the plugin first came out, but decided to write this article after using it for a period of time. I have been using it for more than a week, and the overall feeling is quite satisfactory. It may be the requirements and expectations at the beginning. It's a bit high, but after using it, I feel that it is the same. If it is 100 points, I will give it 80 points (personal opinion, don't worry too much), of which 10 points are because of Chinese, hahahaha.

Let’s first sort out the more obvious advantages:

  • Chinese prompts, and detailed prompts and explanations are given in accordance with the "Alibaba Java Development Manual", which seems really comfortable.
  • Being able to detect some problems that you don't usually care about can improve your attention to the minutiae during the development and coding process.
  • Quick fix, after detecting the problem, you can click the button to complete the modification of the code.
  • It basically meets the needs of code specification detection.

There are also disadvantages:

  • Only IDEA and Eclipse are supported, and there are version requirements.
  • There are typos (minor issues).
  • There are bugs.

At present, this plugin is still in the first public version, so there must be some small problems or bugs, which should be fixed gradually in the future, but it has basically met the needs of code review, and it is quite satisfactory to use. However, some statements and problems that are prone to null pointer exceptions do not seem to be detected, but only meet the basic code quality inspection. If the requirements are stricter, you still need to use a more professional tool such as SonarQube for code quality inspection and analysis. , In comparison, p3c is more basic, and SonarQube is more complete, but this comparison is also asymmetrical. At present, the two are not tools of the same order of magnitude.

A few words more, the specification used by this plugin is the set of specifications published by Ali, which must be slightly different from other specifications. I remember that when Ali's specification was announced, some people even used "eight-legged text" in the comment area. To describe it, I don’t think it’s so scary. Norm, if you comply with it, it’s the norm. If you don’t comply with it, what is it? Each family has their own regulations, and they can’t be completely unified. Just choose the one that suits you. There's no need to go to the gang and go online, so I'll stop talking about it.

Summarize

Is it possible to use this plugin to make our code less buggy? Can we step on some pits less? The answer is definitely not, the plug-in only detects and prompts, the real coder is still himself, so don’t put the cart before the horse, this is just a tool, this kind of idea must not exist, the most important thing to write robust code is the coder himself .

First published on my personal blog , October 22, 2017.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325782823&siteId=291194637