The Latest Static PageThere is a website design paradigm that is very much in vogue at the moment. It is called Model-View-Controller, or "MVC" for short. I've come up against it in a PHP website where I work as well as starting to participate in discussions on Stack Overflow. And, once again, it seems too many programmers not really worthy of the term have taken a good-sounding idea and run off with it. In the wrong direction.
The idea of Model-View-Controller predates the web, but the unique characteristics of programming for the web has given it a new audience. I'm not going to go into all the gory details of it, but the basic idea is that a page request is at first handled by the Controller which calls the appropriate Model, returning information to present to the View which is then rendered to the browser. Sounds simple, doesn't it? And it sounds very attractive to programmers of languages like PHP which does not enforce a framework, because you don't have to solve all those problems yourself.
PHP programming depresses me from time to time, which is kind of odd because I like the language and what it is capable of. What depresses me is that most of the PHP code I encounter has been written by - at best - mediocre programmers. By definition, "mediocre programmers" do not fully understand the concepts of abstraction and modularity. They copy code from one spot and modify it slightly for another location, instead of putting the common algorithm behind even a function so it can be re-used. Or they do that to the first level and end up with functions that carry long, complex parameter lists. Or multiple functions that do the same thing in slightly different ways. There is rarely organisation of included files, or worse, there are multiple attempts at organisation, none of which seem to have been completed. Class libraries often confuse is-a with has-a and otherwise nifty features are overused for no good reason. Code often makes assumptions about where it is in the filesystem, or worse, what domain name it is being served under. Hard-coded SQL is everywhere and often closely tied to the underlying version of the database being used. And don't turn Notices on, or the code will just fall over.
And then you see the efforts of mediocre programmers at implementing MVC. *shudder*
One of the discussions I'm trying to encourage on Stack Overflow is about data abstraction. In a previous job, before I truly encountered MVC, I rewrote the database handling aspect of a very busy intranet application and accompanying website. This highly reliable and complex code presented the application layer with fully parameterised objects that knew how to do their own retrieval and saving. It also seamlessly supported database replication and partitioning. In addition, almost all SQL was generated by the core object handler. The application didn't need to worry about SQL or where the database was. Or what the database was.
Unfortunately, an awful lot of PHP code does not do this. I'm finding that this sadly includes a lot of MVC advocates, too. To my way of thinking, how the web application processes data and how this data is stored and retrieved are two separate problems. This is in addition to the other two concepts of MVC: data direction and data display. So how do you fit four concepts in three locations?
It appears I am not alone in thinking this. Mike Seth and (perhaps) Ian P Christiansen think along the same lines. Here is a blog entry detailing this. In it, Mike talks about how so many frameworks do stupid things, like turn the Model into the Data access layer (so where do you put your application code?), or require the Controller to know what the View is doing. All three often have knowledge about each other and that puts you back to square one: PHP code that thinks it's structured to a well-known and well-respected pattern... but clearly isn't.
I've long shunned the PEAR codebase for the appallingly mediocre quality of the code. PHP may be mediocre, frustrating, bizarre or all of those at any number of things, but one thing I am sure that it does well: and that is get too many people programming beyond their ability.
Wade.