Wednesday 17 December 2008

Problems with the Model-View-Controller pattern

Malcolm Tredinnick wrote an interesting article Defying Classification (Hat tip: Slashdot).

While he never gets to a useful conclusion in terms of providing alternatives it certainly jibes with my experience where everything in a GUI gets turned into MVC even when it really shouldn't.

The common issue I see is that the business logic gets put into the controller. This often because developers think that the controller has to do something. The issue here is that the model becomes anaemic and has no reason to exist other than holding data. In my opinion (and nowadays this is a little controversial) objects should always have data and behaviour.

It's interesting to note that MVC comes from the old SmallTalk days where the code to run the GUI element itself was written in SmallTalk and was significantly complex. i.e. For a listbox, say, the SmallTalk code would have to draw the listbox line by line and then update it based on mouse events and other actions. In this context you can see the value of MVC.

Unfortunately, now that browsers or the OS draws the GUI element for us it's not so important.