Oop 重构一个包含条件语句的方法,该方法具有完全不同的相同代码块;)
所以我有一个糟糕的方法,两个条件块做几乎完全相同的事情,但参数完全不同(至少在我看来)。我想像鲍勃叔叔那样打扫,但我一辈子都想不出一个整洁的方式来做。所以我来找你们,我的书呆子朋友们,看看你们如何把这些东西提炼出来,而不是让人想挖出他们的眼睛。代码是AS3,但在我看来这并没有什么区别Oop 重构一个包含条件语句的方法,该方法具有完全不同的相同代码块;),oop,refactoring,coding-style,Oop,Refactoring,Coding Style,所以我有一个糟糕的方法,两个条件块做几乎完全相同的事情,但参数完全不同(至少在我看来)。我想像鲍勃叔叔那样打扫,但我一辈子都想不出一个整洁的方式来做。所以我来找你们,我的书呆子朋友们,看看你们如何把这些东西提炼出来,而不是让人想挖出他们的眼睛。代码是AS3,但在我看来这并没有什么区别 /** * Splits this group into two groups based on the intersection of the group * with another group. The
/**
* 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;
}
根据需要重命名内容
/**
* 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;
numberOfCellsToSplit = (directionToMoveSplitCells == "RIGHT" ? (endIndex - onGroup.startIndex) : (onGroup.endIndex - startIndex)) + 1;
splitStartIndex = directionToMoveSplitCells == "RIGHT" ? (length - numberOfCellsToSplit) : 0;
splitCells = trimCells(splitStartIndex, numberOfCellsToSplit);
resultingGroupStartIndex = directionToMoveSplitCells == "RIGHT" ? (onGroup.startIndex - splitCells.length) : (onGroup.endIndex + 1);
if (splitCells.length > 0)
{
newGroup = row.createGroup(splitCells, resultingGroupStartIndex)
newGroup.nextGroup = nextGroup; //not sure how to not set this from jump
newGroup.previousGroup = previousGroup; //not sure how to not set this from jump
if (newGroup.previousGroup){
newGroup.previousGroup.nextGroup = newGroup;
previousGroup = newGroup;
var newX:int = (onGroup.endIndex + 1) * cellSize.width;
x = newX;
}
if (newGroup.nextGroup) newGroup.nextGroup.previousGroup = newGroup;
else{
newGroup.nextGroup = this;
newGroup.previousGroup = this;
nextGroup = newGroup;
}
}
removeArrayOfCellsFromGroup(splitCells);
row.joinGroups();
row.updateGroupIndices();
repositionCellsInGroup();
return newGroup;
}
根据需要重命名内容。这就是我所想到的一切。链接列表实际上是一个单独的细节。。。所以也许它可以被重构
/**
* 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;
numberOfCellsToSplit = (directionToMoveSplitCells == "RIGHT" ? (endIndex - onGroup.startIndex) : (onGroup.endIndex - startIndex)) + 1;
splitStartIndex = directionToMoveSplitCells == "RIGHT" ? (length - numberOfCellsToSplit) : 0;
splitCells = trimCells(splitStartIndex, numberOfCellsToSplit);
resultingGroupStartIndex = directionToMoveSplitCells == "RIGHT" ? (onGroup.startIndex - splitCells.length) : (onGroup.endIndex + 1);
if (splitCells.length > 0)
{
newGroup = row.createGroup(splitCells, resultingGroupStartIndex)
newGroup.nextGroup = nextGroup; //not sure how to not set this from jump
newGroup.previousGroup = previousGroup; //not sure how to not set this from jump
if (newGroup.previousGroup){
newGroup.previousGroup.nextGroup = newGroup;
previousGroup = newGroup;
var newX:int = (onGroup.endIndex + 1) * cellSize.width;
x = newX;
}
if (newGroup.nextGroup) newGroup.nextGroup.previousGroup = newGroup;
else{
newGroup.nextGroup = this;
newGroup.previousGroup = this;
nextGroup = newGroup;
}
}
removeArrayOfCellsFromGroup(splitCells);
row.joinGroups();
row.updateGroupIndices();
repositionCellsInGroup();
return newGroup;
}
public function split(onGroup:CellGroup, directionToMoveSplitCells:String):CellGroup
{
if (!hasIntersection(onGroup))
return this;
valr splitCells:Array;
var newGroup:CellGroup ;
var numberOfCellsToSplit:int;
var splitStartIndex:int;
var resultingGroupStartIndex:int;
if (directionToMoveSplitCells == "RIGHT")
{
numberOfCellsToSplit = this.endIndex - onGroup.startIndex + 1;
splitStartIndex = this.length - numberOfCellsToSplit;
splitCells = trimCells(splitStartIndex, numberOfCellsToSplit);
resultingGroupStartIndex = onGroup.endIndex + 1;
if (splitCells.length > 0)
{
newGroup = row.createGroup(splitCells, resultingGroupStartIndex);
nextGroup=insertGroup(newGroup,this,nextGroup);
}
}
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);
previousGroup=insertGroup(newGroup,previousGroup,this);
var newX:int = (onGroup.endIndex + 1) * cellSize.width;
x = newX;
}
}
removeArrayOfCellsFromGroup(splitCells);
row.joinGroups();
row.updateGroupIndices();
repositionCellsInGroup();
return newGroup;
}
private function insertGroup(toInsert:CellGroup,prior:CellGroup,next:CellGroup):CellGroup
{
toInsert.nextGroup = next;
toInsert.previousGroup = prior;
if (toInsert.nextGroup )
toInsert.nextGroup.previousGroup = toInsert;
if (toInsert.previousGroup )
toInsert.previousGroup.nextGroup = toInsert;
return toInsert;
}
我对splitCells Assignment的取消识别是指它是块中的inoly非条件行。
我看了Anon的建议,但我看不到任何方法可以让代码变得更好。这就是我想到的。链接列表实际上是一个单独的细节。。。所以也许它可以被重构
public function split(onGroup:CellGroup, directionToMoveSplitCells:String):CellGroup
{
if (!hasIntersection(onGroup))
return this;
valr splitCells:Array;
var newGroup:CellGroup ;
var numberOfCellsToSplit:int;
var splitStartIndex:int;
var resultingGroupStartIndex:int;
if (directionToMoveSplitCells == "RIGHT")
{
numberOfCellsToSplit = this.endIndex - onGroup.startIndex + 1;
splitStartIndex = this.length - numberOfCellsToSplit;
splitCells = trimCells(splitStartIndex, numberOfCellsToSplit);
resultingGroupStartIndex = onGroup.endIndex + 1;
if (splitCells.length > 0)
{
newGroup = row.createGroup(splitCells, resultingGroupStartIndex);
nextGroup=insertGroup(newGroup,this,nextGroup);
}
}
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);
previousGroup=insertGroup(newGroup,previousGroup,this);
var newX:int = (onGroup.endIndex + 1) * cellSize.width;
x = newX;
}
}
removeArrayOfCellsFromGroup(splitCells);
row.joinGroups();
row.updateGroupIndices();
repositionCellsInGroup();
return newGroup;
}
private function insertGroup(toInsert:CellGroup,prior:CellGroup,next:CellGroup):CellGroup
{
toInsert.nextGroup = next;
toInsert.previousGroup = prior;
if (toInsert.nextGroup )
toInsert.nextGroup.previousGroup = toInsert;
if (toInsert.previousGroup )
toInsert.previousGroup.nextGroup = toInsert;
return toInsert;
}
public function split(onGroup:CellGroup, directionToMoveSplitCells:String):CellGroup
{
if (!hasIntersection(onGroup))
return this;
var newGroup:CellGroup;
if (directionToMoveSplitCells == "RIGHT")
{
newGroup = splitGroupAndMoveSplitCellsRight(onGroup);
if (!newGroup)
return;
insertAsNextGroupInLinkedList(newGroup);
}
else
{
newGroup = splitGroupAndMoveSplitCellsLeft(onGroup);
if (!newGroup)
return;
insertAsPreviousGroupInLinkedList(newGroup);
x = (onGroup.endIndex + 1) * cellSize.width;
}
removeArrayOfCellsFromGroup(splitCells);
row.joinGroups();
row.updateGroupIndices();
repositionCellsInGroup();
return newGroup;
}
private function splitGroupAndMoveSplitCellsRight(onGroup:CellGroup):CellGroup
{
var numCellsToSplit:int = endIndex - onGroup.startIndex + 1;
var splitStartIndex:int = length - numberOfCellsToSplit;
var splitCells:Array = trimCells(splitStartIndex, numberOfCellsToSplit);
if (!splitCells.length)
return null;
var resultingGroupStartIndex:int = onGroup.endIndex + 1;
return row.createGroup(splitCells, resultingGroupStartIndex);
}
private function splitGroupAndMoveSplitCellsLeft(onGroup:CellGroup):CellGroup
{
var numCellsToSplit:int = onGroup.endIndex - startIndex + 1;
var splitStartIndex:int = 0;
var splitCells:Array = trimCells(splitStartIndex, numberOfCellsToSplit);
if (!splitCells.length)
return null;
var resultingGroupStartIndex:int = onGroup.startIndex - splitCells.length;
return row.createGroup(splitCells, resultingGroupStartIndex);
}
private function insertAsNextGroupInLinkedList(group:CellGroup):void
{
var currentNextGroup:CellGroup = nextGroup;
if (currentNextGroup)
{
group.nextGroup = currentNextGroup;
currentNextGroup.previousGroup = group;
}
group.previousGroup = this;
nextGroup = group;
}
private function insertAsPreviousGroupInLinkedList(group:CellGroup):void
{
var currentPreviousGroup:CellGroup = previousGroup;
if (currentPreviousGroup)
{
group.previousGroup = currentPreviousGroup;
currentPreviousGroup.nextGroup = group;
}
group.nextGroup = this;
previousGroup = group;
}
我对splitCells Assignment的取消识别是指它是块中的inoly非条件行。
我看了Anon的建议,但我看不出有什么方法可以让代码变得更好。当然,这只是减少行数的一次拙劣尝试,但是伙计,斯塔克把密码塞上去了。我的错误。代码现在已修复。看起来好多了,这似乎没必要。你到底测试了多少次方向?你真的需要做多少次?这完全没有必要。:-)诚然,这仅仅是为了减少台词而做的一次拙劣的尝试,但是meh.:-)伙计,斯塔克把密码塞上去了。我的错误。代码现在已修复。看起来好多了,这似乎没必要。你到底测试了多少次方向?你真的需要做多少次?这完全没有必要。:-)这是一个有点受虐狂的练习,因为您必须假设类中其他方法的功能!这很适合我凌晨4点的时间,因为我睡不着:)自从你提到鲍勃叔叔,我就努力坚持单一责任原则。我明白了,自从我开始编辑我的回复以来,有人指出了分解链表管理代码的方法——绝对正确。此外,我将逻辑分为向左移动和向右移动,但是如果Anon建议的反身性有效,那么这可以变得更加整洁。希望有帮助。我本来打算这样做的,但是方法仍然是几乎相同的代码,这很烦人,但在这种情况下可能是不可避免的。这是一个有点受虐狂的练习,因为你不得不假设类中其他方法的功能!这很适合我凌晨4点的时间,因为我睡不着:)自从你提到鲍勃叔叔,我就努力坚持单一责任原则。我明白了,自从我开始编辑我的回复以来,有人指出了分解链表管理代码的方法——绝对正确。此外,我将逻辑分为向左移动和向右移动,但是如果Anon建议的反身性有效,那么这可以变得更加整洁。希望能有帮助。我本来打算这样做的,但是方法仍然是几乎相同的代码,这很烦人,但在这种情况下可能是不可避免的。
public function split(onGroup:CellGroup, directionToMoveSplitCells:String):CellGroup
{
if (!hasIntersection(onGroup))
return this;
var newGroup:CellGroup;
if (directionToMoveSplitCells == "RIGHT")
{
newGroup = splitGroupAndMoveSplitCellsRight(onGroup);
if (!newGroup)
return;
insertAsNextGroupInLinkedList(newGroup);
}
else
{
newGroup = splitGroupAndMoveSplitCellsLeft(onGroup);
if (!newGroup)
return;
insertAsPreviousGroupInLinkedList(newGroup);
x = (onGroup.endIndex + 1) * cellSize.width;
}
removeArrayOfCellsFromGroup(splitCells);
row.joinGroups();
row.updateGroupIndices();
repositionCellsInGroup();
return newGroup;
}
private function splitGroupAndMoveSplitCellsRight(onGroup:CellGroup):CellGroup
{
var numCellsToSplit:int = endIndex - onGroup.startIndex + 1;
var splitStartIndex:int = length - numberOfCellsToSplit;
var splitCells:Array = trimCells(splitStartIndex, numberOfCellsToSplit);
if (!splitCells.length)
return null;
var resultingGroupStartIndex:int = onGroup.endIndex + 1;
return row.createGroup(splitCells, resultingGroupStartIndex);
}
private function splitGroupAndMoveSplitCellsLeft(onGroup:CellGroup):CellGroup
{
var numCellsToSplit:int = onGroup.endIndex - startIndex + 1;
var splitStartIndex:int = 0;
var splitCells:Array = trimCells(splitStartIndex, numberOfCellsToSplit);
if (!splitCells.length)
return null;
var resultingGroupStartIndex:int = onGroup.startIndex - splitCells.length;
return row.createGroup(splitCells, resultingGroupStartIndex);
}
private function insertAsNextGroupInLinkedList(group:CellGroup):void
{
var currentNextGroup:CellGroup = nextGroup;
if (currentNextGroup)
{
group.nextGroup = currentNextGroup;
currentNextGroup.previousGroup = group;
}
group.previousGroup = this;
nextGroup = group;
}
private function insertAsPreviousGroupInLinkedList(group:CellGroup):void
{
var currentPreviousGroup:CellGroup = previousGroup;
if (currentPreviousGroup)
{
group.previousGroup = currentPreviousGroup;
currentPreviousGroup.nextGroup = group;
}
group.nextGroup = this;
previousGroup = group;
}