前置条件

傷城~ 2022-04-02 15:18 276阅读 0赞

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

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

  1. int[] intArray = {1, 2, 3, 4, 5, 6};
  2. private void testPreconditions(boolean preCondition, int[] array, int position) {
  3. if (!preCondition) {
  4. throw new IllegalArgumentException("preCondition not allow!!");
  5. }
  6. if (array == null) {
  7. throw new NullPointerException("array is null!!");
  8. }
  9. if (array.length == 0) {
  10. throw new IllegalArgumentException("array length is 0!!");
  11. }
  12. if (position > array.length || position < 0) {
  13. throw new ArrayIndexOutOfBoundsException("position error!!");
  14. }
  15. //do something...
  16. }

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

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

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

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

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

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

这就是Preconditions另一个作用:尽早发现错误,精确控制出错的位置 抽取Preconditions
/**
* Created by lgq on 16-1-5.

  1. */
  2. public class TestPreconditions {
  3. /**
  4. * 前置条件检查,当条件不满足时,就会抛出异常
  5. */
  6. @Test
  7. public void testPreconditions(){
  8. String constantName = null;
  9. //Preconditions.checkNotNull(constantName,"constantName 为 null");
  10. String str = "abc";
  11. Preconditions.checkNotNull(str,"str 为 null"); // 不会输出
  12. int age = 23;
  13. Integer number = 12;
  14. //Preconditions.checkArgument(age < 20, "age 必须要< 20");
  15. Preconditions.checkArgument(number < 20, "number 必须要< 20");
  16. List<Integer> integerList = Lists.newArrayList(3,5,6,98,2,45);
  17. for(int i=0; i<integerList.size(); i++) {
  18. // 检查数组下标是否越界
  19. int passIndex = Preconditions.checkElementIndex(i, integerList.size());
  20. //System.out.println("下标:"+ passIndex + " ==> 满足要求");
  21. }
  22. // 检查下标值7 是否在集合integerList中
  23. //Preconditions.checkElementIndex(7, integerList.size());
  24. //Preconditions.checkPositionIndex(8, integerList.size());
  25. Preconditions.checkPositionIndexes(2, 4, integerList.size());
  26. // 判断是否为true,当为false时,会抛出IllegalStateException
  27. Preconditions.checkState(4 < 3);
  28. }
  29. }

发表评论

表情:
评论列表 (有 0 条评论,276人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C++ 声明

    问题 最近遇到了两个类A、B相互调用的情况,于是想当然的在两个类A和B的头文件中 \include 了所需的头文件,当然结果编译报错了。为什么呢,A需要B,B需要A,形成

    相关 条件

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

    相关 Vue——课程

            本博客记录了小编在学习Vue过程中的体会与感悟,简单来说就是小编的学习笔记,学习地址主要为Vue的官网,官网地址为:[《Vue官方文档》][Vue]。相关学习笔