patterncsharpModerate
Loops for removing unnecessary lines
Viewed 0 times
unnecessaryremovingloopsforlines
Problem
I have the following method:
As you can see, there is a lot of iteration and casting. Is there a simple way to improve that?
private void removeUnnecessaryLines(List list)
{
List remove = new List();
foreach (Line line in lines)
{
SourceFile destination = (line.Tag as Call).getCallee();
foreach (ScatterViewItem svi in list)
{
SourceFile test = svi.Tag as SourceFile;
if (test.Equals(destination))
{
remove.Add(line);
}
}
}
foreach (Line l in remove)
{
lines.Remove(l);
Dependencies.Children.Remove(l);
}
}As you can see, there is a lot of iteration and casting. Is there a simple way to improve that?
Solution
The most obvious improvement would be to use LINQ's
You can also use the
Where method to filter the lines list instead of building up a list of to-be-deleted items and then deleting them. In addition to being easier to read and understand it will have the benefit of its runtime not being quadratic in the length of lines.You can also use the
List.RemoveAll method which also takes a predicate like Where (though the predicate specifies the items to be removed, so it's in a way the opposite of Where), but modifies the list in-place, like your solution did.Context
StackExchange Code Review Q#462, answer score: 13
Revisions (0)
No revisions yet.