I often come across this problem.
Say to have a form with some controls:
A
B
C = A + B
D
E = C * 2 ONLY IF D==TRUE
F
G = A - F
The user fills A, then B.
The system calculates C.
The user fills D (boolean)
The system calcluates E
The user fills F
The system calculates G
But now:
The user clears A
The system clears C, E and G
The user clears B
The system does nothing
The user inserts A
The system recalculates only F
And so on and on.
There clearly is a tree of dependences, and that's not difficult to understand.
But pragmatically i found quite difficult implement it without any 'framework' to model it.
Be them inputs in a HTML form or controls in a javafx application, the problem of creating a comprehensive 'recalculation' algorithm still remains.
(Javafx offers a 'binding' architecture that despite being a bit verbose, yet is very powerfull and addresses (part of) this specific issue.)
But i'm more interested in a methodology that can be used across the different platforms.
I use to use a bunch of tecniques to solve the issues that come across but not yet a methodology, that is a way to go from the definition to the implementation with the minimum amount of trials.
The tecniques are the good old: invalidations, lazy init, wrapping each control in a 'node' of a recalc-graph.
But i feel that there should be a more canonical approach to this recurrent problem, and a paper where this approach is explained and so on.
Can anyone suggest me where can i find such information?
EDIT 1: terms of the problem
- One of the key goals of such a solution is to have a way to organize the code in a predictable way, so that, knowing the method, it should be easy to evaluate its correctness.
- Initial state: many system fail to return to their initial state, that's the smell of a poor design that such methodology should prevent
- It should be designed for real cases such as (hitting back to the example): when the user changes A, the system should clear B in the case in which they were coupled. It's easy to think of such cases: a user selects a contry and the system provides a list of regions for the user to choose one. If the user changes the city, the region previously chosen has to be cleared even if it's not strictly a dependent field since it's not calculated by the system.
As an example of a very partial solution:
Each control should have a backing 'update' method that should trigger only update of 'directly connected' controls. This hypothesis should guarantee that when C is based on A, A is always up-to-date.
function update_A() { update_C; }
function update_B() { update_C; }
function update_C() { C.setValue(A + B); update_E(); }
etc. etc.
since there mustn't be circular references, these update function should form a 'tree'.
Other contributes to come.