|
Casing on input patternsOnly handle very simple things in the dispatcher. The problem with handling stuff in the dispatcher is that it has a very flat structure and typically a lot of different things has to happen. So code that belongs logically together gets spread and pieces of code that basically has very little to do with each other stands next to each other. To avoid this code that belongs logically together should be placed in handling predicates that are grouped in a module so the dispatcher only calls the handling predicates. This way the dispatcher is just that a dispatcher and code that belongs together are placed close together.
The first "rule" is straight forward. The second rule can be illustrated with the following example: clauses qwerty(17, W, E, R, 13, Y) :- ..., % A !, ... % B qwerty(17, W, E, R, 13, Y) :- ..., % C qwerty(Q, W, E, R, 13, Y) :- ... % D ... The clauses above represents bad coding style, because is has two clauses for the same input "pattern". This would have been OK if this was the only input pattern the predicate deals with. But in this case, we have clauses for other patterns as well. I think that the predicate above should have been rewritten, such that the purpose of qwerty is solely to case out on the specific patterns of input leaving other work to sub-predicates: clauses qwerty(17, W, E, R, 13, Y) :- !, % we have cased out, this is one of our cases qwerty_17_w_e_r_13_y(W, E, R, Y). qwerty(Q, W, E, R, 13, Y) :- !, % we have cased out, this is one of our cases qwerty_q_w_e_r_13_y(Q, W, E, R, Y). ... clauses qwerty_17_w_e_r_13_y(W, E, R, Y) :- ..., % A !, ... % B qwerty_17_w_e_r_13_y(W, E, R, Y) :- ... % C clauses qwerty_q_w_e_r_13_y(Q, W, E, R, Y) :- ... % D This code have changed the qwerty predicate into a predicate, which only cases-out on the various combinations of input. It is not essential that you only have a single predicate call for each kind of input as illustrated above. The main point is that you do not back-track from one clause to an other on the same input pattern. This rule does especially apply to event handlers, you should not have more than one clauses for "close" (or anything else) in the same event handler. An event handler often spread on several pages, so if it is not a rule that each case is handled by a single clause, then you will have to look at all clauses to be sure that you know how a single input pattern is handled.
|