Work on UVFood - Refactor, Refactor, Refactor

I haven’t done much with UVFood in the last few weeks. Partly because we were traveling for two weeks, but we’ve been back for a while now.

The main reason is that I’ve been doing the dreaded refactoring of the code.

I’ve used Catalyst as the framework for building the web app. Catalyst apps use the Model-View-Controller (MVC) paradigm, and application builders often fall into the trap of having too thin a Model and putting too much of the work into the Controller.

The authors of Catalyst are very clear about this: put your business logic in the model. Don’t do any work in the controller that isn’t part of getting things set up for the View. Your business logic in your model should be enough so that you could build a command-line version of you app that doesn’t use the Controllers or the Views.

Many web applications are simply a thin veneer of web interface over a SQL database, though, and many Catalyst apps simply use the Model to get access to the SQL database and put the work in the Controller.

UVFood isn’t my first Catalyst app, but it’s easily the biggest one I’ve written. My first Catalyst app was a rewrite of some home automation code I use myself and have never shared. It’s wretched code and it’s very application-specific. I did something even worse in it than put the business logic in the Controller; I put it in the View! I barely used Models at all in this app and it’s really horrible… lots of duplicated code, lots of failures that can happen in the wrong place. It was a quick and dirty and it’s an awful Catalyst application that would earn me a sound thumping were I to publish it.

Someday I’ll clean it up, if only for my own sake.

UVFood is much better than that, but the current code still falls into the classic trap of having too much of the stuff that does the work live in the Controller.

I finally hit the wall on that. I had too many places where the same thing needed to be done by different handlers inside my Controllers. I’ve structured the underlying database so that I can easily reuse data structures in places that make sense - for instance, when you write a review, it’s using a generalized “Comment” table to store your review. I have generalized HTML::Mason components for writing and displaying comments (reviews). So as I expand UVFood to allow you to, say, comment on a photo or a menu, I don’t have to re-implement all that code.

Except - I do have to re-implement some of it, because too much of the logic that supports it is in the Controller. So I have to (1) copy the code, (2) move it into a set of separate subroutines or (3) move it into the Model.

(1) is out of the question. You never, ever want to replicate your code. This way lies madness, yadda yadda.

(2) is better, but not compelling. It gets messy fast. There’s usually a right place for this code to live, especially in an object-oriented environment. And that’s not as a set of subroutines hanging off in space, in an ever-growing mess of a package that holds all this random stuff.

(3) is great, though. It’s difficult because I haven’t done it before. But it’s the right thing to do. Things just go together properly. Subroutines become methods on the objects they’re affecting and everything is easier to use.

So, I’ve created a new model - BFRCMS - in recognition of the fact that UVFood is a Content Management System, albeit specialized, and I’m moving UVFood’s business logic into BFRCMS and out of its Controllers.

The bits of code I’ve done so far are already much cleaner. It’s slow work for me, though, because I’m also learning about more arcane bits of DBIx::Class and working out ways to do more object-oriented work so that I can properly encapsulate some of what UVFood does.

Then there’s the testing. UVFood doesn’t have a large user base yet, but that doesn’t mean I want to deploy untested code that will turn away the users who are coming to it.

So it will be a while longer.

I’ll publish an article here in more detail about the refactoring I’ve done after I’ve got a better grip on it.

And UVFood will get a nice update with a site news blog, photo uploads for users, better user pages with tags and user info, and the ability to set users as friends and follow what they’re doing. Oh yes - and menus.

Tags: , , , , , , , ,

Email This Post Email This Post | Print This Post Print This Post

Comments are closed.

Sponsored Links