How to design this program with easy modification in mind?
<p>This question is about how to design a program so it will be easy to make certain modifications.</p>

<p>I have a class which holds some (non-trivial) data and has several member functions that change this data.</p>

<p>Sometimes I need to compute some property of this data.  But it is slow to recompute it from scratch on every change.  It is much faster to compute a small update to these properties instead.</p>

<p>I have several such properties which I need to be able to easily add or remove to/from my class (or turn on/off) to carry out some numerical experiments.  The class is only modified by myself and is used for numerical simulations (scientific code).</p>

<h3>Concrete example</h3>

<p>Let's say I have a class that holds a number <code>x</code>.  But I also need <code>2^x</code> (a "property" of <code>x</code>).  The basic class is:</p>

<pre><code>class C {
    double x;

public:
    C() : x(0.0) 
    { }

    void inc() { x += 1; } 
    void dec() { x -= 1; } 
    void set(double x_) { x = x_; } 
};
</code></pre>

<p>But now I need to keep track of <code>2^x</code> and to keep updating this value every time <code>x</code> changes.  So I end up with</p>

<pre><code>class expC {
    double expx;

public:        
    expC(const double &amp;x) {
        recompute(x);
    }

    void inc() { expx *= 2; } // fast incremental change
    void dec() { expx /= 2; } // fast incremental change
    void recompute(const double &amp;x) {
        expx = std::pow(2, x); // slow recomputation from scratch
    }
};


class C {
    double x;

    expC prop1; // XX

public:
    C() : x(0.0), prop1(x) // XX 
    { }

    void inc() { 
        x += 1;
        prop1.inc(); // XX 
    }
    void dec() { 
        x -= 1; 
        prop1.dec(); // XX
    }
    void set(double x_) { 
        x = x_;
        prop1.recompute(x); // XX
    }
};
</code></pre>

<p><code>XX</code> marks changes I needed to make to the class <code>C</code>.  That's a lot of changes, which is error prone.  It becomes even more complicated with several properties, which my even depend on each other.</p>

<pre><code>class C {
    double x;

    expC  prop1; // XX
    someC prop2; // XX

public:
    C() : x(0.0), prop1(x), prop2(x, prop1) // XX 
    { }

    void inc() { 
        x += 1;
        prop1.inc(); // XX 
        prop2.inc(); // XX 
    }
    void dec() { 
        x -= 1; 
        prop1.dec(); // XX
        prop2.dec(); // XX
    }
    void set(double x_) { 
        x = x_;
        prop1.recompute(x); // XX
        prop2.recompute(x, prop1); // XX
    }
};
</code></pre>

<p><strong>Question:</strong> What is a good design for such a program?  I'm sure it's possible to do better than the above.  The goals are: 1) Make it easy to add/remove such properties or turn on/off their computation 2) Performance is critical.  <code>inc</code> and <code>dec</code> are called in tight inner loops and do relatively little. They cannot be made virtual for performance reasons.</p>

<p>In reality <code>x</code> is a more complex data structure.  Think e.g. adding/removing edges to/from a <a href="https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)" rel="nofollow">graph</a> and keeping track of its degree sequence during the process.</p>

<hr>

<p><strong>Update</strong></p>

<p>@tobi303 asked that I show how this class would be used.  It's in a manner similar to this:</p>

<pre><code>void simulate(C &amp;c) {
    for (/* lots of iterations */) {
        c.inc();
        double p1 = c.prop1.value();
        double p2 = c.prop2.value();
        if (condition(p1,p2))
            c.dec();
    }
}
</code></pre>

<p>Or in words:</p>

<ul>
<li>make a (random) change</li>
<li>get property values after the change</li>
<li>depending on the new property values, decide whether to accept or undo the change.</li>
</ul>

<p>It's actually a Monte-Carlo simulation similar to a <a href="https://en.wikipedia.org/wiki/Metropolis%E2%80%93Hastings_algorithm" rel="nofollow">Metropolis-Hasting algorithm</a>.</p>

<p>A concrete example could be where the "data" in class <code>C</code> (state) is the spin state of an <a href="https://en.wikipedia.org/wiki/Ising_model" rel="nofollow">Ising model</a> (for those familiar with it) and properties are the total energy and total magnetization of the system.  These are much faster to update after a single spin flip than to recompute from scratch.  In practice I don't have an Ising model, I have something a bit more complicated.  I have several properties, some fast to compute and some slow (actually I have some auxiliary data structures that help compute properties).  I need to experiment with combinations of different properties, so I often change what I include in the code.  Sometimes I implement new properties.  When I don't need an already implemented property, I need to be able to turn off its computation for performance reasons (some are really slow to compute).</p>