sonar Code Quality Check Problem Analysis

Mutable fields should not be "public static"

Variable fields should not be public static

Non-conformity

public interface MyInterface {
  public static String [] strings; // Noncompliant
}

public class A {
  public static String [] strings1 = {"first","second"};  // Noncompliant
  public static String [] strings2 = {"first","second"};  // Noncompliant
  public static List<String> strings3 = new ArrayList<>();  // Noncompliant
  // ...
}

Compliance

private static final String [] COLUMN_NAMES = new String[]{"date","customerNumber","customerName",
        "account","emailAdress","mobilePhoneNumber","emailStatus"};

public static List<String> getColumnNames() {
    return Collections.unmodifiableList(Arrays.asList(COLUMN_NAMES));
}

problem analysis

Note that making a variable field (such as an array) final prevents reassignment of variables, but does not affect the variability of the internal state of the array

Types that can be changed, if it is necessary to declare'public static', can be implemented by the Collections.unmodifiableList described above.

 

"public static" fields should be constant

  • The public static attribute should be constant


Compliance Code

public class Greeter {
  public static Foo foo = new Foo();
  public static String realmOplate = "this is name";

  ...
}

Non-conformance code

public class Greeter {
  public static final Foo FOO = new Foo();
  ...
}
  private static String realmOplate = "this is name";
public static String getRealmOplate() {    
  return realmOplate;
}
public static void setRealmOplate(String realm) {   
  realmOplate = realm;
}

Analysis

In most cases, public static s are an expectation of sharing state among multiple objects.However, with this method, any object can do anything it wants to do in a shared state, such as setting it to null.

Shared values are only prevented from being modified if set to final.

Class variable fields should not have public accessibility

  • Member attributes in classes should not be accessed collectively.


Unqualified Code

public class MyClass {

  public static final int SOME_CONSTANT = 0;     // Compliant - constants are not checked

  public String firstName;                       // Noncompliant

}

Compliance Code

public class MyClass {

  public static final int SOME_CONSTANT = 0;     // Compliant - constants are not checked

  private String firstName;                      // Compliant

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

}

problem analysis

Common class variable fields do not adhere to encapsulation principles and have three main drawbacks:
Other behaviors, such as validation, cannot be added.
The internal representation is public and cannot be changed later.
Member values may change anywhere in the code and may not conform to the programmer's assumptions.

Unauthorized modifications can be prevented by using private properties and accessor methods (settings and acquisitions).

Invoke method(s) only conditionally

 

Non-conformance code

logger.log(Level.DEBUG, "Something went wrong: " + message); 
// Noncompliant; string concatenation performed even when log level too high to show DEBUG messages

Compliance Code

logger.log(Level.DEBUG, "Something went wrong:{} ",message)

Analysis
Logs support a single string output in the case of output, so it is unnecessary to generate multiple strings with + to waste performance.

 

"toString()" and "clone()" methods should not return null

Non-conformance code

public String toString () {
  if (this.collection.isEmpty()) {
    return null; // Noncompliant
  } else {
    // ...
@Override
    public Object clone() {
        try {
            return (ContractQuery) super.clone();
        } catch (CloneNotSupportedException e) {
        }
        return null;
    }

Compliance

public String toString () {
  if (this.collection.isEmpty()) {
    return "";
  } else {
    // ...
     @Override
    public Object clone() throws CloneNotSupportedException {

        return (ContractQuery) super.clone();
    }

Analysis

When toString clone returns Null, null pointer problems tend to occur.

Modifiers should be declared in the correct order

The order in which modifiers are placed in the Java language.

1. Annotations

2. public

3. protected

4. private

5. abstract

6. static

7. final

8. transient

9. volatile

10. synchronized

11. native

12. strictfp

Non-conformance code

public final static String HOME= "home";

 

Compliance Code

public static final String HOME = "home";

Analysis

Codes should be coded according to conventions, not just for machines, but for people to see.What can be used for a machine is not skill, but what everyone understands is endurance.

 

"@Deprecated" code should not be used

So declare expired methods, and classes have their own alternatives.

Common in Reflection

//newInstance() has expired
ContractDto contract = contractClass.newInstance();
//Substitution
contractClass.getDeclaredConstructor().newInstance()

Parentheses should be removed from a single lambda input parameter when its type is inferred

An input parameter in a lambda expression does not need parentheses

Non-conformance code

(x) -> x * 2

Compliance Code

x -> x * 2

summary

Code programming has a long way to go, so I'll look up and down.Using sonar to improve the code, beautiful code is desirable, like a beautiful and sophisticated girl who can't help but keep your eyes open.

 

 

 

It's still necessary to be a aspiring programmer.

Headline Number

Wechat number

Tags: Programming Lambda Attribute Java

Posted on Sat, 14 Mar 2020 22:13:18 -0400 by marms