Click here to Skip to main content
15,908,842 members
Articles / Programming Languages / C#

Try Not to Implement Property with Only Set Accessor TIP

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
10 Nov 2011LGPL32 min read 9.4K   3
Try not to implement Property with only set accessor TIP

Again as with my code reviews, one of the things I noticed in many classes is that they all have properties with only set accessors, but either no get accessor at all or with a private get accessor.

Although if you think for a sec, it sounds good to have many times a writable property but not readable. In fact, even I had the same notion many a times while writing code so far. But one thing I realized or learned today that such a feature is really not needed or its kind of not worthy to have in your code. The reason is simple that consumer code using your property can set a value for your private variable, so when he can set a value that means he already knows the value, so why to make your property a read only?

As of now, you may think that “OK? but what if I change this private variable's value once the consumer code sets a value to it”. Let me show that in code:

C#
class CustomClass
{
    private string _someValue;
    
    public string MyProperty
    {
        set
        {
            _someValue = value;
        }
    }
    
    public void SomeMethod()
    {
        if (!string.IsNullOrEmpty(_someValue))
        {
            //Do some operations
           _someValue = string.Concat(_someValue, " some other values");
        }
    }
}

class TestClass
{
    public void TestMethod()
    {
        CustomClass cs = new CustomClass();
        cs.MyProperty = "My values";
        
        cs.SomeMethod();
    }
}

If you are planning to do this, then you are having a flawed design actually. When you are receiving a value from consumer into your class, then you should not change it or use it to store it, rather use other member to store the data. This way, a guy who is having rights to set a data will also have rights to get (read) a data. That way, you shall have a better design implemented. Because if you do not do this, later on, if consumer calls a method which may return this set value along with other data (say as a List<TVal>), then you are giving the consumer code wrong information.

Along with this, I do not see any better security added to your data in your class at all, right? Think for a moment. The only reason you implement properties to have a better security is mostly for not giving writable access. But here in this case, you already gave away the most critical part of the security and now you are trying do a small work in making it secure which is not really useful.

Hope it helps, your comments are welcome.

Happy coding, thanks. :)

Filed under: C#, CodeProject, Dotnet
Tagged: .NET, blog, C#, codeproject, Dotnet, tips

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Siemens
India India
A .net developer since 4+ years, wild, curious and adventurous nerd.

Loves Trekking/Hiking, animals and nature.

A FOSS/Linux maniac by default Wink | ;)

An MVP aspirant and loves blogging -> https://adventurouszen.wordpress.com/

Comments and Discussions

 
SuggestionResponsibility of the class Pin
mike7520-Nov-11 2:15
mike7520-Nov-11 2:15 
Hi zenwalker,

your main argument not to use a single public set accessor is that there is no need to hide a value to a caller as far as it provides this value before. But here you are considering the caller point of view where you should think in terms of responsibility of the class IMHO.

I mean that if you have a class exposing a property Value, you should ask whether, according to your model, the responsibility of this class is to consume this Value (set accessor) or to provide it (get accessor), or both.

Let's take two examples:
Example #1
Let's take a class Car exposing a property Color. The responsibility of the Car is to let someone change the Color and to provide the Color to external caller. In this case, Color has both a set and get accessors.

Exemple #2
Now let's take a class CarComputer whose goal is to provide various information on a car (speed, remaining fuel) according to a sensor input.
In my model CarComputer has 3 properties: SensorValue, Speed and Fuel.
What is the reponsibility of CarComputer towards SensorValue ? It consumes a SensorValue. Its responsibility is not to produce a SensorValue. As a consequence my model is more consistent if SensorValue has just a set accessor.
No matter that CarComputer could technically expose SensorValue. It is nor a security issue. It is just not the responsibility of a CarComputer to provide a SensorValue.

I hope you'll find my humble participation useful. Don't hesitate to critize my point of view.
GeneralRe: Responsibility of the class Pin
zenwalker198520-Nov-11 15:18
zenwalker198520-Nov-11 15:18 
GeneralRe: Responsibility of the class Pin
mike7522-Nov-11 8:05
mike7522-Nov-11 8:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.