I understand the whole principle and concept behind the Some/None/Option and I can certainly appreciate its merits. My question is more of best practice. When does it become overkill and when does it make sense to use it religiously. I think (and i could be wrong) but it makes sense to use it as much as possible since it's a safer way to pass around nothing (as opposed to null). The thing I see myself doing a lot though is having some functions littered with map, getOrElse, get, match and even sometimes nesting them which tends to look ugly. Is there some concept I'm missing or some best practice to do with a function that receives multiple Optional values. For example:
def updateJobs(id: Int) = withAuth {
request => {
User.userWithToken(request.headers.get("token").get).map {
user =>
Job.jobsAfterIdForForeman(id.toString, user.id.toString) match {
case Some(json) => Ok(json)
case _ => NoContent
}
}.getOrElse(BadRequest)
}
}
or even worse for example:
def addPurchaseRequest(json: JsValue) = {
(json \ "jobId").asOpt[Long].map {
jobId => JobDAO.jobWithId(jobId).map {
job => PurchaseRequestDAO.insert(new PurchaseRequest(json, job)).map {
model =>
val request = model.asInstanceOf[PurchaseRequest]
(json \ "items").asOpt[List[JsObject]].map {
list => {
if (PurchaseItemAssociationDAO.bulkInsert(PurchaseItemAssociation.itemsFromJsonArray(list, request.id))) Option(request.addResponseJson) else None
}
}.getOrElse(None)
}.getOrElse(None)
}.getOrElse(None)
}.getOrElse(None)
}
I managed to refactor some to not look so crazy, but is there a better way to refactor this so it doesn't look so crazy? Am I missing something or do you get used to things just looking like this? Seems like there should certainly be a cleaner practice.