Please help me with the following code refactoring, I spent so much time researching it and there's still no clear answer on how to approach it. Any help is greatly appreciated!
So, given 2 methods (C#):
private void func1()
{
int i1 = 0;
bool b1 = false
foreach (Object item in List<Object>)
{
if (!b1)
{
if (i1 == 0)
doCommonJob1();
else
b1 = true;
doCommonJob2();
}
// --- LOGIC 1 (could be whatever here)
if (i1 == 10)
{
doSpecificJob1()
}
// ---
doCommonJob3();
i1++;
}
}
And
private void func2()
{
int i1 = 0;
bool b1 = false
foreach (Object item in List<Object>)
{
if (!b1)
{
if (i1 == 0)
doCommonJob1();
else
b1 = true;
doCommonJob2();
}
// --- LOGIC 2 (could be whatever here)
if (b1 == true)
{
doSpecificJob2()
doSpecificJob3()
}
doSpecificJob4()
// ---
doCommonJob3();
i1++;
}
}
There's so much duplication between them with some specific-to-method logic in the middle of a foreach loop.
It obviously smells, but making one method out of them and adding a boolean to the method's params would go against the single responsibility principle.
Any ideas on how to reuse the common code?
Thank you in advance.