Skip to content

3 Great Mistakes of Contemporary App Development

Date: 2016-03-08

Summary: 3 common patterns in web app development that should be questioned

Prelude

I don't normally write contentious things. And let me say up front: this isn't a criticism of individual people, or some of the great software that people build. But I've been around software for a long time now; which means I'm old; which means I get grouchy now and then. And a wise colleague said something that has stayed with me: paraphrasing, it's better to express opinion if the intent is to facilitate progress, or at least constructive dialogue. And if you're proved wrong, have the courage to admit your mistake, learn from it, and move on.

That is the intent behind what follows. It may (or may not) be contentious; but it's meant to be constructive.

With that said, here we go. What follows are recurring patterns I've observed in contemporary application development. I think they're wrong. I should be clear that, in this context, "application" means typical interactive web applications. So the things that have Web UIs, through which users view and/or edit data that are typically held in some kind of database. It might be a messaging app, a forum, a customer management app, etc. You get the picture.

Mistake number 1: Conflating logical and physical separation

Web clients run in the browser, which runs on the client's machine (desktop, laptop, tablet, whatever). The server components run elsewhere: perhaps the application owner's datacentre, or increasingly some third party's (the "cloud"). Either way, it's rare for both to be hosted on the same machine. That means there's network-based distribution, with all the attendant characteristics that brings: latency, partial failure, etc.

This is physical separation. In the 80s and 90s, we thought we could abstract away from it: RPC, CORBA, and DCOM tried to. They were at best partially successful, simply because they failed to acknowledge the different characteristics of local vs. remote interaction. Synchronous calls aren't a good model when distribution is involved. It's now generally accepted that asynchronous message passing is a better foundation for distributed communication. More on that later.

We also know that Models and Views are different things. Models are the underlying concepts, the things that form the language of our applications: Customers, Orders, "To Do" items. Views are the way we present those: text, diagrams, inputs, buttons, etc. And compositions thereof.

This is logical separation. We don't want to mix models with their views, or we end up with duplicate code and/or inconsistent behaviour. Again, we've known this since at least Trygve Reenskaug's seminal papers on MVC.

All good so far. But it all goes wrong when we mix the two. "Clients" render the UI, so they must be the view, right? And so, by logical deduction, "servers" are the models.

Bzzzt. Wrong. No.

Much of the accidental complexity in our apps today arises from this mistake. Patterns like MVVM and supporting frameworks like knockout are symptomatic of the malaise. It goes something like this:

  1. The model is stored on the server
  2. We need to render the view on the client
  3. The view needs information from the model
  4. But the model is stored on the server
  5. So we need to copy the relevant data from the server to the client
  6. So we'll store a copy of the model on the client for the view to use.

All OK so far.

  1. We'll expose the server view as web services (good practice, right?)
  2. Those web services will satisfy the read & write needs of all the views
  3. Oh wait, we've lots of views. And they need overlapping but different data.
  4. Hey, we seem to be building a lot of web services here. Can't you front end guys just adapt a smaller set of services?
  5. No way server chap, we don't want to complicate our client code with filtering out the data we need. And it's wasteful to send unnecessary data. Latency!

Oh dear. It's all gone a bit pear shaped. And, in reality of course, it's worse than that. Even if the client and server guys work in perfect harmony, there's loads of duplication going on. The view model must be a projection (subset) of the underlying server model, but it's written in javascript ('course). And the server's in C# or Java or Ruby or Python or (if you're hip/lucky) Elixir. So anytime we change the conceptual model, we need to manually edit the server model and all its client variants ("Hey Product Owner guy, why didn't you tell me customers need email addresses?").

This sucks. I mean time and money, but also in the more pejorative sense. Where did it all go wrong?

Because we conflated physical and logical separation. They're not the same thing. Specifically: Model-View separation isn't the same thing as Client-Server separation. In particular, the Model doesn't fit the mould. It sits across both client and server.

So at this point (if you're still here), you're thinking "....uuuuh, yeah... that's exactly why we need MVVM and such like".

Yes. And No.

Yes: we need a way to distribute the model over client and server. But no, it shouldn't be the application developer's job to do it.

Let me say that again. It shouldn't be the application developer's job to manually construct model distribution. No negotiating what the web services should be; no manual, co-ordinated updates to "client" and "server" models when the requirements change.

These things are entirely derivable. The UI developer should simply pick the model data they need; the underlying library/environment can then figure out what needs distributed when. From the developer's point of view, it's a single logical model.

Don't believe me? Take a look at Webdsl. Or Nitrogen. Or R Shiny. Notice anything? Views. And Models. No "view models", no web services. Just clear code.

Woah, what kind of magic is this? No magic: just proper separation of concerns. The developer concentrates on the logical concerns - the application concerns. What concepts and behaviour does my application need? How do I render those concepts in each given context (view)?

The framework takes care of the physical concerns. Each of the above example frameworks implements a distribution strategy under the covers. Nitrogen, for example, uses Websockets. It's then trivial for the framework to look at each view and say "View A needs data items 1, 2 and 3. OK, I'll send 'em over".

And because the developer only deals with one model, it's not just that the code's cleaner. Evolution is quicker, cheaper and less error prone- because the model only has to change in one place. Because there is only the model. There's no view model, at least not one exposed to the application developer.

We can build applications that require less code that are easier to understand, maintain and evolve.

It's all doable. But it stems from realising mistake number 1: the falsehood in conflating logical and physical separation.

Mistake number 2: Controllers

It might seem strange to have discussed web app architecture without giving more than passing mention to Model-View-Controller (MVC). There's a good reason for that. Controllers are so especially wrong that they deserve to be a headline mistake all on their own.

The heresy police are probably on their way. If I don't finish the paragraph, you'll know I've been lynched. Pretty much every language used for web app development has at least one MVC framework. It would be no exaggeration to say it's the most influential pattern in web apps today.

And it's wrong.

Specifically: Controllers are wrong.

It's worth reading Reenskaug's original papers if you haven't already. Whilst the language is slightly strange (reflective of the time they were written), one thing really jumps out: Controllers don't sit between Views and Models. Some quotes are apposite:

"A controller is the link between a user and the system".

"The controller receives such user output, translates it into the appropriate messages and passes these messages on to one or more of the views".

It's always possible I'm mis-interpreting of course. But it seems pretty clear that Reenskaug saw Controllers as a means to connect users to views, not views to models. Now, those papers were written in the early days of GUI programming. The context was "thick client" UIs; desktop applications. The web's different; apps are distributed.

We're back to mistake number 1. Controllers in web applications are symptomatic of the logical/physical conflation. They're bad for at least 2 reasons:

  1. It's where application developers insert all the logic that connects the server to the client. This is done manually. Mistake no. 1 asserts this is wrong.
  2. They cause anaemic domain models. Logic that belongs in the domain model floats out into the controllers, creating a hodge-podge of application routing, view selection, and domain logic. In fact, you might as well throw in security for good measure (oh, wait... we do).

Controllers are ugly, time hogs, and lead to poor structure. They are a mistake. Remedy mistake number 1 and the need for Controllers as application-level constructs goes away. The domain logic belongs in the model; the presentation in the view. The view is nothing more or less than a representation of the model - its data and behaviour. The framework/environment deals with distribution. Controllers are unnecessary.

Mistake number 3: Async

"He's finally jumped the shark". I'll admit this is a rather duplicitous headline. So let me clear up a couple of things:

  1. We need concurrency. It's fundamentally important.
  2. Asynchronous message passing is a very good way to co-ordinate among concurrent activities. Carl Hewitt is nothing short of a genius for conceiving the Actor Model, particularly since he did so in 1973.

So the headline says "Async is a mistake", and I just said "Async is good". Makes no sense.

Let's clear it up. Async, as embodied in current mainstream languages, is a mistake. Or rather, it's half-baked. But this article is already too long. So I'll pick this one up separately at some point.

Update 2022-12-22: I finally got round to writing up my views on Async. In retrospect, I should have been clear that I'm talking specifically about async/await. Not async in general, or the need to support fine-grained concurrency.