[Chicago-talk] design opinions

Jim Thomason thomasoniii at gmail.com
Thu Aug 12 11:45:05 CDT 2004

I'm sure I get groans when I send out one of my requests for design
debate, but I'll do it again. It helps me to type things out and think
about them, at a bare minimum.

I've been working around with some old code we have and it's just been
driving me nuts. It's effectively a giant switch statement. "If state
is this, do that."; "if state is this, do that", "If state is this, do
that"; and then it all falls through down to the bottom for final
output and display (usually).

A lot CGIs/web based things tend to look like this. Most controllers
in other MVC arenas don't, because the application is effectively
constantly preserving state, it knows where it's supposed to be, and
knows how to deal with it.

But those big huge gigantic if/then/else ladders grate on me. I know
they shouldn't, I know they're normal, but they're just aesthetically
repulsive to me. So I started monkeying around with replacements and
dusted off the state machine code I wrote a few months back.

Opting not to tackle too gigantic a problem at first, I ripped apart
one of the small cgis on my own website - the one governing editing
movie reviews.

So I have the original one:


and the new one:


Neither of which are particularly commented, but that's initentional
at the moment. I'm curious to see how people feel just looking at the
code and trying to figure out what's going on. I may have a few typos
in the machine one, since I haven't tested it yet, so if something
looks odd, it's probably just be screwing up.

Presently, the linear version looks easier to me. It's simpler to
write, it's easier to follow, it's easier to understand. But the
machine version seems to capture the essence of the program much
better and may end up looking more clear when scaled up to a larger
script. It's tougher to follow it, since it's not top down and it
bounces around a lot. Still, the fact that it seems closer to what the
program does implies to me that it'll get much easier to build and
maintain state machines like this once it's comfortable enough. New
design patterns always have a bit of a learning curve, after all.
Program sizes appear to be comparable.

I'm also even thinking that at least some of the coding could be done
graphically with an ERD-style tool. Define the states, define the
transisition values, generate a bunch of perl code and then fill in
the details. I haven't decided if I'd like that or not.

Opinions welcome, as well as suggestions about other patterns that may
have worked well for you guys in the past.


More information about the Chicago-talk mailing list