This happens to me every once in a while and I always end up solving it the same way and then wishing for a cleaner way.
I start out with a calls to related utility functions, followed by and update call.
SynchA();
SynchB();
UpdateLastTime();
Then later I add check boxes so I have:
if(synchA.Checked)
{
SynchA();
}
if(synchB.Checked)
{
SynchB();
}
BUT now I only want to call UpdateLastTime() of ONE OR BOTH the two executed so invariably I end up with:
bool synchHappened = false;
if(synchA.Checked)
{
SynchA();
synchHappened = true;
}
if(synchB.Checked)
{
SynchB();
synchHappened = true;
}
if(synchHappened)
{
UpdateLastTime();
}
That final step always bothers me because I’m spreading this one bool around to three branches of logic.
Is there some obvious better approach to the above logic/scenario that I could use?
The main goal would be – each time as logic has changed – code should be affected at least as possible.
So you’ve to structure such things once and then it will work for you.
In your particular case I would suggest to Keep It Simple (no Strategy Pattern, and so on), so
extract and encapsulate a logic of switches into the properties. So each time as requirements will be changed – you’ve to update either logic of particular switch or main logic itself.
Switches with encapsulated rules:
Main logic: