简体   繁体   English

重新构造foreach循环并继续吗?

[英]Refactor the foreach loop with continue in it?

I was trying to refactor a very long foreach loop. 我试图重构一个很长的foreach循环。 I got stuck as at few places I am having continue in it. 我陷入困境,因为在我continue进行的几个地方。 Here is some dummy code to mimic my original code 这是一些模仿原始代码的虚拟代码

IEnumerable<dynamic> allData = FetchAllData();
IEnumerable<dynamic> relativeData = FetchAllRelativData();

foreach (var rdata in relativeData)
{
    IEnumerable<dynamic> dataTobeProcessed = allData.Where(c => c.Name = rdata.Name);

    //Do something

    //if then Continue

    // do something

    //if then continue

    // do something

    // do something

    // add data to db
}

Here I have remove all the do something part in to separate function. 在这里,我删除了所有分开的功能。 But again I am having few code portion having continue in it, that is breaking loop and moving forward to next element. 但同样我有几个有代码部分continue在里面,那就是打破循环,并前进到下一个元素。 So, I was unable to separate that part. 所以,我无法将那部分分开。

Again, here I am having constraint like steps are in continuation. 再次,在这里,我有限制,像步骤是继续。 Means first thing happens then second one and then third one then next... So, I can't move the position of code as of now. 意味着第一件事发生在第二件,然后是第三件,然后是下一件......所以,我现在无法移动代码的位置。 Because of that my function got big, around 300 lines and it is now getting hard to change or maintain. 因为我的功能变得很大,大约300行,现在变得很难改变或维护。

Please provide a good solution for removing continue or another way to break method by changing the way of coding. 请提供一个很好的解决方案,通过改变编码方式来删除continue或其他方法来break方法。

Please let me know if any other details are needed. 如果需要任何其他细节,请告诉我。

Just refactor the if blocks into separate functions with appropriate names. 只需将if块重构为具有适当名称的单独函数即可。
Then put all the ifs inside eachother instead of the continue. 然后将所有ifs放在另一个而不是继续。

if (!something) 
{
  DoFirstThing();
  if (!otherthing)
  {
     DoSechondThing();
  }
} 
//continue implicitly happens here anyway.

At the start of the loop, set a boolean - let's call it escape to false.. Now replace all of your existing continue s with an assignment of escape to true. 在循环的开始,设置一个布尔-我们称之为escape假。现在替换所有现有的continue s的赋值escape为true。 This, by itself, isn't a true refactoring - it changes the behavior of the existing code - but we are only partway done. 这本身并不是真正的重构 - 它改变了现有代码的行为 - 但我们只是在中途完成。 Now select the body of the loop (now with no continue s), and extract it as a method. 现在选择循环体(现在没有continue s),并将其作为方法提取。 In the extracted method, replace each assignment of escape with a return statement. 在提取的方法中,使用return语句替换每个escape赋值。 Eliminate the original creation of the escape variable, and your code is in a state that is easier to further refactor. 消除escape变量的原始创建,并且您的代码处于更容易进一步重构的状态。

You dont mention whether the if implementation of your DoSomething() calls share args and return types, if they do, an alternative option would be to create a collection of Funcs containing your DoSomething() logic, and instead of lots of if statements you could just loop over the Func implementations until the return value dictates you should break. 你没有提到你的DoSomething()调用的if实现是否共享args和返回类型, 如果他们这样做,另一个选择是创建一个包含你的DoSomething()逻辑的Func集合,而不是你可以使用很多if语句只需遍历Func实现,直到返回值指示您应该中断。

var somethingFuncs = new List<Func<x, bool>> 
{
  DoSomething,
  DoSomethingB,
  Blah....
};

foreach(var something in somethingFuncs)
{
  var result = something(arg);

  if (result)
  {
    break;
  } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM