For public methods, use the Javadoc @throws tag to document the exception that will be thrown if a restriction on parameter values is violated (Item 62). Typically the exception will be IllegalArgumentException, IndexOutOfBounds Exception, or NullPointerException(Item 60).

/**

* Returns a BigInteger whose value is (this mod m). This method

* differs from the remainder method in that it always returns a

* non-negative BigInteger.

*

* @param m the modulus, which must be positive

* @return this mod m

* @throws ArithmeticException if m is less than or equal to 0

*/

Public BigInteger mod(BigInteger m) {

if (m.signum() <= 0)

throw new ArithmeticException("Modulus <= 0: " + m);

... // Do the computation

}

For an unexported method, you as the package author control the circumstances under which the method is called, so you can and should ensure that only valid parameter values are ever passed in. Therefore, nonpublic methods should generally check their parameters using assertions.

// Private helper function for a recursive sort

private static void sort(long a[], int offset, int length) {

assert a != null;

assert offset >= 0 && offset <= a.length;

assert length >= 0 && length <= a.length - offset;

... // Do the computation

}

Unlike normal validity checks, assertions throw AssertionError if they fail. And unlike normal validity checks, they have no effect and essentially no cost unless you enable them, which you do by passing the -ea(or -enableassertions) flag to the java interpreter.

Note

It is particularly important to check the validity of parameters that are not used by a method but are stored away for later use.

Summary

Each time you write a method or constructor, you should think about what restrictions exist on its parameters. You should document these restrictions and enforce them with explicit checks at the beginning of the method body.

Effective Java 38 Check parameters for validity的更多相关文章

  1. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  2. 《Effective Java》读书笔记 - 7.方法

    Chapter 7 Methods Item 38: Check parameters for validity 直接举例吧: /** * ...其他的被我省略了 * @throws Arithmet ...

  3. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  4. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  5. Effective Java 64 Strive for failure atomicity

    Principle Failure atomic - A failed method invocation should leave the object in the state that it w ...

  6. Effective Java 第三版——38. 使用接口模拟可扩展的枚举

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  7. [Effective Java]第八章 通用程序设计

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  8. effective java 第2章-创建和销毁对象 读书笔记

    背景 去年就把这本javaer必读书--effective java中文版第二版 读完了,第一遍感觉比较肤浅,今年打算开始第二遍,顺便做一下笔记,后续会持续更新. 1.考虑用静态工厂方法替代构造器 优 ...

  9. Effective Java通俗理解(下)

    Effective Java通俗理解(上) 第31条:用实例域代替序数 枚举类型有一个ordinal方法,它范围该常量的序数从0开始,不建议使用这个方法,因为这不能很好地对枚举进行维护,正确应该是利用 ...

随机推荐

  1. 初涉SQL Server性能问题(1/4):服务器概况

    当你作为DBA时,很多人会向你抱怨:“这个程序数据加载和蜗牛一样,你看看是不是服务器出问题了?”造成这个问题的原因有很多.可能是程序应用服务器问题,网络问题,程序实现方式问题,数据库服务器负荷过重.不 ...

  2. 优化C/C++代码的小技巧

    说明: 无意看到一篇小短文,猜测作者应该是一个图形学领域的程序员或专家,介绍了在光线(射线)追踪程序中是如何优化C/C++代码的.倒也有一些参考意义,当然有的地方我并不赞同或者说我也不完全理解,原文在 ...

  3. C#中往数据库插入/更新时候关于NUll空值的处理

    本文转载:http://blog.csdn.net/chybaby/article/details/2338943 本文转载:http://www.cnblogs.com/zfanlong1314/a ...

  4. ThreadLocal解决线程安全问题

    一.线程安全问题产生的原因 线程安全问题都是由全局变量及静态变量引起的 二.线程安全问题 SimpleDateFormate sdf = new SimpleDateFormat();使用sdf.pa ...

  5. 初学者对WAMP服务器的设置

    服务器设置 在wamp/bin/apache/Apache###/conf/httpd.conf文件中设置 根文件夹 修改documentroot和directory两项 保存后重启服务 404返回值 ...

  6. PHP学习笔记:数据库学习心得

    存储过程: 存储过程(Stored Procedure)是一组为了完成特定功能的SQL 语句集,经编译后存储在数据库.中用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它. 因为语 ...

  7. S2 易买网总结

    易买网项目总结 --指导老师:原玉明 不知不觉,又到了S2结业的时间了,S1的项目KTV项目还历历在目.一路走来,感觉时间过的好快,我们离就业也越来越近... 展示: 1.主页面(首页) 01.商品分 ...

  8. 基于进程的Quartz.NET管理系统QuartzService(一)

    需求 在处理定时任务大家可能都用过Quartz.NET,但在生产环境中大家肯定也遇到过如下的问题: 发布的时候需要停掉所有的Job,再整个一起打包发布 没有管理界面(其实在github也有几个这方面的 ...

  9. DOM笔记整理及应用实例

    一.前言 当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model).HTML DOM 模型被构造为对象的树 通过可编程的对象模型,JavaScript 获得了足够的 ...

  10. Thumbnailator压缩图片

    Thumbnailator是一款不可多得的处理图片的第三方工具包,它写法简单到让人无法相信,Java本身也有处理图片压缩的方法,但是代码冗长到让人痛不欲生,在篇末会给出Java本身的实现方式,做下对比 ...