This is an explanation and a confession. For those that don't know, a Model View Controller is a software design pattern. It has there parts, a Model controls the data and logic, a View draws the data to the screen and a Controller links the two together. The benefits of the MVC pattern is that it neatly compartmentalises the different bits, making the code quicker and easier to maintain. There are loads of other benefits but I'll leave those to the links.
Two years ago, when I started writing Chom Isis 3, I had no idea how this might work in PHP. I'd seen it in Java and .NET, both catering for MVC really well. Yesterday at lunchtime, I saw this article at PHP Patterns (site colours are a little sickening but the content is great). I gave it a read and then idly dismissed it. Chom 3 is written in a semi-MVC way anyway. Not very neat but neat enough.
It bugged me most of the afternoon as I knew there were going to be tricky bits comming up that would mean I'd have to refactor (coding speak for going back and changing for the better) some of the big classes: Pawn, Alliances and Player. Last night, after bass practise (for the coming weekend), I sat down and had a bit of a play. In short order, I managed to get the database system up and running and the skinning done. Some code can move across, some will need modification but I am beginning to think I'm going to need to refactor a lot.
Not only that but I started writing Unit tests. A unit test is a bit of code that calls and tests a very specific piece of code. I've got a couple in Chom Isis but not nearly the coverage I should have. By putting in a Unit Test system (easier with MVC), it means that I can be sure that changes I have elsewhere in the system will not effect anything else.
The upshot of all this is that there is a lot of coding to be done without any new functionality. The more I think about it, the more obvious the move is. I had to stop Chom Isis 2 because the code was minging. A spagetti of file-driven lunacy with HUGE amounts of code repetition. I can see Chom 3 going that way if I don't nip it in the bud. I know it smacks of Raymond Loewy and 'Never Leaving Well Enough Alone'. It's the endless cycle of never finishing anything because of not being happy with it. I can see why you might think that. I've got really close to putting a Beta-able game together (thanks to the Alpha testers) and there I go changing everything again. I did this for the skin and I think most people will agree that the new skin (which took me many man-days of work) is far superior to the old Icar blue.
There is another good reason for changing over to MVC and that's the Chom Isis Application Programming Interface. I've had the design in my "Chom Design Notebook" for some time. It would be a series of web services that would serve XML to other applications, even hosted on other servers. The data that would be available would be the same as in game but it would allow other code savvy tinkerers to make their own tools to get at the engine. You could do the same by logging in through an HTTP object of some kind and then screen scraping but the API would be a much better way of doing it. I've been writing APIs at work and have a good hold on how to write a good one. If you wanted, for example, to set up an application that published the message board to twitter, you could. If you wanted to create an application that automated some tasks for you, you could. It's matter for another post entirely but the most important thing is MVC allows me to create a structure that really facilitates this API.
The last reason for doing this is that you should never really write for yourself but write for the person who comes after you. I'm not suggesting that I'm going to take on other developers in the future but I like to think that I might be able to share the code in the future, I wouldn't want to do this in its current state, massively difficult to understand, I'd imagine.
So, there we have it. I've got a lot of work ahead of me but it's less of a head scratching 'how will it work' and more of a outright conversion from A to B.
Comments
As long as the release doesn't clash with Duke Nukem Forever I don't mind ;)