Why should I avoid using Properties in C#?

I haven't read the book, and you haven't quoted the part of it you don't understand, so I'll have to guess.

Some people dislike properties because they can make your code do surprising things.

If I type Foo.Bar, people reading it will normally expect that this is simply accessing a member field of the Foo class. It's a cheap, almost free, operation, and it's deterministic. I can call it over and over, and get the same result every time.

Instead, with properties, it might actually be a function call. It might be an infinite loop. It might open a database connection. It might return different values every time I access it.

It is a similar argument to why Linus hates C++. Your code can act surprising to the reader. He hates operator overloading: a + b doesn't necessarily mean simple addition. It may mean some hugely complicated operation, just like C# properties. It may have side effects. It may do anything.

Honestly, I think this is a weak argument. Both languages are full of things like this. (Should we avoid operator overloading in C# as well? After all, the same argument can be used there)

Properties allow abstraction. We can pretend that something is a regular field, and use it as if it was one, and not have to worry about what goes on behind the scenes.

That's usually considered a good thing, but it obviously relies on the programmer writing meaningful abstractions. Your properties should behave like fields. They shouldn't have side effects, they shouldn't perform expensive or unsafe operations. We want to be able to think of them as fields.

However, I have another reason to find them less than perfect. They can not be passed by reference to other functions.

Fields can be passed as ref, allowing a called function to access it directly. Functions can be passed as delegates, allowing a called function to access it directly.

Properties... can't.

That sucks.

But that doesn't mean properties are evil or shouldn't be used. For many purposes, they're great.


Well, lets take his arguments one by one:

A property may be read-only or write-only; field access is always readable and writable.

This is a win for properties, since you have more fine-grained control of access.

A property method may throw an exception; field access never throws an exception.

While this is mostly true, you can very well call a method on a not initialized object field, and have an exception thrown.

• A property cannot be passed as an out or ref parameter to a method; a field can.

Fair.

• A property method can take a long time to execute; field access always completes immediately.

It can also take very little time.

• If called multiple times in a row, a property method may return a different value each time; a field returns the same value each time.

Not true. How do you know the field's value has not changed (possibly by another thread)?

The System.DateTime class has a readonly Now property that returns the current date and time. Each time you query this property, it will return a different value. This is a mistake, and Microsoft wishes that they could fix the class by making Now a method instead of a property.

If it is a mistake it's a minor one.

• A property method may cause observable side effects; field access never does. In other words, a user of a type should be able to set various properties defined by a type in any order he or she chooses without noticing any different behavior in the type.

Fair.

• A property method may require additional memory or return a reference to something that is not actually part of the object's state, so modifying the returned object has no effect on the original object; querying a field always returns a reference to an object that is guaranteed to be part of the original object's state. Working with a property that returns a copy can be very confusing to developers, and this characteristic is frequently not documented.

Most of the protestations could be said for Java's getters and setters too --and we had them for quite a while without such problems in practice.

I think most of the problems could be solved by better syntax highlighting (i.e differentiating properties from fields) so the programmer knows what to expect.


Jeff's reason for disliking properties is because they look like fields - so developers who don't understand the difference will treat them as if they're fields, assuming that they'll be cheap to execute etc.

Personally I disagree with him on this particular point - I find properties make the client code much simpler to read than the equivalent method calls. I agree that developers need to know that properties are basically methods in disguise - but I think that educating developers about that is better than making code harder to read using methods. (In particular, having seen Java code with several getters and setters being called in the same statement, I know that the equivalent C# code would be a lot simpler to read. The Law of Demeter is all very well in theory, but sometimes foo.Name.Length really is the right thing to use...)

(And no, automatically implemented properties don't really change any of this.)

This is slightly like the arguments against using extension methods - I can understand the reasoning, but the practical benefit (when used sparingly) outweighs the downside in my view.

Tags:

C#

Properties