Improving code for similar code found
<p>I passed codeclimate to my code, and I obtained the following:</p>

<blockquote>
  <p>Similar code found in 1 other location</p>
</blockquote>

<p>This is my code:</p>

<pre><code>stradd = 'iterable_item_added'
if stradd in ddiff:
    added = ddiff[stradd]
    npos_added = parseRoots(added)
    dics_added = makeAddDicts(localTable, pk, npos_added)
else:
    dics_added = []
strchanged = 'values_changed'
if strchanged in ddiff:
    updated = ddiff[strchanged]
    npos_updated = parseRoots(updated)
    dics_updated = makeUpdatedDicts(localTable, pk, npos_updated)
else:
    dics_updated = []
</code></pre>

<p>Where <code>iterable_item_added</code> and <code>values_changed</code> are repeated. How to change it?</p>

<p>just abstract the parameters and create an helper method:</p>

<pre><code>def testmethod(name,localTable,m,ddiff,pk):
    if name in ddiff:
        npos = parseRoots(ddiff[name])
        rval = m(localTable, pk, npos)
    else:
        rval = []

    return rval
</code></pre>

<p>the call it:</p>

<pre><code>dics_added = testmethod('iterable_item_added',localTable,makeAddDicts,ddiff,pk)
dics_updated = testmethod('values_changed',localTable,makeUpdatedDicts,ddiff,pk)
</code></pre>

<p>note: be careful when factorizing code, you can introduce bugs (and make code better readable :)).</p>

<p>Also: that helper method forces to pass a lot of local variables. Maybe creating an object and member variables would simplify even more.</p>

<p>In that case, it appears to be a bit "overkill" to do that in order to make your review tool shut up.</p>

python optimization code-climate