Sometimes you just have a list of operations that need to be performed in a set order, like when implementing a sequence diagram. What are the best ways to enforce code execution order, to prevent refactoring introducing subtle bugs through a change of sequence?
Let's assume that existing unit tests would not catch any problems caused by changing the order of the execution of foo() and bar() in the following.
A few of the methods I've seen and used:
Comments (relies on people reading & understanding them):
// do this
foo();
// then this
bar();Fluent grammar (to make the code read more like English and discourage wanton refactoring):
obj
.Do
.foo()
.Then
.bar();State variables & balking (too elaborate):
foo_done=false;
if(foo()) foo_done=true;
if(foo_done) bar(); else throw_an_exception;Grouping logical blocks into functions:
void foo_bar()
{
foo();
bar();
}
...and many more too ugly to describe (nesting, events, arrays of function pointers, naming functions Begin(), Middle() and End()...).
Are there any better-engineered patterns for doing this sort of thing?