Karl Johan
Karl Johan

Reputation: 4032

Class internal usage of public properties

Let's say I have a class that exposes one property. Is it considered to be a good aproach to use the private "holder variable" for internal use in the class? Or should I use the property for internal use also.

To explain, should I use:

public class foo
{
    String _statusHolder;
    public String myStaus
    {
        get { return _statusHolder; }
        set{ _statusHolder = value; }
    }

    public void DisplayMyStatus()
    {
        Console.WriteLine(_statusHolder);
    }
}

Or:

public class foo
{
    String _statusHolder;
    public String myStaus
    {
        get { return _statusHolder; }
        set{ _statusHolder = value; }
    }

    public void DisplayMyStatus()
    {
        Console.WriteLine(myStaus);
    }
}

I could see it as beeing more consistent and more readable to use the second approach. It would also be more effective if I would later do some modificatins in the set-statement. But are there any performance issues or is it considered bad-practise for some reason?


EDIT:

It seems that everybody is leaning towards using the property internally. My initial thoughts was the same, but as a novice programmer, you can never know. Thanks everyone for the quick feedback!

Upvotes: 2

Views: 470

Answers (4)

Miserable Variable
Miserable Variable

Reputation: 28761

Programming in Java, I prefer using the getter method because I can put a breakpoint there and/or see changes to it in logging output.

Upvotes: 1

Sam Saffron
Sam Saffron

Reputation: 131172

I tend to go with calling the properties cause once stuff gets tricky you can put in locking and business logic in the getter

For C# 3.0 I would go with something along these lines (and only explicitly create the backing field when its really needed)

public class foo
{

    public String Status
    {
        get;
        set;
    }

    public void DisplayMyStatus()
    {
        Console.WriteLine(Status);
    }
}

Upvotes: 2

Sebastian Dietz
Sebastian Dietz

Reputation: 5706

Use the property if there is one. A property can have side effects like lazy initializing that you want to have regardless from where you access the variable.

Even if the property has no side effects now, other developers could add them later, and the places where the "raw" variable is used may be error-prone because the new code is not called.

And lastly, the property makes refactoring easier, for example, when the value later is no longer stored in a variable but is calculated inside the property accessor or comes from some other source variable.

Upvotes: 2

David Wengier
David Wengier

Reputation: 10179

Performance issues should be negligable, as the JITer or compiler will happily work out that your function call (the getter of the property) doesn't do anything exciting, and can be inlined.

The benefit is future changes to business logic that might be put in the getter, which your class will then automatically take advantage of, without refactoring too much.

Of course, the downside is, you might want to avoid that new business logic in some circumstances, so it is something that needs to be considered based on how likely a) logic will change, and b) that logic might need to be circumvented.

The other (potential) advantage of using the property internally is that you can easily move to, or from, automatic properties.

Upvotes: 3

Related Questions