I am working with a type that passes state through instance variables. So you’ll see methods like so:
public MyType MyMethod()
{
DoThisMethod();
DoThatMethod();
AndDoThis();
return _bleh;
}
Is this a recognised approach?
It’s a little disconcerting working with this code because if you don’t understand the code fully, the instance variable might be transformed without your knowledge by another method. If instead state was passed via method parameters, then you could be very confident of the value of the parameter passsed in.
If the order of those method calls is important I would call this “Bad Programming” – as you’ve said any method that requires an earlier call should be using method parameters that are indicative of this.
I’m pretty sure Code Complete gives some great examples of this approach.
Basically something like the following, where each method requires the previous invocation’s result.
Secondly, I like to keep methods “orthogonal” as much as possible (that is they depend only on the state that you provide them among other things). For a great summary on Orthogonal Code see this article.