【发布时间】:2009-12-04 02:44:08
【问题描述】:
所以我有这个臭方法,两个条件块做几乎完全相同的事情,但参数完全不同(至少在我看来)。我想用鲍勃叔叔的风格清洁它,但我一辈子都想不出一个整洁的方法。所以我来找你,我的书呆子朋友,看看你如何能把它提炼成一种不会让人想挖出他们眼睛的东西。代码是 AS3,但在我看来这并没有什么不同。
/**
* 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;
}
【问题讨论】:
标签: oop refactoring coding-style