Go to home page mail me! RSS Feed

Properties or Public variables - The debate must end!

Thursday, November 29, 2007 11:42 AM

Ok, seriously, this debate must end! I strongly believe that changing a variable to a property is breaking a change! However, others feel otherwise.

Update: Yes, this debate is not valid for C# 3.0, however, the question is intended for the earlier versions of C# which is still widely used.

private string name;

public string Name
{
    get
    {
        return name;
    }

    set
    {
        name=value;
    }
}

 

How many times have you seen a similar snippet just like the one above, I just had a mouth battle with other developers that think otherwise about how properties should be used. The debate all started when I stated that if you have a property, you should always access the property rather than the local private variable. Anyway, the question is when should we use a property over a public variable?

  1. When is a property not a property?
  2. When does a property defies the rules of KISS?
  3. Why waste time with meaningless code, if your property doesn't do anything why not use a public variable?
  4. Should we just forget about variables and only use properties?
  5. When should a method be used over a property?

I've heard different arguments and there is no real conclusion on my part, I want to hear what you guys think.

kick it on DotNetKicks.com

Your Comments.

  • # re: Properties or Public variables - The debate must end!

    GravatarC# 3 lets you keep properties and still practice KISS

    public string Foo { get; set; }

    No variable declaration, no trivial get or set. Just declare it.

    I predict this will be the common way of using properties in the future.

    Left by Judah at 11/29/2007 12:08 PM
  • # re: Properties or Public variables - The debate must end!

    Gravatarthe key benefit of using properties over public fields is that you can change the underlying implementation without needing to modify any calling code. For example, what if you wanted an event to be fired when Name was changed? Using a property allows you to quickly add this in.

    Also as Judah mentioned, now that C#3.0 gives us the nice new syntax for declaring properties, it is no more work to type.

    Left by Mark Heath at 11/29/2007 12:26 PM
  • # re: Properties or Public variables - The debate must end!

    GravatarOften times cases come up where you will need to validate a value before setting the variable (not necessarily a control, just a variable in general). If you use properties this becomes centralized:

    private int index;
    public int Index
    {
    get
    {
    return index;
    }
    set
    {
    if(index >= 0)
    index = value;
    }
    }

    Mark mentioned a similar case were you might want to fire an event when a variable is set. You could say that you could just use properties in those special cases and public variables every other time, but if you get into the practice of doing everything on a case by case basis then you'd essentially be getting rid of a set of standards just so the Public Variable people don't have to write a few extra lines of code.

    Left by Schmitty at 3/5/2008 5:48 PM

Your Reply.

Comment Form.

Fields denoted with a "*" are required.

You may also like to leave your email or website.

 
Please add 7 and 8 and type the answer here:

Preview Your Comment.

 
Next entries »