Java try-catch可以用于代码优化吗?

Java try-catch可以用于代码优化吗?,java,nullpointerexception,try-catch,Java,Nullpointerexception,Try Catch,假设我有以下课程: public class RectangularPrism{ public Integer width; public Integer height; public Integer depth; //setters and getters } 我用它来计算所有棱镜的体积之和。我有两种方法来计算这个。哪一个更好 这一个使用条件运算符 public Integer volumeSum(List<RectangularPrism> p

假设我有以下课程:

public class RectangularPrism{
    public Integer width;
    public Integer height;
    public Integer depth;

    //setters and getters
}
我用它来计算所有棱镜的体积之和。我有两种方法来计算这个。哪一个更好

这一个使用条件运算符

public Integer volumeSum(List<RectangularPrism> prismList){
        Integer sum = 0;
        for(RectangularPrism prism: prismList){
            Integer width = prism.getWidth() == null ? 0 : prism.getWidth();
            Integer height = prism.getHeight() == null ? 0 : prism.getHeight();
            Integer depth = prism.getDepth() == null ? 0 : prism.getDepth();
            sum += width * height * depth;
        }
        return sum;
    }
public Integer volumeSum(列表prismList){
整数和=0;
用于(矩形棱镜:棱镜列表){
整数宽度=prism.getWidth()==null?0:prism.getWidth();
整数高度=prism.getHeight()==null?0:prism.getHeight();
整数深度=prism.getDepth()==null?0:prism.getDepth();
总和+=宽度*高度*深度;
}
回报金额;
}
这一个使用try catch

public Integer volumeSum(List<RectangularPrism> prismList){
        Integer sum;
        for(RectangularPrism prism: prismList){
            try {
                Integer width = prism.getWidth();
                Integer height = prism.getHeight();
                Integer depth = prism.getDepth();
                sum += width * height * depth;
            }catch( NullPointerException e){}
        }
        return sum;
    }
public Integer volumeSum(列表prismList){
整数和;
用于(矩形棱镜:棱镜列表){
试一试{
整数宽度=prism.getWidth();
整数高度=prism.getHeight();
整数深度=prism.getDepth();
总和+=宽度*高度*深度;
}捕获(NullPointerException e){}
}
回报金额;
}
如果我的类有100个或更多决定结果的字段,哪一个更好


使用try catch处理空值可以吗?

这在每个级别上都是一个坏主意

  • try-catch
    实际上较慢(如果确实引发了异常)
  • 您在非异常情况(即正常控制流)中使用异常,这在概念上是错误的
  • 如果
    try-catch
    块中的任何其他内容抛出了合法的NPE,您将无法知道发生了什么。(例如,想象一下当
    prism
    本身为空时的情况。)
  • 仅从代码中很难看出您的意图是什么。使用
    ?:
    运算符检查空值是一种大家都能识别的既定模式
  • 一个空的
    catch
    块是一个很大的禁忌,而且可能是产生bug的最可靠的方法,这些bug需要花费很长时间才能找到
  • 在这种情况下,这两个代码段甚至不相等,因为在“异常驱动”的代码段中,一个
    null
    值意味着将跳过
    try
    块的其余部分。当然,
    sum
    仍然是正确的,但这只是巧合
  • 如果确实想提高性能,不要使用
    Integer
    和其他包装类进行简单计算,而是使用相应的原语(
    int
    )。这样做的另一个好处是,您突然不必再担心
    null
    s,因为原始值永远不能
    null

    总之,永远不要这样做。 说真的,不要。每次你这么做,一只可爱的小猫就死了。

    这在各个层面上都是个坏主意

  • try-catch
    实际上较慢(如果确实引发了异常)
  • 您在非异常情况(即正常控制流)中使用异常,这在概念上是错误的
  • 如果
    try-catch
    块中的任何其他内容抛出了合法的NPE,您将无法知道发生了什么。(例如,想象一下当
    prism
    本身为空时的情况。)
  • 仅从代码中很难看出您的意图是什么。使用
    ?:
    运算符检查空值是一种大家都能识别的既定模式
  • 一个空的
    catch
    块是一个很大的禁忌,而且可能是产生bug的最可靠的方法,这些bug需要花费很长时间才能找到
  • 在这种情况下,这两个代码段甚至不相等,因为在“异常驱动”的代码段中,一个
    null
    值意味着将跳过
    try
    块的其余部分。当然,
    sum
    仍然是正确的,但这只是巧合
  • 如果确实想提高性能,不要使用
    Integer
    和其他包装类进行简单计算,而是使用相应的原语(
    int
    )。这样做的另一个好处是,您突然不必再担心
    null
    s,因为原始值永远不能
    null

    总之,永远不要这样做。 说真的,不要。每次你这么做,一只可爱的小猫就死了。

    决不能使用
    try-catch
    来处理程序的控制流


    始终使用已建立的方法,在这种情况下,请像在第一个代码示例中一样检查null。

    您不应使用
    try catch
    来处理程序的控制流


    始终使用已建立的方法,在这种情况下,请像在第一个代码示例中一样检查null。

    我知道这不会回答问题(可以使用
    try..catch
    进行代码优化),但我认为您不应该为
    null
    值操心:您应该强制执行(
    宽度
    高度
    深度
    )为非
    ,如果它们是对象的固有属性

    我的意思是,您正在构建一个
    矩形棱柱体
    宽度
    高度
    ,和
    深度
    来定义此对象。您不能在其中包含
    null
    ,否则它将不会是矩形棱柱体

    在这种情况下,我宁愿这样做:

    public class RectangularPrism {
        private final Integer width;
        private final Integer height;
        private final Integer depth;    
        public RectangularPrism(Integer width, Integer height, Integer depth) {
          this.width = Objects.requireNonNull(width, "width");
          this.height = Objects.requireNonNull(height, "height");
          this.depth = Objects.requireNonNull(depth, "depth");
        }
        // getters    
    }
    
    或使用setter:

    public class RectangularPrism {
        public Integer width;
        public Integer height;
        public Integer depth;
    
        public void setWidth(Integer width) {
          this.width = Objects.requireNonNull(width, "width");      
        }
    }
    
    您不必处理
    null
    ,因为JVM将抛出
    NullPointerException
    ,而只是因为对象没有完全/正确初始化,而不是因为您没有处理不应该存在的
    null
    值。这是一种正常行为,就像抛出
    非法状态异常一样de>因为对象处于不应处于的状态(此处:因为您有
    null
    值)

    为了完整,你必须
    public class RectangularPrism {
        private final Integer width;
        private final Integer height;
        private final Integer depth;    
        public RectangularPrism(Integer width, Integer height, Integer depth) {
          this.width = Objects.requireNonNull(width, "width");
          this.height = Objects.requireNonNull(height, "height");
          this.depth = Objects.requireNonNull(depth, "depth");
        }
        public Integer computeVolume() {
          return width * height * depth; // can't be null.
        }
    }
    
    public Integer volumeSum(List<RectangularPrism> prismList) {
      int initial = 0;
      for (RectangularPrism p : prismList) 
        initial += p.computeVolume();
      return initial;
    }