如何重构抛出异常的函数?

时间:2016-06-19 10:28:02

标签: scala exception refactoring option

假设我正在重构这样的函数:

def check(ox: Option[Int]): Unit = ox match {
  case None => throw new Exception("X is missing")
  case Some(x) if x < 0 => throw new Exception("X is negative")
  case _ => ()
}

我想摆脱异常,但我需要按原样保留check签名和行为,因此我要考虑一个纯粹的实现 - doCheck

import scala.util.{Try, Success, Failure}

def doCheck(ox: Option[Int]): Try[Unit] = ???

def check(ox: Option[Int]): Unit = doCheck(ox).get

现在我正在实施doCheck,如下所示:

def doCheck(ox: Option[Int]): Try[Unit] = for {
  x <- ox toTry MissingX()
  _ <- (x > 0) toTry NegativeX(x)
} yield ()

使用以下implicits

implicit class OptionTry[T](o: Option[T]) { 
  def toTry(e: Exception): Try[T] = o match {
    case Some(t) => Success(t)
    case None    => Failure(e)
  }
}

implicit class BoolTry(bool: Boolean) { 
  def toTry(e: Exception): Try[Unit] = if (bool) Success(Unit) else Failure(e) 
}

有意义吗?

P.S。 implicits肯定会添加更多代码。我希望有一天可以用OptionTry implicit取代scalaz/cats,也许可以找到BoolTry的模拟内容。

1 个答案:

答案 0 :(得分:4)

您可以使用贷款模式重构Try

def withChecked[T](i: Option[Int])(f: Int => T): Try[T] = i match {
  case None => Failure(new java.util.NoSuchElementException())
  case Some(p) if p >= 0 => Success(p).map(f)
  case _ => Failure(
    new IllegalArgumentException(s"negative integer: $i"))
}

然后可以使用如下。

val res1: Try[String] = withChecked(None)(_.toString)
// res1 == Failure(NoSuchElement)

val res2: Try[Int] = withChecked(Some(-1))(identity)
// res2 == Failure(IllegalArgumentException)

def foo(id: Int): MyType = ???
val res3: Try[MyType] = withChecked(Some(2)) { id => foo(id) }
// res3 == Success(MyType)