Some I'm reviewing some code within my team's code base, where we traverse over one hierarchical data structure and build a new data structure from it. There are no nested loops -- every level of the hierarchy has its own dedicated function.
So we have code like this:
public void DoA(A a, Transform transform)
{
foreach(B b in a)
DoB(b, transform);
}
public void DoB(B b, Transform transform)
{
if (b != null && b.IsAvailable)
return;
foreach(C c in b)
DoC(c, transform)
}
public void DoC(C c, Transform transform)
{
var cAndAHalf = DoCAndAHalf(c.FindAll(x => x.Children > 0);
foreach(D d in cAndAHalf)
DoD(d, transform);
}
. . .
public void DoX(X x, Transform transform)
{
Res res = new Res();
if (x.Selected)
{
res.Selected = true;
res.ResourceCount = 1;
}
transform.Add(res);
}
There are dozens of methods like this, where each method is 3 to 5 lines in length, similarly named, usually contains a trivial null check or filter, and a cursory review of the code shows that no method is really invoked more than once. Methods are public for unit testing purposes.
I personally find it the code very hard to navigate through, because dozens of public methods == dozens of potential entry points into the class guts.
Does this sort of coding pattern have a name? Is it an anti-pattern? Is this style more advantageous than simply nesting the loops in a single function?