bumblebee
bumblebee

Reputation: 23

Decision Table or if-else statement

I need to know if its better for me to make Decision Table or if-else statement. if decision table is better for the rules below please tell me what are the basic codes I need to make it. I have tried making it using if-else statement but the output is not what i want. This question is for baccarat game, and here are the rules:

Upvotes: 2

Views: 1341

Answers (3)

Adrian Shum
Adrian Shum

Reputation: 40036

From your comments, you have mentioned using decision table is better because there are lots of rules.

It is right in certain extend but not really true.

From what you described, there are lot of rules for different purpose. No matter you are writing decision tables or hand craft if-else, you still need to properly organize the rules base on their usage and context, and separate the "business flow" to make use of those rules.

If you failed to do so, using decision table is even harder to code and harder to read.

When you are considering use of decision table, which implies you are going for a rule engine, readability is probably not the most important factor. Main reason for using rule engine is to facilitate change of rule in the future. If you don't foresee such need, I would strongly suggest "hand-crafting" the logic, with a properly structured code.

Upvotes: 1

fkl
fkl

Reputation: 5535

In principle, you should use a decision table / switch etc. (if you are worried about efficient processing through the cases) once your if-else-if clauses go up to 5 or more (just a value big enough to impact average access times) instances.

Update: 5 is not written any where. I used it to illustrate a concept though i remember having seen some compiler doing it when switch cases were 8 or more, but that was a long time ago

The rationale is that going through each of the if clause manually would cause linear over head where as for decision table, the access would be constant time.

In practice, your code is already optimized by every decent compiler into a decision table / hash table as soon as number of cases become significant so this is irrelevant.

Following would help. Note that choice of switch or decision table or if else also depends upon how your test clause is structured. Switch works on integer values only. If you cannot relate your test condition to be accessed randomly some how, you might not have another choice but to use if-else only.

Upvotes: 2

Brian Agnew
Brian Agnew

Reputation: 272307

Choose the most readable and maintainable solution. Don't worry about optimisation until you can actually demonstrate a performance issue.

Upvotes: 1

Related Questions