Bruce Fitzsimons (bwooce) wrote,
Bruce Fitzsimons

  • Mood:

Rules for C++ config reading

If you're going to make objects read or accept config then the following best practice applies...

* Make it reconfigurable at the outset
Its much easier than retrofitting it later, honest.

* Make the ctor use the reconfigure method directly
Then the code is concentrated in one place, and the reconfigure method is generally more complicated as its dealing with existing state. I've had so many leaks/cores from forgetting or overdeleting objects after adding in reconfiguration. Do it right the first time.

* Make the reconfigure method only start using the new config if it was all sane and relevant
It makes sense that it shouldn't crash, as thats the whole intent of making it runtime reconfigurable. This is really hard to do later, as by that stage you've got config you can't copy/move around from the temp space to permanent privates.

* Make the reconfigure method throw a specific bad config exception
This caters for the bad config for the ctor and reconfigure cases. In the ctor case the parent can die after printing/rethrowing the nice error message you've put in the exception), in the reconfigure case the parent can print/syslog/rethrow and continue running. You're giving the parent the choice, at least, about what is most appropriate
  • Post a new comment


    Comments allowed for friends only

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded