Join 3,416 readers in helping fund MetaFilter (Hide)


Handling complex business logic without if-then-else
September 22, 2012 2:15 PM   Subscribe

I'm working on a web app (PHP) that generates a personalized health guide based on a few user choices. The logic behind the generator is complex enough that if-then-else statements would get too nested, having too many conditions and difficult to maintain. I've been reading about rules engines, DSLs, karnaugh maps, finite-state machine, decision trees, rete networks and behavior trees. In addition to being utterly confused, I'm unsure which of these is the best approach for my app. Which one do you recommend?

The guide focuses on a very narrow aspect of health so we're not talking about a super advanced expert system.

It's important that the logic can easily be modified because currently most of it is guesswork and will change a lot.
posted by Foci for Analysis to Computers & Internet (4 answers total) 2 users marked this as a favorite
 
It's hard to say without knowing more about the form the logic currently takes and what sort of output you need. I guess I would recommend trying a few different approaches to see A) how naturally your information fits into each and B) how easily maintained and updated it will be. You might be able to eliminate several approaches just by imagining how they'll work for you, then focus on those that remain.

Of those you've mentioned: I don't think Karnaugh maps will provide much benefit (they're primarily for minimizing Boolean functions); a domain specific language is likely more complex than you need; and finite state machines probably don't fit the problem well (they're more for computing a state over a varying-length sequence of inputs, and you might end up with something that's basically a decision tree, anyway).

A decision tree is probably the simplest of those options, so I'd see if you can fit your problem into that form, first. As a bonus, it looks like there is plenty of code for implementing decision trees in PHP out there already. If decision trees won't work, Rete networks could be a good second choice, as they are more complex to implement but can probably represent more complex systems more easily.
posted by whatnotever at 2:56 PM on September 22, 2012 [2 favorites]


It's hard to say without knowing more about the form the logic currently takes and what sort of output you need.

The logic is based on five questions - gender, age, health issues, etc - that the user answers. Four are multiple choice and one is multiple response. The generator takes the input and creates a guide that is unique on 3-4 levels from 'approach' level (there are four of these) down to certain paragraphs and graphics. Example of the logic:
if a=male and b=teen and c=foo|bar and d!=baz, the most likely approach is alpha.

For approach beta, if d=qux|quv and e=3, display paragraph "Lorem ipsum..."

Does this make any sense?
posted by Foci for Analysis at 3:31 PM on September 22, 2012


Split your logic affecting your model from your logic controlling your presentation-

> if a=male and b=teen and c=foo|bar and d!=baz, the most likely approach is alpha.

This is code that can be used to manipulate your model of the user's state.

> For approach beta, if d=qux|quv and e=3, display paragraph "Lorem ipsum..."

This is code that controls the presentation of that information.

I'd suggest looking at cake or codeigniter, and try to think about your problem in classic MVC terms- you should be able to isolate the two kinds of rules from each other pretty well, which will simplify your logic.
posted by jenkinsEar at 3:44 PM on September 22, 2012


If your concern is clarity of thought, instead of nesting your qualifying statements, list them one after another, and preface each with "if ( $myBoolean && some new criteria )". You boolean value is then set to true by default, and false if some criteria along the way hasn't been met.

Basically, you list out your criteria in stages: If beta is only reached after 3 or 4 criteria are met, then you could have those 3 or 4 criteria met in a series of steps, rather than nested statements.

I've found that when you get into sticky situations like this, it's easier to maintain code if it's spaced out like you're explaining it to a 2-year old. That way, when your befuddled mind is forced to return to that code a year later, when your boss says something like "What? I didn't mean X = Y, I meant the other thing! You know what I mean!"), you're not further exasperated by your own code.
posted by thanotopsis at 9:36 PM on September 22, 2012 [1 favorite]


« Older Sexual desire: how does it wor...   |  What is your favorite legendar... Newer »
This thread is closed to new comments.