【编程思想】01 编写高质量代码、Java 开发中通用的方法和准则

1、【建议】: 不要在常量和变量中出现易混淆的字母

  1. 基本规范:包名全小写,类名首字母全大写,常量全部大写并用下划线分隔,变量采用驼峰命名法(Camel Case)命名等。
  1. 为了让程序更容易理解,字母“l”(还包括大写字母“O”)尽量不要和数字混用,以免使阅读者的理解与程序意图产生偏差。如果字母和数字必须混合使用,字母“l”务必大写,字母“O”则增加注释。
  1. 字母“l”作为长整型标志时务必大写。

2、【建议】: 莫让常量蜕变成变量

  1. 常量就是常量,在编译期就必须确定其值,不应该在运行期更改,否则程序的可读性会非常差
  1. 变化是指每次运行都会变化。常量就应该是确定的值,不是运行时才确定。除非有特别的需求。
package com.hao.test;

import lombok.extern.slf4j.Slf4j;

import java.util.Random;

/**
 * @author haoxiansheng
 */
@Slf4j
public class Client {
    public static void main(String[] args) {
        log.info("Const1=>{}",Const.RAND_CONST);
        log.info("Const1=>{}",Const1.RAND_CONST);
        log.info("Const1=>{}",Const.RAND_CONST);
        log.info("Const1=>{}",Const1.RAND_CONST);
    }
}

interface Const {
    //  是否为常量
    public static final int RAND_CONST = new Random().nextInt();
}
class Const1{
    //  是否为常量
    public static final int RAND_CONST = new Random().nextInt();

}

3、【建议】: 三元操作符的类型务必一致

  1. 代码案例
package com.hao.test;

import lombok.extern.slf4j.Slf4j;

/**
 * @author haoxiansheng
 */
@Slf4j
public class Test1 {
    public static void main(String[] args) {
        int a = 80;
        String s1 = String.valueOf(a < 100? 90 : 100);
        String s2 = String.valueOf(a < 100? 90 : 100.0);
        log.info("si==s2?=>{}",s1.equals(s2));
        log.info("s1=>{}, s2=>{}", s1, s2);
    }
}

2)三元操作符类型的转换规则

a:若两个操作数不可转换,则不做转换,返回值为Object类型。

b:若两个操作数是明确类型的表达式(比如变量),则按照正常的二进制数字来转换,int类型转换为long类型,long类型转换为float类型等。

c:若两个操作数中有一个是数字S,另外一个是表达式,且其类型标示为T,那么,若数字S在T的范围内,则转换为T类型;若S超出了T类型的范围,则T转换为S类型

d:若两个操作数都是直接量数字,则返回值类型为范围较大者。

  1. 保证三元操作符中的两个操作数类型一致,即可减少可能错误的发生。

4、【建议】: 避免带有变长参数的方法重载

  1. 代码案例
package com.hao.test;

import lombok.extern.slf4j.Slf4j;

import java.text.NumberFormat;

/**
 * @author haoxiansheng
 */
@Slf4j
public class Test2 {
    public static void main(String[] args) {
        Test2 client = new Test2();
        client.calPrice(49900, 75);
    }

    // 复杂多折扣计算
    public void calPrice(int price, int... discounts) {
        float knockdownPrice = price;
        for (int discount : discounts) {
            knockdownPrice = knockdownPrice * discount / 100.0F;
        }
        log.info("复杂多折扣后的价格折扣=>{}", formateCurrency(knockdownPrice));
    }
    // 简单折扣计算
    public void calPrice(int price, int discount) {
        float knockdownPrice = price * discount / 100.0F;
        log.info("简单折扣后的价格折扣=>{}", formateCurrency(knockdownPrice));
    }

    // 格式化成本的货币形式
    private String formateCurrency(float price) {
        return NumberFormat.getCurrencyInstance().format(price / 100);
    }
}


  1. 慎重考虑变长参数的方法重载

5、【建议】:别让null值和空值威胁到变长方法

  1. 代码编译错误,重载导致
package com.hao.test;

/**
 * @author haoxiansheng
 */
public class Test3 {
    public static void main(String[] args) {
        Test3 client = new Test3();
        // 错误
        client.methodA("hello", null);
        client.methodA("hell0");
        // 正确
        String[] strings = null;
        client.methodA("hello", strings);
    }

    public void methodA(String str, Integer...integers) {

    }

    public void methodA(String str, String...strings){

    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章