6

How much logic is too much for a property getter? For example, I have code which looks like this:

        public double value
        {
            get
            {
                if (condition1
                    || condition2
                    || condition3
                    || condition4)
                {
                    _value = anotherValue;
                }
                return _value;
            }
        }

I have read various posts that say the code inside properties should not be expensive. But the term 'expensive' is a bit vague for me.

d219
  • 2,707
  • 5
  • 31
  • 36
Jan Paolo Go
  • 5,842
  • 4
  • 22
  • 50

3 Answers3

5

Generally speaking, in OOP a getter (setters too) should be a trivial operation. What you've posted above is trivial, depending on what the conditions actually are.

You mentioned that "expensive" is a vague term to you. An operation being computationally expensive means that it will take a long time to complete (usually because it has to complete a lot of computations -- that's an oversimplification, but a decent approximation). For instance, consider:

if (a == 5
    || b == true
    || c == "FOO!"
    || d == 3.14159)
{
    _value = anotherValue;
}
return _value;

In this example, the conditions are trivial, and your program will crunch through this block near-instantaneously. On the other hand:

if (some_slow_function())
{
    _value = anotherValue;
}
return _value;

Assuming some_slow_function does actually run slowly, this block will take a long time to return _value, making it no longer trivial. If this getter is called often, some_slow_function will be called often as well, causing it to bottleneck your program and cause it to run slowly.

Sebastian Lenartowicz
  • 4,695
  • 4
  • 28
  • 39
  • the conditions will take some (boolean) fields from another class. will that still be trivial? – Jan Paolo Go Apr 04 '16 at 13:52
  • @PaoloGo: Assuming that the getters those fields use aren't computationally expensive either, then you're good. – Sebastian Lenartowicz Apr 04 '16 at 13:53
  • Alright. Thanks! I tried testing it with mock classes and the if block doesn't affect the performance of the getter. I guess it will all come down to the implementation of the getter of the fields on the actual 'another' class. – Jan Paolo Go Apr 04 '16 at 13:58
2

As long as there are no waits or changes in values logic can be used. You should also care on not going with too much exceptions. General ones are okay, custom ones are too much.

Getters are meant to work instant. With this point there is your limit of logic. Stay safe it works instant, then its okay.

C4d
  • 3,183
  • 4
  • 29
  • 50
2

The MSDN says:

Properties are members that provide a flexible mechanism to read, write, or compute the values of private fields. Properties can be used as though they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily while still providing the safety and flexibility of methods.

So as long as your Property getter is idempotent and performing fast(i.e, not creating any performance bottleneck) it is fine.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331