首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么在outOfBoundsMsg中有私有方法java.util.ArrayList?

为什么在outOfBoundsMsg中有私有方法java.util.ArrayList?
EN

Stack Overflow用户
提问于 2014-03-04 14:28:07
回答 1查看 263关注 0票数 6

这是来自java.util.ArrayList的片段

代码语言:javascript
复制
/**
 * Constructs an IndexOutOfBoundsException detail message.
 * Of the many possible refactorings of the error handling code,
 * this "outlining" performs best with both server and client VMs.
 */
private String outOfBoundsMsg(int index) {
    return "Index: "+index+", Size: "+size;
}

这是来自com.google.collect.Preconditions的片段

代码语言:javascript
复制
  /*
   * All recent hotspots (as of 2009) *really* like to have the natural code
   *
   * if (guardExpression) {
   *    throw new BadException(messageExpression);
   * }
   *
   * refactored so that messageExpression is moved to a separate
   * String-returning method.
   *
   * if (guardExpression) {
   *    throw new BadException(badMsg(...));
   * }
   *
   * The alternative natural refactorings into void or Exception-returning
   * methods are much slower.  This is a big deal - we're talking factors of
   * 2-8 in microbenchmarks, not just 10-20%.  (This is a hotspot optimizer
   * bug, which should be fixed, but that's a separate, big project).
   *
   * The coding pattern above is heavily used in java.util, e.g. in ArrayList.
   * There is a RangeCheckMicroBenchmark in the JDK that was used to test this.

希望有人能阐明:

  • 为什么需要私有outOfBoundsMsg
  • 意思是“这个大纲表现最好.”
  • 是否应该开始重构代码以包含异常构造函数的字符串返回方法?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-04 14:32:35

意思是“这个大纲表现最好.”

这是相反的内联,但不是一个标准的术语,这就是为什么它是常见的。

为什么需要私有outOfBoundsMsg

这就是“大纲”的意思--将代码提取到单独的方法中。

是否应该开始重构代码以包含异常构造函数的字符串返回方法?

如果您关心每次抛出没有字符串文字的异常时浪费3纳秒时间,那么是的。换句话说:没有。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22174682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档