Is there a better (more concise) way of iterating over the same collections objects in an if/else condition than the following:
bool condition = DetermineConditionValue();
if(condition)
{
foreach(var v in variables)
{
PerformAction(v);
}
else
{
foreach(var v in variables)
{
PerformAnotherAction(v);
}
}
Is there a better way to avoid writing the loop twice?
if
statement inside yourforeach
loop, but really I wouldn't get too concerned. As long as the code seems logical when you read it.if
inside the loop? Two words: Branch prediction. Okay, two more words: Speculative execution. Also, you would need to put the performance cost ofif
in relation to the performance cost of executing Perform(Another)Action, which probably alone makes the whole performance cost ofif
inside the loop insignificant...