Have seen a few play apps' source and it feels like massive boilerplate and leaky promise/future/async crud everywhere.
Lets take this:
case class ArticlePage(article: Article, storyPackage: List[Trail], edition: String)
object ArticleController extends Controller with Logging {
def render(path: String) = Action { implicit request =>
val promiseOfArticle = Akka.future(lookup(path))
Async {
promiseOfArticle.map(_.map { renderArticle }.getOrElse { NotFound })
}
}
private def lookup(path: String)(implicit request: RequestHeader): Option[ArticlePage] = suppressApi404 {
// .. stuff omitted
}
private def renderArticle(model: ArticlePage)(implicit request: RequestHeader): Result = // .. omitted
}
Let's look at that render
method. What does it do? It's pretty hard to tell, even with a lot of the code removed. It's trying to render an article.
If we just wrote some vanilla code to do that, it would be:
def render(path:String) = {
renderArticle(lookup(path).getOrElse(NotFound))
}
So what's all that other stuff? We've got all the buzzwords covered:
- promise
- future
- async
Why is this here? Several of this apps controllers have this exact structure. Shouldn't this concern be separated somehow? Even the Action
DSL method
that starts this off sticks out like a sort thumb. This method is all unnecessary complexity. To figure out what it does, I have to know what futures
and promises are, and what Async
does, but moreover, almost none of this method's source is specific to the business logic being performed. Why?
Let's see some more boilerplate:
implicit request
- why do I have to type this in every controller? Shouldn't every controller have a request without me typing code?!views.html.article
- I really have to tell theArticleController
to use HTML and to use thearticle
view?Cached
- why is this in the controller? This should be a separate concern.
I realize that some of this might be because of Scala's type system, but it just feels like we've taken the same boilerplate from XML or Annotations and made it code and somehow that makes it OK.
I hated repeating myself and telling the framework what it should've already known in XML, XDoclet, and annotations. I don't like it any better in code.
implicit request - Action is just function that takes a request and returns a response. If you don't need to inspect the request you could have as easily used Action ( … ) // No request naming.
views.html.article - Play uses routes that are in scala. If that view doesn't exist it wouldn't compile. There's no convention that controller actions map to a specific named view. Maybe this is a worthwhile improvement. With macro support it would probably be possible to do at compile time w/o reflecting the current object name.
Cached - I'm probably missing the main reason why this is bad.
A nice part of Play is that since everything is so explicit and mostly functional you can compose your common Actions so that you could define a function that goes from a request to an async cached response without so much code.
E.g. instead of
You could write
The promise/future fiasco is unfortunate and will be resolved in the next release. It's a result of being bought by typesafe and switching over to their future libraries instead of using a homegrown promise one.