# Chained conditional statements or table?

The post title is probably confusing, but I couldn’t think of how to express the idea concisely. At any rate, take the following situation. Let’s say I have an NPC, and that NPC has a value that keeps track of his mood. When the player interacts with the NPC, something should happen based on the mood of the NPC and certain other factors over which the player has control. I can think of two ways to do this: 1) With a string of conditional statements or 2) with a table.

``````(Rule triggered by interacting with NPC):
if the NPC is happy:
say "appropriate response [if condition 1]altered for PC condition one[otherwise]altered for PC condition two[end if].";
say "appropriate response [if condition 1]altered for PC condition one[otherwise]altered for PC condition two[end if].";
if the NPC is angry:
say "appropriate response [if condition 1]altered for PC condition one[otherwise]altered for PC condition two[end if].";
if the NPC is pensive:
say "appropriate response [if condition 1]altered for PC condition one[otherwise]altered for PC condition two[end if].";
if the NPC is lonely:
say "appropriate response [if condition 1]altered for PC condition one[otherwise]altered for PC condition two[end if]."``````
``````(Rule triggered by interacting with NPC):
let E be the state of the NPC;
if there is a Reaction corresponding to an Emotion of E in the Table of NPC Reactions:
say "[Reaction corresponding to an Emotion of E in the Table of NPC Reactions]".

Table of NPC Reactions
Emotion	Reaction
happy	"appropriate response [if condition 1]altered for PC condition one[otherwise]altered for PC condition two[end if]."
sad	"appropriate response [if condition 1]altered for PC condition one[otherwise]altered for PC condition two[end if]."
angry	"appropriate response [if condition 1]altered for PC condition one[otherwise]altered for PC condition two[end if]."
pensive	"appropriate response [if condition 1]altered for PC condition one[otherwise]altered for PC condition two[end if]."
lonely	"appropriate response [if condition 1]altered for PC condition one[otherwise]altered for PC condition two[end if]."``````

So, my question is, which of these is better? Although I suppose “better” is rather vague. The second one looks more elegant, although the first one is actually much shorter in terms of the length of code.

I have a number of such code blocks in my game right now, all of which I wrote using chained conditionals as in #1. I had the idea that I would rewrite all of these to be table-based, but after rewriting one I’m not sure that it is better. They both work fine, of course, and I imagine that any difference in performance would be minuscule. I guess I’m just curious how other people handle this sort of situation, what the opinions are on best practices, etc.

I would go with option 1, except I would go one better and make each conditional a rule in and of itself:

[code]
Rule triggered by interacting with happy NPC:
say “appropriate response [if condition 1]altered for PC condition one[otherwise]altered for PC condition two[end if].”;

Rule triggered by interacting with sad NPC:
say “appropriate response [if condition 1]altered for PC condition one[otherwise]altered for PC condition two[end if].”;

Rule triggered by interacting with angry NPC:
say “appropriate response [if condition 1]altered for PC condition one[otherwise]altered for PC condition two[end if].”;

etc.[/code]

I am NOT an expert on memory usage or code efficiency by any means, but purely in terms of keeping your code readable, modifiable, and extendable, rules are a better choice than tables. The standard rulebooks used throughout Inform 7 are all basically massive lists of conditionals already, so yours are not really adding much to the pile.

Interesting. I hadn’t thought about making separate rules for each conditional. Some of the situations I’m dealing with are a lot more complex than what I posted above, so I’d have to do a little experimentation, but it’s something worth looking into. Thanks.

Seconding the use of rules. They can really help readability when you have a lot of conditions involved, and Inform will try to keep them in the right order (so that specific will always come before general).

Good point about the automatic ordering. That is definitely a point in favor of using rules. I think I will indeed try to rework my test block using rules. I have to admit that I have been a little bit wary of having so many rules flying about. I think part of it is that I need to clean up my code so that the rules are located where I can find them. I started off with a pretty good system and a clearly defined structure, but as things have become more complex I think they have also become a bit messier.

Thank you both for giving me a lot to think about, and for possibly helping me to uncover a problem that I wasn’t aware I had!

I agree with the other posters about using rules, but just for the record, you can use a special format to make the chained conditionals easier to read:

```if the NPC is: -- happy: say "appropriate response [if condition 1]altered for PC condition one[otherwise]altered for PC condition two[end if]."; -- sad: say "appropriate response [if condition 1]altered for PC condition one[otherwise]altered for PC condition two[end if]."; -- angry: say "appropriate response [if condition 1]altered for PC condition one[otherwise]altered for PC condition two[end if]."; -- pensive: say "appropriate response [if condition 1]altered for PC condition one[otherwise]altered for PC condition two[end if]."; -- lonely: say "appropriate response [if condition 1]altered for PC condition one[otherwise]altered for PC condition two[end if]."```

See §11.8 of Writing with Inform.

Thanks, matt! I was actually going to ask if Inform had a “switch” statement or something of that variety. I went through the manual but couldn’t remember seeing anything like that. Don’t know how I missed that.