Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a setter return immediately if assigned the same value?

In classes that implement INotifyPropertyChanged I often see this pattern :

    public string FirstName
    {
        get { return _customer.FirstName; }
        set
        {
            if (value == _customer.FirstName)
                return;

            _customer.FirstName = value;

            base.OnPropertyChanged("FirstName");
        }
    }

Precisely the lines

            if (value == _customer.FirstName)
                return;

are bothering me. I've often did this but I am not that sure it's needed nor good. After all if a caller assigns the very same value I don't want to reassign the field and, especially, notify my subscribers that the property has changed when, semantically it didn't.

Except saving some CPU/RAM/etc by freeing the UI from updating something that will probably look the same on the screen/whatever_medium what do we obtain?

Could some people force a refresh by reassigning the same value on a property (NOT THAT THIS WOULD BE A GOOD PRACTICE HOWEVER)?

1. Should we do it or shouldn't we?

2. Why?

like image 646
Andrei Rînea Avatar asked Apr 12 '10 16:04

Andrei Rînea


People also ask

Can c# properties be private?

Properties can be marked as public , private , protected , internal , protected internal , or private protected . These access modifiers define how users of the class can access the property. The get and set accessors for the same property may have different access modifiers.

What is get and set in c#?

The get method returns the value of the variable name . The set method assigns a value to the name variable. The value keyword represents the value we assign to the property.

How to access property in c#?

Internally, C# properties are special methods called accessors. A C# property have two accessors, get property accessor and set property accessor. A get accessor returns a property value, and a set accessor assigns a new value. The value keyword represents the value of a property.


4 Answers

Yes, you should return immediately when the consumer is setting a property value that is equal to the value which is already being persisted.

First of all, there is no reason to waste any time or resources in the setter of the property - the value is already set so no further action is needed. Also you should never call OnPropertyChanged if the value stored in the property's backing field hasn't changed - the method is intended to be raised when the value has changed not when the property's setter has been called.

All that being said, however - if the setter didn't have a call to OnPropertyChanged I wouldn't have bothered to check the value first. In the case of a simple setter that only sets the backing field's value and nothing else it is actually going to be faster to always the set the value rather than checking first then setting the value. Only use this pattern when the property's setter has additional logic that either shouldn't fire or may incur an unnecessary performance penalty.

like image 111
Andrew Hare Avatar answered Oct 20 '22 15:10

Andrew Hare


Or you could do this:

   set
    {
        if (value != _customer.FirstName)
       {

           _customer.FirstName = value;

          base.OnPropertyChanged("FirstName");
       }
    }

No need for multiple return paths.

To further answer your question, I wouldn't force an update to property if it's being overwritten by the same value. There's really no point, because you're probably not going to get any benefit from it. (I could see an instance where you would want to track each time someone tries to update a value.)

like image 45
kemiller2002 Avatar answered Oct 20 '22 13:10

kemiller2002


The only argument against that pattern (where you return if the value hasn't changed) I can think of is the purist's view that every function should have only one exit. Not being a purist, I don't agree. I see nothing wrong with breaking out if the value hasn't changed, avoiding the notification update.

like image 2
Dave Swersky Avatar answered Oct 20 '22 15:10

Dave Swersky


The only situation when you shouldn't use it is when you know that you can have dirty data on your class, for example on a ORM layer object that might have outdated values compared to the underlying database because of modification by another user.

If that situation doesn't affect you, then cache away!


Edit

I misunderstood your question, as you are talking about setters, not getters.

Similar points apply. If the set operation is a costly one, and is not supposed to have any kind of side effect (it should't! Side effects on setters are <blink>evil</blink>!!1), then it's a valid optimization.

like image 1
Esteban Küber Avatar answered Oct 20 '22 13:10

Esteban Küber