3 ms·
I think that's a pretty extreme position. First, it will run a lot slower than the pattern matching alternative: optional match { case Some(x) => wha
by modersky 13y ago
I think that's a pretty extreme position. First, it will run a lot slower than the pattern matching alternative:
optional match {
case Some(x) => whatever(x)
case None => fallback
}
Second, I find the pattern matching code much clearer. Sure, it's also longer, but clarity trumps everything.
- noelwelsh 13y agomap / getOrElse is standard in all the Scala code I've seen. Abstracting over the monad is also a pretty big win that has made some big refactorings painless for me. Pattern matching is really only used by beginners IME.
- modersky 13y agoWell, then maybe the beginners do it right? You have not argued against my two points: It's much slower, and less clear.
- noelwelsh 13y agoClarity comes from familiarity. It's not an objective measure. The speed benefit is well known, as is the corollary, premature optimisation. I expect checking for null is even faster if speed is the main concern. It's an engineering tradeoff, just like making the abstraction to a monad (or monad plus). In the normal course of events I'm more concerned about flexibility than performance and would prefer the abstraction.