I have the following doubt.
For refactoring, I have read that is good create methods that has a very specific responsability, so if it is possible, it is a good idea to split a complex method in others small methods.
But imagine that I have this case:
I have to create a list of objects, and insdie this objects, I have to create another object. Something like that:
public void myComplexMethod(List<MyTypeA> paramObjectsA)
{
foreach(MyTypeA iteratorA in paramObjectsA)
{
//Create myObjectB of type B
//Create myObjectC of type C
myObjectB.MyPorpertyTpyeC = myObjectC;
}
}
I can split this method in two methods.
public void myMethodCreateB(List<MyTypeA> paramObjectsA)
{
foreach(MyTypeA iteratorA in paramObjectsA)
{
//Create myObjectB of type B
}
}
public void myMethodCreateB(List<MyTypeB> paramObjectsB)
{
foreach(MyTypeB iteratorB in paramObjectsB)
{
//Create myObjectC of type C
iteratorB.PropertyC = myObjectC;
}
}
In the second option, when I use two methods instead one, the unit tests are less complex, but the problem is that I use two foreach loops, so it is less efficient than use only one loop like in the first option.
So, what is the best practice, at least in general, to use a method a bit more complex to be more efficient or to use more methods?
Thanks so much.