After using these two plug-ins, my colleagues never say that my code is bad.

Colleague: Your code is not well written, it is not standardized enough.

Me: How could the code I wrote be irregular, don't talk nonsense.

So my colleague opened my IDEA, installed a plug-in, and then executed it. The specification is not standardized, let's see the report.

How can this be good, this thing has picked out so many problems for me, is it reliable or not.

The colleague walked away in a cool manner, leaving me in my seat staring at the screen in panic. I carefully checked every item in this report, and the more I read, the more I felt that the problems pointed out by this plug-in were reasonable. I was really careless, and even picked out a bug for me.

What kind of plugin is this, the review code is invincible.

This plug-in is SonarLint, and the Slogan of the official website is clean code begins in your IDE with {SonarLint}.

As a programmer, we certainly hope that the code we write is impeccable, but due to various reasons, some problems and even bugs cannot be avoided, especially for students who are new to development, and many programmers with many years of development experience will also have Some bad coding habits.

Code quality and code specification must first be determined by the programmer's own level and literacy, but to improve the level requires methods, and there are many methods, such as referring to the specifications and codes of major manufacturers, such as being led by a big boss, The rest is accumulated little by little, and some useful plug-ins can always remind us what is a good code specification and what is a good code.

SonarLint is such a useful plug-in that can help us reviewcode in real time, and even find potential problems in the code and provide solutions.

SonarLint uses static code analysis techniques to detect common errors and vulnerabilities in code. For example, it can detect null pointer references, type conversion errors, duplicate code and logic errors, etc. These are common problems, but sometimes hard to spot. Using the SonarLint plug-in, these problems can be discovered while writing the code, and they can be corrected in time, which helps to prevent these problems from affecting the stability of the application.

For example, the following piece of code does not have the condition setting to end the loop, SonarLint will give a prompt, and those with obsessive-compulsive disorder can bear it. Is the red underline here?

The SonarLint plugin helps me improve the readability of my code. Code should be easy to read and understand, which helps other developers maintain and modify the code more easily. The SonarLint plugin can detect bad code smells in your code, such as unnecessary comments, long function and variable names that are not descriptive, and more. By using the SonarLint plugin, you can gain a better understanding of how to write clear, concise, and understandable code.

hello_worldFor example, for the static variable named below final, SonarLint gives two suggestions.

  1. Because the variable has not been used, it is recommended to remove it;
  2. Static immutable variable names do not conform to the specification;

The SonarLint plugin helps me follow best practices and standards. Writing code that adheres to standards and best practices ensures application quality and reliability. The SonarLint plugin can detect rule violations in your code, such as unsafe type conversions, unused variables and methods, incorrect exception handling, and more. By using the SonarLint plug-in, you can learn how to write code that complies with best practices and standards, and make your code more robust and reliable.

For example, the following exception throwing method is thrown directly Exception, and SonarLint recommends not to use it Exception, but to customize an exception. The custom exception may let people intuitively see what the exception is for, rather than the Exceptionbasic type of export and transmission.

Install SonarLint

You can directly open IDEA Settings -> Plugins, MarketPlacesearch in it SonarLint, and install it directly.

You can also download directly from the official website, open the page https://www.sonarsource.com/products/sonarlint/, you can see the download icons of multiple languages ​​and multiple development tools on the page, click the button below EXPLOREto go to the download page to download. Although we only use it in IDEA, it supports Java, not only IDEA, but also many languages ​​such as Python and PHP, as well as many IDEs such as Visual Studio and VS Code.

Use in IDEA

After the SonarLint plug-in is installed, the real-time analysis function is enabled by default, just like the smart prompt function. As you clack and type on the keyboard, the plug-in SonarLintwill silently analyze. , yellow wavy line prompts.

Of course, you can also right-click in a file, or right-click in the project root directory, and click in the pop-up menu Analyze with SonarLintto analyze the current file or the entire project.

After the analysis, an analysis report will be generated.

On the left are the results of the analysis of the individual files, and on the right are suggestions and examples of modifications to the problem.

SonarLint divides problems into three types

type illustrate
Bug A bug in the code that affects the running of the program
Vulnerability Vulnerabilities, which may be used as attack entry points
Code smell Code meaning, which may affect code maintainability

Problems are divided into 5 categories according to severity

severity illustrate
BLOCKER It has affected the normal operation of the program, and it will not work if it is not changed.
CRITICAL It may affect the operation of the program and may threaten the security of the program. Generally, it cannot be changed
MAJOR Code quality issues, but serious
MINOR Same code quality issue, but less severe
INFO some friendly advice

SonarQube

SonarLint is a plug-in for analysis at the IDE level. In addition, SonarQubeit can also use functions. It is displayed in the form of a web, which can provide a web visualization effect for the entire development team's projects. And it can be integrated with deployment tools such as CI\CD to provide code analysis before release.

SonarQubeIt is a Java project. You can download the project from the official website and start it locally, or you can start it in the way of docker. The global configuration can then be configured in IDEA SonarQube.

You can also configure a project separately in SonarQube web. After creating the project, execute the mvn command directly in the project to be analyzed to generate an analysis report for the corresponding project, and then view it in SonarQube web.

5

For the vast majority of developers and development teams, SonarQube is actually unnecessary, as long as each of us solves the suggestions given by SonarLint in the IDE, of course the final code quality is up to standard.

Ali Java protocol plug-in

Every development team has code specifications within the team, such as variable naming, comment format, and how to use various class libraries. Ali has been updating the Java version of the Alibaba Developer Manual. You must have heard of the Taishan version and the ultimate version. If the developers can abide by the regulations in it, I am afraid that others will not be able to diss your code. up.

Corresponding to the language-level specifications of this development manual, Ali also released an IDEA plug-in called Alibaba Java Coding Guidelines, which can be downloaded directly from the plug-in store.

hello_worldFor example, for the variable name mentioned above , the plug-in directly prompts "Correct to an uppercase pattern separated by underscores".

Another example is some hints on comments, it is not recommended to use end-of-line comments.

image-20230314165107639

In addition, for example, for the use of thread pool, there are content recommended according to the specification. It is recommended to define parameters such as the number of core threads and the maximum number of threads by yourself, and it is not recommended to use Excutorstool classes.

​ With these two plug-ins, who can say that my code is not standardized.

If you are interested in Python and want to get a higher salary by learning Python, then the following set of Python learning materials must be useful to you!

Materials include: Python installation package + activation code, Python web development, Python crawler, Python data analysis, artificial intelligence, machine learning and other learning tutorials. Even beginners with 0 basics can understand and understand. Follow the tutorial and take you to learn Python systematically from zero basics!

1. Learning routes in all directions of Python

The route of all directions in Python is to organize the commonly used technical points of Python to form a summary of knowledge points in various fields. Its usefulness lies in that you can find corresponding learning resources according to the above knowledge points to ensure that you learn more comprehensively.
insert image description here
2. Python learning software

If a worker wants to do a good job, he must first sharpen his tools. The commonly used development software for learning Python is here!
insert image description here
3. Python introductory learning video

There are also many learning videos suitable for getting started with 0 basics. With these videos, you can easily get started with Python~insert image description here

4. Python exercises

After each video lesson, there are corresponding practice questions, you can test the learning results haha!
insert image description here

Five, Python actual combat case

Optical theory is useless. You have to learn to type codes along with it, and then you can apply what you have learned in practice. At this time, you can learn from some practical cases. This information is also included~insert image description here

6. Python interview materials

After we have learned Python, we can go out and find a job with the skills! The following interview questions are all from top Internet companies such as Ali, Tencent, Byte, etc., and some Ali bosses have given authoritative answers. After reading this set of interview materials, I believe everyone can find a satisfactory job.
insert image description here
insert image description here
7. Information collection

The full set of learning materials for the above-mentioned full version of Python has been uploaded to the CSDN official website. Those who need it can scan the QR code of the CSDN official certification below on WeChat to receive it for free.

Guess you like

Origin blog.csdn.net/pythonhy/article/details/132066465