What are the pros and cons of putting conditions in the preamble or body of a rule?
Now for rulebooks where only one rule will be run, or where you would like to rely on Inform sorting the rules for you obviously putting conditions in the preamble is advantageous. But is there any real difference for rulebooks when you’re going to run every rule anyway, and it doesn’t matter what order they run in?
[code]A standard AI target selection rule for the player:
increase the Weight by 1;
A standard AI target selection rule for a person (called target):
if the target is the player:
increase the Weight by 1;[/code]
It helps with debugging. Rule tracing can show you whether every rule that you expect to fire actually does. Plus, if there’s a problem, if it’s one giant mass of nested conditionals, you’ll have to put your own debugging code in to figure out exactly where it’s going wrong.
A series of small self-contained rules also tends to be much easier to read, understand, and scroll through at a glance – a significant issue as a project grows in length and complexity. It basically boils down to ease of use for you as the coder and maintainer.
You’re describing the situation where you put several stanzas of code into one giant rule (with a different condition on each).
I think the question is about having several rules, but putting the condition inside the body rather than on the rule header.
The only difference there is debugging. “Rules all” will show every rule as running, and every rule will look like “target selection rule for a person” in the debugging output.
The non-debugging behavior is exactly the same. It looks like the execution will be slightly slower, because it winds up checking “x ofclass person” before “x == player”, rather than the other way around. But this is not significant until you’re executing the rulebook many times per turn.
I award 1 point to Zarf for using the phrase “stanzas of code”.
Not original to me, at all. Nor is it original to the natural-language programming model. I think I first saw it in reference to C.
I’ve noticed that the preambles often generate I6 code that does a lot of unnecessary testing… it seems to always test for class even if an adjective makes that superfluous.