-2

I have two coding styles:

public class Age
{
    public double AgeYear { get;}
    public double AgeInDays {get;}
    public Person GetAge()
    {
      int x;
      //implementation that calculates AgeYear and AgeInDays

      return this;
    }
}

Age a = new Age();
Age a1 = a.GetAge();

Second implementation:

public class Age
{
    public double Age { get; set; }
    public double AgeInDays { get;set; }

    public void CalculateAge()
    {
      int x;
      //implementation that calculates Age and AgeInDays
      Age = x;
      AgeInDays = Age * 365;
    }
}

Age a = new Age();
a.CalculateAge();
double age = a.Age;
double ageInDays = a.AgeInDays;

This is just a simple example. What do you prefer - change object properties in void method and then expose public properties or call some method that returns object itself.

  • 2
    In your first example, don't a and a1 refer to the same object? – Dan Pichelman Mar 22 '16 at 21:33
  • 1
    For this particular example, the obvious answer is that you should be doing these calculations in the constructor and not require an additional method call at all, regardless of how that additional method works. I'm guessing that's not the kind of answer you're after though (although, as you can see, that is the kind of answer you've gotten so far). Also note that we don't do opinion polls here; we'd prefer an example that's sufficiently realistic that we could give you well-reasoned arguments why one style or another would be more appropriate. – Ixrec Mar 22 '16 at 21:45
  • Might want to make your "CalculateAge" function private, then you could call it when you get the age or as @Ixrec says "doing these calculations in the constructor" (if you only do the calculation once). – Snoop Mar 23 '16 at 11:25
4

You should use neither of those two styles!

Both ways you have it, your object is often in a questionable state, and it isn't at all obvious that it is. Often, you should strive to follow the Principle Of Least Astonishment.

Whenever you have get properties, accessing those should almost always be a legitimate operation with expected results. Returning garbage until some other function is called is unexpected.

If calculating the age is an expensive operation, then you should have neither of the age properties, and you should return those via the CalculateAge() method, either in two different methods or with another class to carry their computed values. In both cases they should be named as such that it is clear the function is doing some lifting and not just spitting out a saved variables value.

If calculating the age is cheap, then calculate it on the spot when the property is accessed. In neither case should you depend on having some other function set it up for you.

Getters are expected to be fast, cheap, and safe operations. If the values they are providing access to are not always in a valid state, you shouldn't have them.

  • 1
    +1!!! After construction, an object should be ready to use, ready to work, fully initialized, valid, with all its invariants fulfilled and enforced. Ideally, it should be immutable after construction. – Jörg W Mittag Mar 22 '16 at 22:06
  • "class to carry ther computed values" - do you mean some kind of model with necessary properties, DTO class is it? – user2457382 Mar 23 '16 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.