<p>I've started refactoring some legacy code recently and came across two functions for drawing a coordinate grid, the problem is that these functions differ only in orthogonal variables they treat, something like that</p>

<pre><code>void DrawScaleX(HDC dc, int step, int x0, int x1, int y0, int y1)
{
    for(int x = x0; x &lt; x1; x += step)
    {
         MoveToEx(dc, x, y0, NULL);
         LineTo(dc, x, y1);
    }
}
void DrawScaleY(HDC dc, int step, int x0, int x1, int y0, int y1)
{
    for(int y = y0; y &lt; y1; y += step)
    {
         MoveToEx(dc, x0, y, NULL);
         LineTo(dc, x1, y);
    }
}
</code></pre>

<p>So if I decide to add some fancy stuff, like antialiasing or merely change drawing pencil or whatever I'll have to put the same code in both of them and it's code duplication and it's bad we all know why.</p>

<p>My question is how would you rewrite these two functions into a single one to avoid this problem?</p>

[{'answer_id': 80722, 'author': 'Serge', 'author_id': 1007, 'author_profile': 'https://Stackoverflow.com/users/1007', 'pm_score': 0, 'selected': False, 'text': "<p>Here is my own solution</p>\n\n<pre><code>\nclass CoordGenerator\n{\npublic:\n    CoordGenerator(int _from, int _to, int _step)\n        :from(_from), to(_to), step(_step), pos(_from){}\n    virtual POINT GetPoint00() const = 0;\n    virtual POINT GetPoint01() const = 0;\n    bool Next()\n        {\n            if(pos > step) return false;\n            pos += step;\n        }\nprotected:\n    int from;\n    int to;\n    int step;\n    int pos;\n};\n\nclass GenX: public CoordGenerator\n{\npublic:\n    GenX(int x0, int x1, int step, int _y0, int _y1)\n        :CoordGenerator(x0, x1, step),y0(_y0), y1(_y1){}\n    virtual POINT GetPoint00() const\n        {\n            const POINT p = {pos, y0};\n            return p;\n        }\n    virtual POINT GetPoint01() const\n        {\n            const POINT p = {pos, y1};\n            return p;\n        }\nprivate:\n    int y0;\n    int y1;\n};\n\nclass GenY: public CoordGenerator\n{\npublic:\n    GenY(int y0, int y1, int step, int _x0, int _x1)\n        :CoordGenerator(y0, y1, step),x0(_x0), x1(_x1){}\n    virtual POINT GetPoint00() const\n        {\n            const POINT p = {x0, pos};\n            return p;\n        }\n    virtual POINT GetPoint01() const\n        {\n            const POINT p = {x1, pos};\n            return p;\n        }\nprivate:\n    int x1;\n    int x0;\n};\n\nvoid DrawScale(HDC dc, CoordGenerator* g)\n{\n    do\n    {\n        POINT p = g->GetPoint00();\n        MoveToEx(dc, p.x, p.y, 0);\n        p = g->GetPoint01();\n        LineTo(dc, p.x, p.y);\n    }while(g->Next());\n}\n</code></pre>\n\n<p>But I it seems to me too complicated for such a tiny problem, so I'm looking forward to still see your solutions.</p>\n"}, {'answer_id': 80750, 'author': 'Matej', 'author_id': 11457, 'author_profile': 'https://Stackoverflow.com/users/11457', 'pm_score': 3, 'selected': False, 'text': '<p>Why you just do not extract the body of the for cycle into a separate function? Then you can do the funny stuff in the extracted function. </p>\n\n<pre><code>void DrawScaleX(HDC dc, int step, int x0, int x1, int y0, int y1)\n{\n    for(int x = x0; x &lt; x1; x += step)\n    {\n        DrawScale(dc, x, y0, x, y1);\n    }\n}\n\nvoid DrawScaleY(HDC dc, int step, int x0, int x1, int y0, int y1)\n{\n    for(int y = y0; y &lt; y1; y += step)\n    {\n        DrawScale(dc, x0, y, x1, y);\n    }\n}\n\nprivate void DrawScale(HDC dc, int x0, int y0, int x1, int y1)\n{\n    //Add funny stuff here\n\n    MoveToEx(dc, x0, y0, NULL);\n    LineTo(dc, x1, y1);\n\n    //Add funny stuff here\n}\n</code></pre>\n'}, {'answer_id': 80755, 'author': 'NeARAZ', 'author_id': 6799, 'author_profile': 'https://Stackoverflow.com/users/6799', 'pm_score': 0, 'selected': False, 'text': '<p>Well, an obvious "solution" would be to make a single function and add one extra parameter (of enum-like type). And then do an if() or switch() inside, and perform the appropriate actions. Because hey, the <em>functionality</em> of the functions is different, so you have to do those different actions <em>somewhere</em>.</p>\n\n<p>However, this adds runtime complexity (check things at runtime) in a place that could be just better checked at compile time.</p>\n\n<p>I don\'t understand what\'s the problem in adding extra parameters in the future in both (or more functions). It goes like this:</p>\n\n<ol>\n<li>add more parameters to all functions</li>\n<li>compile your code, it won\'t compile in a bunch of places because it does not pass new parameters.</li>\n<li>fix all places that call those functions by passing new parameters.</li>\n<li>profit! :)</li>\n</ol>\n\n<p>If it\'s C++, of course you could make the function be a template, and instead adding an extra parameter, you add a template parameter, and then specialize template implementations to do different things. But this is just obfuscating the point, in my opinion. Code becomes harder to understand, and the process of extending it with more parameters is still <em>exactly</em> the same:</p>\n\n<ol>\n<li>add extra parameters</li>\n<li>compile code, it won\'t compile in a bunch of places</li>\n<li>fix all places that call that function</li>\n</ol>\n\n<p>So you\'ve won nothing, but made code harder to understand. Not a worthy goal, IMO.</p>\n'}, {'answer_id': 80760, 'author': 'Douglas Leeder', 'author_id': 3978, 'author_profile': 'https://Stackoverflow.com/users/3978', 'pm_score': 0, 'selected': False, 'text': "<p>I think I'd move:</p>\n\n<pre><code>     MoveToEx(dc, x0, y, NULL);\n     LineTo(dc, x1, y);\n</code></pre>\n\n<p>into their own function DrawLine(x0,y0,x0,y0), which you can call from each of the existing functions.</p>\n\n<p>Then there's one place to add extra drawing effects?</p>\n"}, {'answer_id': 80906, 'author': 'Igor Semenov', 'author_id': 11401, 'author_profile': 'https://Stackoverflow.com/users/11401', 'pm_score': 0, 'selected': False, 'text': '<p>A little templates... :)</p>\n\n<pre><code>void DrawLine(HDC dc, int x0, int y0, int x0, int x1)\n{\n    // anti-aliasing stuff\n    MoveToEx(dc, x0, y0, NULL);\n    LineTo(dc, x1, y1);\n}\n\nstruct DrawBinderX\n{\n    DrawBinderX(int y0, int y1) : y0_(y0), y1_(y1) {}\n\n    void operator()(HDC dc, int i)\n    {\n        DrawLine(dc, i, y0_, i, y1_);\n    }\n\nprivate:\n    int y0_;\n    int y1_;\n\n};\n\nstruct DrawBinderY\n{\n    DrawBinderX(int x0, int x1) : x0_(x0), x1_(x1) {}\n\n    void operator()(HDC dc, int i)\n    {\n        DrawLine(dc, x0_, i, x1_, i);\n    }\n\nprivate:\n    int x0_;\n    int x1_;\n\n};\n\ntemplate&lt; class Drawer &gt;\nvoid DrawScale(Drawer drawer, HDC dc, int from, int to, int step)\n{\n    for (int i = from; i &lt; to; i += step)\n    {\n         drawer(dc, i);\n    }\n}\n\nvoid DrawScaleX(HDC dc, int step, int x0, int x1, int y0, int y1)\n{\n    DrawBindexX drawer(y0, y1);\n    DrawScale(drawer, dc, x0, x1, step);\n}\n\nvoid DrawScaleY(HDC dc, int step, int x0, int x1, int y0, int y1)\n{\n    DrawBindexY drawer( x0, x1 );\n    DrawScale(drawer, dc, y0, y1, step);\n}\n</code></pre>\n'}, {'answer_id': 81827, 'author': 'ppi', 'author_id': 2044155, 'author_profile': 'https://Stackoverflow.com/users/2044155', 'pm_score': 3, 'selected': True, 'text': '<p>Drawing a line is simply joining two points, and drawing a scaling incrementing (x0,y0) and(x1,y1) in a particular direction, through X, and/or through Y.\nThis boils down to, in the scale case, which direction(s) stepping occurs (maybe both directions for fun).</p>\n\n<pre><code>template&lt; int XIncrement, YIncrement &gt;\nstruct DrawScale\n{\n  void operator()(HDC dc, int step, int x0, int x1, int y0, int y1)\n  {\n    const int deltaX = XIncrement*step;\n    const int deltaY = YIncrement*step;\n    const int ymax = y1;\n    const int xmax = x1;\n    while( x0 &lt; xmax &amp;&amp; y0 &lt; ymax )\n    {\n        MoveToEx(dc, x0, y0, NULL);\n        LineTo(dc, x1, y1);\n        x0 += deltaX;\n        x1 += deltaX;\n        y0 += deltaY;\n        y1 += deltaY;\n    }\n  }\n};\ntypedef DrawScale&lt; 1, 0 &gt; DrawScaleX;\ntypedef DrawScale&lt; 0, 1 &gt; DrawScaleY;\n</code></pre>\n\n<p>The template will do its job: at compile time the compiler will remove all the null statements i.e. deltaX or deltaY is 0 regarding which function is called and half of the code goes away in each functor.</p>\n\n<p>You can add you anti-alias, pencil stuff inside this uniq function and get the code properly generated generated by the compiler.</p>\n\n<p>This is cut and paste on steroids ;-)</p>\n\n<p>-- ppi</p>\n'}]
['https://Stackoverflow.com/questions/80691', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1007/']