3

I have a complex object which routinely needs to compute a sub-object representing various aspects of the parent's state as a bundle. For example, imagine the object represents information about an aircraft and the sub-object is a summary of various key parameters of the aircraft at some particular time such as its heading, speed, etc. The object needs to store the last computed version of the sub-oject.

Currently the way I do this is that I have a void method in the object that does the computation and then sets a module-level variable to be equal to the newly computed sub-oject. The outside user of this sub-object then retrieves the current sub-oject via a getter. So, for the client methods the operation looks like this:

main_object.computeState();
State new_state = main_object.getState();

To handle possible errors, however, in the computation, which is currently not done, I am thinking of changing to a compute method that returns the state:

State new_state = main_object.computeState( error_msg );
if( new_state == null ){
   print( error_msg );
   goto failure continuation
}
.... [everything ok, continue normally ]

Is there a better strategy for constructing this pattern?

3
  • Just to double-check I'm understanding: the class needs the sub-object internally to implement its own behavior, but there are also callers of the class who need the same sub-object? Also, is there any operation that either the class or another caller would do which uses the state but where they can be sure they don't want to recompute it? Sep 8, 2015 at 15:24
  • The class stores the sub-object internally because many calls might be made asking for the sub-object. If the sub-object is up to date then it does not need to be recomputed, but the current one is just returned. This current sub-object is stored in the main object. Sep 8, 2015 at 15:43
  • 1
    The clients/callers do not know whether the state needs to be recomputed, so (in the current implementation), they always ask to recompute it. Internally, the computation method, if it detects that the current state is valid and does not need to do an update, it does nothing further and just returns. Sep 8, 2015 at 15:46

2 Answers 2

3

What you are suggesting is IMHO pretty straightforward, I just would consider to use exceptions for error handling if your programming language allows this. For example:

 try
 {
     State new_state = main_object.computeState();
     .... [everything ok, continue normally ]
 }
 catch(MyException ex)
 {
    print( ex.error_msg );
    ... do further error processing
 }
3

So, for the client methods the operation looks like this:

main_object.computeState();
State new_state = main_object.getState();

This is a bad pattern. You are exposing all the client code to an implementation detail of main_object. This complicates the client code, complicates future optimizations of main_object, and will lead to bugs when some programmer calls getState() without calling computeState().

You should provide a single method of main_object that clients can call to get the current state. That method can call computeState if necessary. Sometime in the future you may find that it is not necessary to call computeState for every call to getState.

The getState() method should take care of calling computeState whenever necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.