可变字段不该该是public static程序员
不合规编程
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 // ... }
合规数组
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)); }
问题分析微信
请注意,使可变字段(例如数组)为final将阻止从新分配变量,但这样作不会影响数组内部状态的可变性ide
能够改变的类型若是很是有必要声明为 ‘public static’ 能够经过上述 Collections.unmodifiableList 方式实现。性能
public static 属性应是常量this
合规代码编码
public class Greeter { public static Foo foo = new Foo(); public static String realmOplate = "this is name"; ... }
不合规代码spa
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; }
分析指针
在大多数状况下,public static 在多个对象之间共享状态的一种指望。可是使用这种方法,任何对象均可以在共享状态下作任何想作的事情,例如将其设置为null。
只有设置为了final时才会防止共享值被修改。
类中的成员属性不该该共有访问。
不合格代码
public class MyClass { public static final int SOME_CONSTANT = 0; // Compliant - constants are not checked public String firstName; // Noncompliant }
合规代码
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; } }
问题分析
公共类变量字段不遵照封装原理,而且具备三个主要缺点:
没法添加其余行为,例如验证。
内部表示形式已公开,之后不能更改。
成员值可能会在代码中的任何位置发生更改,而且可能不符合程序员的假设。
经过使用私有属性和访问器方法(设置和获取),能够防止未经受权的修改。
不合规代码
logger.log(Level.DEBUG, "Something went wrong: " + message); // Noncompliant; string concatenation performed even when log level too high to show DEBUG messages
合规代码
logger.log(Level.DEBUG, "Something went wrong:{} ",message)
分析
日志在输出的状况下支持一个字符串输出,没有必要经过+生成多个字符串,浪费性能。
不合规代码
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; }
合规
public String toString () { if (this.collection.isEmpty()) { return ""; } else { // ... @Override public Object clone() throws CloneNotSupportedException { return (ContractQuery) super.clone(); }
分析
当toString clone 返回Null时,容易产生空指针问题。
Java 语言中修饰符的位置顺序。
1. Annotations
2. public
3. protected
4. private
5. abstract
6. static
7. final
8. transient
9. volatile
10. synchronized
11. native
12. strictfp
不合规代码
public final static String HOME= "home";
合规代码
public static final String HOME = "home";
分析
应根据约定俗称的规范进行编码,代码不单单是给机器用,但倒是给人看的。写给机器的能用的不叫本事,写出人人都看的懂的才是能耐。
因此声明过时的方法,类都有与之对应的代替方式。
反射中经常使用的
//newInstance() 已过时 ContractDto contract = contractClass.newInstance(); //代替方式 contractClass.getDeclaredConstructor().newInstance()
lambda 表达式中一个输入参数没有必要加括号
不合规代码
(x) -> x * 2
合规代码
x -> x * 2
代码编程长路漫漫,吾将上下而求索。使用sonar改进代码,漂亮的代码使人向往,犹如一位漂亮精致的姑娘,让人目不转睛,爱不释手。
作个有追求的程序员仍是有必要的。
头条号
微信号