Google Guava中Preconditions的用法,让前置条件判断变得更优雅

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zivensonice/article/details/51912188

这几天看Fresco和Glide的代码,发现他们都使用了Preconditions来做前置判断条件,发现这样写,代码变得既优雅又容易调试。

Preconditions的用法

OK,先看看平常写代码我们是怎么做的参数判断。
判断参数,如果参数配置错误,就抛出异常

    int[] intArray = {1, 2, 3, 4, 5, 6};

    private void testPreconditions(boolean preCondition, int[] array, int position) {
        if (!preCondition) {
            throw new IllegalArgumentException("preCondition not allow!!");
        }
        if (array == null) {
            throw new NullPointerException("array is null!!");
        }
        if (array.length == 0) {
            throw new IllegalArgumentException("array length is 0!!");
        }
        if (position > array.length || position < 0) {
            throw new ArrayIndexOutOfBoundsException("position error!!");
        }
        //do something...

    }

这里看起来,没什么,但是如果我这个类很大,方法很多呢?看到全篇都是if(XX==null) throw{...}作何感想。
下面我们看看如果用Preconditions做前置判断

    private void testPreconditions(boolean preCondition, int[] array, int position) 
    {
        Preconditions.checkArgument(preCondition);
        Preconditions.checkNotNull(array);
        Preconditions.checkElementIndex(position, array.length, "position error!");
          //do something...
    }

是不是清爽多了,使用Preconditions可以更清晰的表达程序意图

为什么要使用Preconditions

那么你可能会问,为啥要用Preconditions,如果是空指针在用的时候,他自己就会抛出异常啊。
但是我们期望的是尽早抛出异常,而不是等到数据被层层传递,传递到非常深的位置,这样浪费系统资源更不利于我们开发精确定位错误。
所以推荐在方法的入口,或运算开始前,先检查数据。
例如:

       int length = datas.get(5).getDescription().concat("XX").split(",").length;

像这种代码,就算抛出空指针也不知道到底是那个对象空指针了,需要一步一步调试

这就是Preconditions另一个作用:尽早发现错误,精确控制出错的位置

抽取Preconditions

Preconditions是guava的一个功能, 项目地址:https://github.com/google/guava
使用方式直接引用

dependencies {
  compile 'com.google.guava:guava:19.0'
}

guava的诞生旨在对Java Collection进行扩展以提高开发效率,但是随着多年的发展它已经覆盖到了Java开发的方方面面,在Java 8中,已经可以看到不少API就是从Guava中原封不动的借鉴而来。
例如,初始化一个集合,使用JDK这么做

//JDK
List<String> list = new ArrayList<String>(); 
list.add("a");
list.add("b");
list.add("c");
list.add("d");

但是使用Guava,可以很简洁的表达

//Guava
List<String> list = Lists.newArrayList("a", "b", "c", "d");

但是这个库中包含大约14k的方法数,是个很大的库,这里我们只需要他的Preconditions这个类就行了。
地址:https://github.com/google/guava/blob/master/guava/src/com/google/common/base/Preconditions.java

猜你喜欢

转载自blog.csdn.net/zivensonice/article/details/51912188
今日推荐