首页 > 其他 > 详细

SonarLint实践总结

时间:2020-06-05 16:17:50      阅读:147      评论:0      收藏:0      [点我收藏+]

1、集合要用isEmpty()判空。

Minor code smell

Use isEmpty() to check whether the collection is empty or not.

问题代码:

技术分享图片

 

 

 Rule:

Using Collection.size() to test for emptiness works, but using Collection.isEmpty() makes the code more readable and can be more performant. 
The time complexity of any isEmpty() method implementation should be O(1) whereas some implementations of size() can be O(n). Noncompliant Code Example  if (myCollection.size() == 0) { // Noncompliant    /* ... */  }   Compliant Solution  if (myCollection.isEmpty()) {    /* ... */  }

 

2、重复的字符串要定义常量。

Critical code smell

Define a constant instead of duplicating this literal "total" 3 times.

rule:

Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
  public void run() {    
   prepare("action1"); // Noncompliant - "action1" is duplicated 3 times    execute("action1");    
   release("action1"); 
 }    

@SuppressWarning("all") // Compliant - annotations are excluded  private void method1() { /* ... */ }  
@SuppressWarning("all")  
private void method2() { /* ... */ }    
public String method3(String a) {    
  System.out.println("‘" + a + "‘"); // Compliant - literal "‘" has less than 5 characters and is excluded    
  return ""; // Compliant - literal "" has less than 5 characters and is excluded  }  
Compliant Solution
  private static final String ACTION_1 = "action1"; // Compliant    public void run() {    
  prepare(ACTION_1); // Compliant    execute(ACTION_1);   release(ACTION_1);  
  }  
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.

 

3、方法不要返回null

Major code smell

Return an empty collection instead of null.

问题代码:

技术分享图片

 

 rule:

Returning null instead of an actual array or collection forces callers of the method to explicitly test for nullity, making them more complex and less readable.
Moreover, in many cases, null is used as a synonym for empty.
Noncompliant Code Example
  public static List<Result> getResults() {
    return null; // Noncompliant
  }
    public static Result[] getResults() {
    return null; // Noncompliant
  }
    public static void main(String[] args) {
    Result[] results = getResults();
      if (results != null) { // Nullity test required to prevent NPE 
     for (Result result: results) {        /* ... */      }
    }
  }    
Compliant Solution
  public static List<Result> getResults() {
    return Collections.emptyList(); // Compliant
  }
    public static Result[] getResults() {
    return new Result[0];
  }
    public static void main(String[] args) {
    for (Result result: getResults()) {      /* ... */    }
  }  
See
CERT, MSC19-C. - For functions that return an array, prefer returning an empty array over a null value
CERT, MET55-J. - Return an empty array or collection instead of a null value for methods that return an array or collection

 

4、父类的静态成员不应该使用子类访问。

Critial code smell

Use static access with "com.alibaba.fastjson.JSON" for "parseObject".

问题代码:

技术分享图片

修改方案:

技术分享图片

 rule:

"static" base class members should not be accessed via derived types
 
Code smell
 
Critical
squid:S3252
 
In the interest of code clarity, static members of a base class should never be accessed using a derived type‘s name. Doing so is confusing and could create the illusion that two different static members exist.
Noncompliant Code Example
  class Parent {
    public static int counter;
  }
    class Child extends Parent {
    public Child() {
      Child.counter++; // Noncompliant
    }
  }  
Compliant Solution
  class Parent {
    public static int counter;
  } 
   class Child extends Parent {
    public Child() {
      Parent.counter++;
    } 
 }

 

5、Boolean包装类应该避免在表达式中使用。

Minor code smell

Use the primitive boolean expression here.

问题代码:

技术分享图片

 

 修改方案:

技术分享图片

rule:

Boxed "Boolean" should be avoided in boolean expressions
 
Code smell
 
Minor
squid:S5411
 
When boxed type java.lang.Boolean is used as an expression it will throw NullPointerException if the value is null as defined in Java Language Specification §5.1.8 Unboxing Conversion.
It is safer to avoid such conversion altogether and handle the null value explicitly.
Noncompliant Code Example
  Boolean b = getBoolean();
  if (b) { // Noncompliant, it will throw NPE when b == null
    foo();
  } else {
    bar();
  }  
Compliant Solution
  Boolean b = getBoolean();
  if (Boolean.TRUE.equals(b)) {
    foo();
  } else {
    bar(); // will be invoked for both b == false and b == null
  }  
See
* Java Language Specification §5.1.8 Unboxing Conversion

 

 

 

 

 

 

 

 

 

 

结束。

SonarLint实践总结

原文:https://www.cnblogs.com/it-deepinmind/p/13049913.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!