15

Whenever there is question about credibility of Properties, I see that most of the discussion happens around functions/methods vs properties. But I would also like to know the compelling reason to use property with associated private field vs public field directly itself, incase of most common get/set behaviors with no other processing, I mean this way

public string CustomerName;

vs

private string customerName;
public string CustomerName
{
get{return customerName;}
set(string value){this.customerName=value;}
}
skaffman
  • 398,947
  • 96
  • 818
  • 769
Raj
  • 159
  • 1
  • 3

6 Answers6

21

You get source/binary compatibility if you later need to add other behavior, you get to add break points, and it's just philosophically cleaner (care about the behavior, not the storage mechanism).

Note that you don't need the whole of the latter block in C# 3:

public string CustomerName { get; set; }

See my article on "Why Properties Matter" for more information.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Jon..I see your point. But I somehow felt that code would be cleaner in other way i.e, if there are more fields in class(say 30+) then the number of lines of code would be atleast 4 times more with properties. Now automatic properties in C# 3 and in VB 10 are making all of us happy !!! – Raj Aug 13 '09 at 15:16
  • 7
    If you have 30 fields in your class, you should probably use more encapsulation to start with. – Jon Skeet Aug 13 '09 at 15:32
  • 1
    Just to confirm if I understood right about encapsulation, grouping logical subsets of the large fields in a single class into different classes/interfaces and get these through inheritance or composition. Is that correct? If so, that would still contain extra load of code spreaded into different classes rather in one class. Sorry if I am bugging you with nonsense. This is all due to my frustration in maintaining thousands of lines of code(with lots of properties with no extra logic) written by someone else. – Raj Aug 13 '09 at 15:47
  • Yes, you'd be grouping things together - which often means you can reuse them. For example, if your 30 fields contain 2 * 5 fields representing an address, then encapsulating the concept of an "address" definitely saves code. – Jon Skeet Aug 13 '09 at 16:03
  • @JonSkeet, you want to jump in on this [one](http://programmers.stackexchange.com/questions/267246/how-did-oop-evolve-to-include-the-notion-of-properties/267253#267253)? I took these sorts of questions and stated them in terms of language designers intent. – jxramos Dec 24 '14 at 00:01
3
  1. You can override or at least create a "new" property in a derived class

  2. At this point people expect properties to be exposed and fields to be hidden. If someone's going to reflect over your class (its becoming more and more common with tools like Castle Windsor, NHibernate) there is a world of difference, they will likely not be checking for exposed fields.

George Mauer
  • 117,483
  • 131
  • 382
  • 612
2

This is mostly a bug in Java. In many other languages (Python, Delphi, Groovy), the compiler will generate the getters and setters for you unless you supply the code.

This means you can use a "public" field in Groovy and the compiler will silently generate and invoke the setter/setter. If you need to do additional magic when a field is changed, you can introduce a specialized setter and everything will work.

It's one of those things where reality clashes with a design. The Java designers didn't want the compiler to do anything you can't see. What seemed like a good idea many years ago, didn't turn out too well.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Note: The question originally lacked a language tag, so this answer assumes that the code in the question is Java; the [tag:c#] tag was added later. – mklement0 Apr 06 '17 at 11:59
2

I notice one helpful use of property. If you are going to bind the collection of your object to a DataGrid or DataGridView or other bindable control, the only recognized evaluatable names are Property and not public fields.

Nap
  • 8,096
  • 13
  • 74
  • 117
1

You can also provide some basic validation with properties. For example to prevent setting a property to an invalid state like a negative value for a height:

private int height;
public int Height
{
  get{ return height; }
  set 
  { 
     if (value > 0)
     {
         height = value;
     }
  }
}
Valentein
  • 847
  • 1
  • 8
  • 17
  • 1
    I do agree that, any extra processing like validation makes it obvious to use properties. That is why I have specifically mentioned the case of no such extra processing. – Raj Aug 13 '09 at 15:18
0

Even without any extra processing, there are still a few benefits:

  1. Good coding practice. In general it is good to not expose your fields to outside classes directly and instead only allow them to access through this extra layer of abstraction. Even without extra processing, it is still good to be consistent as you might need it somewhere else in the code.
  2. You might need to change a field to a property later on when you have multiple other projects depend on yours. That would require re-compiling everything.
  3. You can use events with them, such as binding handlers to PropertyChanging or PropertyChanged events. In the case of fields this is not possible.
Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68