Is there any way to simplify this code I'd like to simplify  this Scala code:

private def getToken() = {
    val token = getTokenFromDb()
    token match {
      case Some(x) =&gt; token
      case None =&gt; {
        val remoteToken = getTokenRemote()
        remoteToken match {
          case Some(x) =&gt; {
            writeTokenToDb(x)
            remoteToken
          }
          case None =&gt; None
        }
      }
    }
  }
private def getToken(): Option[Token] = {
  def remoteToken =
    getTokenRemote() map { r =&gt; writeTokenToDb(r); r }
  getTokenFromDb orElse remoteToken
}


Please write always the type of a function for outer functions - it makes it far more readable to understand the source code (often it is useful to add the types to inner functions/declarations as well).

Beside from that you can also write case t: Some =&gt; t or case t @ Some(_) =&gt; t to express that you return the matched type. Your way of case Some(x) =&gt; token is hard to understand, because one needs to know what's token.

Furthermore, you don't need braces around a match block, you can simply write

case x =&gt;
  a
  b


And naming a variable in a similar way as a function, like in val token = getToken() is completely unnecessary, when it is only used once, it also doesn't increase readability. In such cases just inline the function call: getToken match { ... }.