我通常通过Guava的Precondition方法检查几乎所有的构造函数和公共方法参数.私有方法参数通常带有断言.但是,现在我正在考虑替换"内部"前置条件检查,即检查构造函数/工厂方法/一般方法(不属于公共API /应用程序API)...使用断言,您如何看待?也许这种方式有点快,因为我有很多支票;-)
编辑:我的意思是公共构造函数和工厂,它们不应该是公共API的一部分,只是在内部使用,例如:
/**
* Constructor with both, complete and modifying page.
*
* @param complete
* to be used as a base for this container
* @param modifying
* to be used as a base for this container
*/
public NodePageContainer(final @Nonnull NodePage complete,
final @Nonnull NodePage modifying) {
assert complete != null;
assert modifying != null;
mComplete = complete;
mModified = modifying;
}
Run Code Online (Sandbox Code Playgroud)
在我之前mComplete = checkNotNull(complete);...但它只是从另一个包中的类调用,甚至不应该成为公共API的一部分.如果Java允许降低此类的可见性,那将会很棒;-)