For some reason the below design doesn't sit well with me, but I'm unable to come up with an articulate reasoning as to why. I would like your help in forming a case against (or for) it.
Say, the design of FooViewModel is as such:
// Some Observables
Observable_1: bool
Observable_2: string;
// --- //
// The method where all the "logic" is dumped
godMethod() {
if (Singleton1.Status1 != DesiredStatus1) {
set_state_1();
return;
}
if (Singleton2.Status2 != DesiredStatus2) {
set_state_2();
return;
}
if (Observable_2.Value != "Desired String") {
set_state_3();
return;
}
set_state4();
}
// --- //
// In case of ANY external or internal event that may need the observables to change, just call the "godMethod"
externalEvent_Singleton1_Status1_Changed += () => {
godMethod();
}
externalEvent_Singleton2_Status2_Changed += () => {
godMethod();
}
Observable_2.Changed += () => {
godMethod();
}
// --- //
// Actual values of the observables are defined here
set_state_1() {
Observable_1 = false;
Observable_2 = "Status 1 is undesirable :(";
}
set_state_2() {
Observable_1 = false;
Observable_2 = "Status 2 is undesirable :(";
}
set_state_3() {
Observable_1 = false;
}
set_state_4() {
Observable_1 = true;
}
// --- //
I guess this design, instead of defining the logic for each observable, instead defines logic for each "state".
- What are some pros/cons of this design?
- What can be some pitfalls of this design?
- Can this become a maintenance nightmare?
My $0.02:
It is very difficult to find out dependencies for the observables by just reading the code:
- Say you want to figure out when the Observable_1 is set false.
- The code would indicate:
- If Status1 is not appropriate, OR
- If Status1 is appropriate AND If Status2 is not appropriate, OR
- If Status1 is appropriate AND If Status2 is appropriate AND Observable_2 is not appropriate
- Instead of merely defining(declaring?) that if any one of Status1, Status2 or Observable_2 is not appropriate.
- Do you see how complicated it can become?
The number of "states" can explode exponentially.
Calling the godMethod after every action just does my head in; but I'm unable to point to any specific principles that this design violates.
I think that this may be the "procedural" way of doing things, and it's my OOP brain complaining; but again, why is this objectively inferior/superior?
Finally, just to add, the arguments that I've heard for this design are:
- You can look at all logic at one place, that make the code more readable.
- Whenever any event occurs, just call the godMethod. This, supposedly, makes the code easier to write and maintain.