我正在尝试更多地了解基本Java和不同类型的Throwables,有人能告诉我异常和错误之间的区别吗?
我正在设计一个类似这样的API:
// Drop $howMany items from after $from
def dropElements[T](array: Array[T], from: Int, howMany: Int)
Run Code Online (Sandbox Code Playgroud)
预期的行为是howMany应该是非负的,如果howMany为零,它不应该做任何修改.我有两种方法来实现这个:
def dropElements[T](array: Array[T], from: Int, howMany: Int) {
assert(howMany >= 0);
if (howMany == 0) return;
assert(0 <= from && from < array.length);
....
}
Run Code Online (Sandbox Code Playgroud)
要么:
def dropElements[T](array: Array[T], from: Int, howMany: Int) {
assert(howMany >= 0);
assert(0 <= from && from < array.length);
if (howMany == 0) return;
....
}
Run Code Online (Sandbox Code Playgroud)
我赞成第二种方法(预先声明你的先决条件)与第一种方法相比,但有人指出,当howMany = 0时,第一种方法更加尊重要求.
language-agnostic scala api-design preconditions scala-collections