|
|
|
|
|
by mark242
3428 days ago
|
|
In Scala, you're encouraged to write more bulletproof code by avoiding exceptions at all time, simply because it's easier to realize that (in your case) getSomeRiskyNumber returns an option instead of an integer. getSomeRiskyNumber map {
case Some(i) => Map("risk" -> i)
case None => Map("error" -> "Error retrieving number.")
} map { response =>
Json.toJson(response)
}
Reading this code you know you're always going to generate a json response to the user, and depending upon what getSomeRiskyNumber() returns, the json response will either look like { "risk": 23 } or { "error": "Error retrieving number." }. No exceptions needed. |
|
Also, currently, Java doesn't have a match {} expression that works with Some() and None. I belive that would completely change my opinion and the name of the game.