The role of sonar in Java code check

Source: Internet
Author: User

As a very practical code quality check tool, sonar can quantify the entire project in various dimensions, such as the Code test coverage rate, code cohesion indicators, Code complexity, code repetition check, dependency between packages, and code conflict, You can intuitively view the data through the icon, another article on installing and using sonar in Benbo (using the code Quality Check Tool sonar in combination with Hudson and Maven) is described in detail, this article describes the potential code risks that sonar can detect.

  • Avoid decimal literals in big decimal constructor should avoid using decimal (0.5), because this is incorrect and inaccurate
  • Avoid rethrowing exception should not be thrown again in the catch statement
  • Avoid throwing NULL pointer exception should not throw a null pointer exception
  • Boolean instantiation you do not need to instantiate a Boolean variable. You only need to = Boolean. True or = Boolean. flase
  • Close resource has unclosed resources. Use the finally statement block to ensure security.
  • Collapsible if statements if multiple if statements appear consecutively, you can use the short-circuit condition if (A & B &...) to improve efficiency.
  • Compare objects with equals compare two objects using equal instead of equal
  • Cyclomatic complexity generally speaking, this method or class is too long and needs to be decomposed.
  • Dont import Java Lang java. Lang is introduced by default and does not need to be explicitly introduced
  • Empty finalizer, empty Finally block, empty if stmt, empty statement, empty static initializer, empty switch statements, empty synchronized block, empty try block, empty while stmt: this is a very common code problem. If, switch, finally, try and so on are empty, and it is very likely that the coders forget to handle it.
  • Equals hash code method equal is rewritten, but hashcode is not rewritten.
  • Equals null and null use = instead of equals
  • If else should ts must use braces or if should ts must use braces encoding, if or else does not write braces, it is easy to see problems
  • Magic number refers to a number that appears in the code without reason, but does not know its meaning. It is very common.
  • Naming-*** the naming of classes, methods, and variables is not standard.
  • Replace vector with list should use list instead of vector to improve efficiency
  • Security-array is stored directly is usually used by parameters of the array type. If clone is not used, the method directly changes the object referred to by the reference.
  • Singular field variables used only in a method should not be declared as instance variables.
  • Unconditional if statement is usually true if statements are always true or false
  • Unnecessary case change use equalsignorecase () more efficient
  • Unused private field or unused formal parameter or unused local variable or unused private method variables, parameters, and methods that are not useful
  • Useless operation on immutable modifies a value in the method body and intends to pass it out. However, the operation is actually a new object or value, for example, changing a parameter of the basic type.

The above are some common hidden danger types, and many others are not written.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.