Reputation: 23
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:
Second, if there was no natural, the player receives a third card (“draws” or “hits”), or not (“stands pat”), according to the following rule: -The player must draw a card with a hand value of 5 or less, and stand pat with 6 or more. -Third, if there was no natural, the dealer draws or stands pat according to the following rules -If the player did not draw a card, then the dealer follows the same rule as the player: The dealer must draw a card with a hand of 5 or less, and stand pat with 6 or more.
-If the player did draw a third card, the decision is based on the value of that card alone (call it C3), and the value of the dealer’s hand. The dealer must draw a card with a hand value less than or equal to some limit L, where L is calculated as follows:
If C3 is 8 or more, let Y = C3-10, else let Y = C3. So Y will be from -2 to 7.
o To get L, divide Y by 2, truncate the result, and add 3. L will be
from 2 to 6. o The dealer must draw with a hand value of L or
less.
Upvotes: 2
Views: 1341
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
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
Reputation: 272307
Choose the most readable and maintainable solution. Don't worry about optimisation until you can actually demonstrate a performance issue.
Upvotes: 1