I have some loops inside of each other. In the most inner loop I calculate values that will be needed outside of the loops later.
public class Foo
{
public void Bar()
{
...
double baz;
var i = 0;
while (i < 100)
{
...
var j = 0;
while (j < 100)
{
...
baz = DoSomething(i, j);
j ;
}
i ;
}
// some more calculations. helper1 and helper2 with only the
// the last values of i and j are needed and I do not want
// to calculate them again
}
private double DoSomething(int i, int j)
{
// many lines of code with complex calculations
var helper1 = i j;
var helper2 = 2*i j;
baz = helper1 helper2;
return baz;
}
}
Option 1: I could calculate helper1 and helper2 again but this is time consuming and not necessary.
Option 2: I could pass out the values with out
.
Option 3: I could create a static class with static properties where I store the values in. Inside Loop 2: StaticClass.MoreValues1 = bar;
...
All three options do not seem good practice to me. Is there a better way?
Many thanks
Edit: Updated code block for clarification.
Edit: Added dependency of i and j to helper1 and helper2
CodePudding user response:
Based on the edits and comments:
- "The helpers are different for each call. I only need the last values."
- "Even also only the last baz."
I'd refactor like so:
public class Foo
{
public void Bar()
{
...
double lastBaz;
int helper1;
int helper2;
var i = 0;
while (i < 100)
{
...
var j = 0;
while (j < 100)
{
...
lastBaz = DoSomething(i, j, out helper1, out helper2);
j ;
}
i ;
}
// some more calculations. helper1 and helper2 with only the
// the last values of i and j are needed and I do not want
// to calculate them again
}
private double DoSomething(int i, int j, out int helper1, out int helper2)
{
// many lines of code with complex calculations
helper1 = i j;
helper2 = 2*i j;
baz = helper1 helper2;
return baz;
}
}
That would be for a first step. I'd probably look over this again to see if I can tweak it to be more efficient (memory/exec time) and readable.
Probably, I'd switch to a struct to return all three values instead of using out params.
public class Foo
{
public void Bar()
{
...
BazResult lastResult;
var i = 0;
while (i < 100)
{
...
var j = 0;
while (j < 100)
{
...
lastResultz = DoSomething(i, j);
j ;
}
i ;
}
// some more calculations. helper1 and helper2 with only the
// the last values of i and j are needed and I do not want
// to calculate them again
// use lastResult.Baz
// use lastResult.Helper1
// use lastResult.Helper2
}
private BazResult DoSomething(int i, int j)
{
// many lines of code with complex calculations
helper1 = i j;
helper2 = 2*i j;
baz = helper1 helper2;
return new BazResult(baz, helper1, helper2);
}
}
public struct BazResult
{
public readonly double Baz;
public readonly int Helper1;
public readonly int Helper2;
public BazResult(double baz, int h1, int h2)
{
Baz = baz;
Helper1 = h1;
Helper2 = h2;
}
}
CodePudding user response:
From your example it seems like you will at worse do the same work twice (recalculating MoreValues1, 2, ...). This is not normally a performance concern because complexity (big O) won't change. Option 1 is fine but if you want it to be cleaner you can improve it by using out as you already mentioned in Option 2. Option 3 will also improve performance but your static variable will continue to be stored after the call and will increase you apps memory usage. I'd only use this if the outer loop will be called again in the future.