So I have this stinky method, the two conditional blocks do almost the exact same thing, but with radically different parameters (at least in my eyes). I want to clean it Uncle Bob style, but can't for the life of me figure out a tidy way of doing it. So I come to you, my fabulous nerd friends, to see how you might distill this down to something that doesn't make one want to gouge out their eyes. The code is AS3, but that doesn't really make a difference in my opinion.
/**
* Splits this group into two groups based on the intersection of the group
* with another group. The group is split in a direction to fill empty
* cells left by the splitting group.
*
* @param onGroup
* @param directionToMoveSplitCells
* @return
*
*/
public function split(onGroup:CellGroup, directionToMoveSplitCells:String):CellGroup
{
if (!hasIntersection(onGroup))
return this;
var numCellsToSplit:int = 0;
var splitCells:Array;
var newGroup:CellGroup;
var numberOfCellsToSplit:int;
var splitStartIndex:int;
var resultingGroupStartIndex:int;
if (directionToMoveSplitCells == "RIGHT")
{
numberOfCellsToSplit = endIndex - onGroup.startIndex + 1;
splitStartIndex = length - numberOfCellsToSplit;
splitCells = trimCells(splitStartIndex, numberOfCellsToSplit);
resultingGroupStartIndex = onGroup.endIndex + 1;
if (splitCells.length > 0)
{
newGroup = row.createGroup(splitCells, resultingGroupStartIndex)
newGroup.nextGroup = nextGroup;
if (newGroup.nextGroup)
newGroup.nextGroup.previousGroup = newGroup;
newGroup.previousGroup = this;
nextGroup = newGroup;
}
}
else
{
numberOfCellsToSplit = onGroup.endIndex - startIndex + 1;
splitStartIndex = 0;
splitCells = trimCells(splitStartIndex, numberOfCellsToSplit);
resultingGroupStartIndex = onGroup.startIndex - splitCells.length;
if (splitCells.length > 0)
{
newGroup = row.createGroup(splitCells, resultingGroupStartIndex)
newGroup.previousGroup = previousGroup;
if (newGroup.previousGroup)
newGroup.previousGroup.nextGroup = newGroup
previousGroup = newGroup;
newGroup.nextGroup = this;
var newX:int = (onGroup.endIndex + 1) * cellSize.width;
x = newX;
}
}
removeArrayOfCellsFromGroup(splitCells);
row.joinGroups();
row.updateGroupIndices();
repositionCellsInGroup();
return newGroup;
}