是否可以减少这种长嵌套的 for 循环和 if 条件以增加其可读性并对其进行优化以供将来参考。
在为我的日程安排应用程序编写代码时,我最终得到了如下所示的方法。真的,我有这样的数据结构。在这里,我检查 - 是否有任何阶段(在 LCycle 内部)同时使用相同的工具,如果发现,LCycleTimeShift
则调用另一种方法进行重新排列。
但我想检查新的排列是否具有适应性,并重置 for 循环计数器,以便再次检查新的排列。我认为这不是编写代码以提高可读性的更好方法。对该主题的一些研究发现枚举器可以在这方面帮助我。但我不知道如何使用以下代码完成此操作。
public List<LCycle> ToolArrangment(List<LCycle> TimeLineInit)
{
for (int i = 0; i < TimeLineInit.Count; i++)//Each LIfeCycles In TimeLine
{
for (int j = 0; j < TimeLineInit[i].Stage.Count; j++)//Each Stages inTimeLine
{
for (int k = 0; k < i; k++)//Each L esvd =ifeCycles Upto Current LifeCycle
{
for (int l = 0; l < TimeLineInit[k].Stage.Count; l++)//Each Stages of (LifeCycle upto current LifeCycle)
{
for (int m = 0; m < TimeLineInit[i].Stage[j].ToolList.Count; m++)//each tools in stage of timelkine
{
for (int n = 0; n < TimeLineInit[k].Stage[l].ToolList.Count;n++ )// Each tools In that stage (for loop outer of outer)
{
if (TimeLineInit[i].Stage[j].ToolList[m].ToolName == TimeLineInit[k].Stage[l].ToolList[n].ToolName)//If both tools are same (satidfying above for loop conditions)
{
if (IsTimeOverLaps(TimeLineInit[i].Stage[j].StageSpan, TimeLineInit[k].Stage[l].StageSpan))
{//tool using at same time.
Stage ReplaceStage = TimeLineInit[i].Stage[j].DeepCopy();//Taking Copy of stage Span to make time shift
Double TimeDifference=(ReplaceStage.StageSpan.ToTime-ReplaceStage.StageSpan.FromTime).TotalMinutes;//Calculating required time shift
ReplaceStage.StageSpan.FromTime=TimeLineInit[k].Stage[l].StageSpan.ToTime;//FromTime changed accordingly
ReplaceStage.StageSpan.ToTime=ReplaceStage.StageSpan.ToTime.AddMinutes(TimeDifference);//To Time Changed accordingly
LCycleTimeShift(TimeLineInit[i], ReplaceStage);//passing refernce
j = 0; k = 0; l = 0; m = 0; n = 0;//Counter Reset to validate the new arrangment
}
}
}
}
}
}
}
}
return TimeLineInit;
}