相关疑难解决方法(0)

异常和错误之间的差异

我正在尝试更多地了解基本Java和不同类型的Throwables,有人能告诉我异常和错误之间的区别吗?

java exception throwable

162
推荐指数
6
解决办法
8万
查看次数

API设计 - 在条件超出范围的前置条件检查中混合使用?

我正在设计一个类似这样的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

5
推荐指数
1
解决办法
163
查看次数