HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Storing values or calculating values

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
valuescalculatingstoring

Problem

I am trying to decide between two designs of a class. The basic difference is the one calculates properties when they are called, and the other when the object is instantiated.

I would usually just take the first one, but the calculations call each other, so when calling all of them, they get recalculated multiple times.

I, however, still prefer the first code. It is cleaner. Easier, for me, to read. I also feel the calculations are probably small enough to not worry about as opposed to storing extra values.

Is there anything else I should be considering, or looking out for? Or any reason one is amazingly better than the other?

public class Angle1
{
    private decimal angle;

    public int Degree => (int)Math.Floor(angle);
    private decimal MinuteSecond => (angle - Degree) * 60;
    public int Minute => (int)Math.Floor(MinuteSecond);
    public decimal Second => (MinuteSecond - Minute) * 60;

    public Angle1(decimal angle)
    {
        this.angle = angle;
    }
}

public class Angle2
{
    private decimal angle;

    public int Degree { get; private set; }
    public int Minute { get; private set; }
    public decimal Second { get; private set; }

    public Angle2(decimal angle)
    {
        this.angle = angle;
        Degree = (int)Math.Floor(angle);
        var minSec = (angle - Degree) * 60;
        Minute = (int)Math.Floor(minSec);
        Second = (minSec - Minute) * 60;
    }
}


I have also thought of a "hybrid" option. Store values in private values. When a property is called the first time, the value is calculated, and the private value is set. When it is called again, the private value is returned.

This hybrid option is the one I naturally go to when properties are larger objects, datasets, or binary data. I feel in this instance, it would "dirty" the code more than necessary, for the size of the stored values (integers and decimals).

Solution

I'm leaning toward Angle2...


I also feel the calculations are probably small enough to not worry about

I infer that doing the calcs in the constructor is trivial as well - no problematic user-perceived lag, hesitation, slowness.

Communicating through design

How would I interpret - make an initial guess about - design, intent, and use from what I see here?

-
... so when calling all of them, they get recalculated multiple times.

  • By doing something so obviously avoidable, there must be a reason. Somewhere.



-
Lazy loading, generally, suggests

  • the operation is time, CPU, whatever consuming to some degree



  • doing it all at once certainly is an issue



  • instantiation and/or re-calculation are not intrinsically inter-dependent.



  • angle is being mutated because there is no logic avoiding re-calc on every call.



  • an object is in a valid state with all, some, or none of the calculations done.



  • Client doesn't need guard code all over the place



  • Client does not have to know any particular call order.



  • If the client must know call order & all the involved properties to do it, why is Angle1 even a class?



-
Doing it all in the constructor suggests

  • This object is immutable



  • I'm guaranteeing valid state.



  • angle is not mutated



  • client code will be just that much cleaner, easier




however, still prefer the first code. It is cleaner. Easier, for me, to read.

You should write code that is "cleaner, and easier" to use.

Conclusion: Angle2 is better.

Context

StackExchange Code Review Q#146157, answer score: 5

Revisions (0)

No revisions yet.