views:

476

answers:

7

I was reading a book called C# Yellow Book by Rob Miles when I came across this statement:

Programmer’s love new shiny toys. They are very keen to use language features to show off. Properties can be a bit like this. When considering properties versus get and set methods I am rather a fan of the old fashioned get and set methods because you know where you are with these. On the other hand I can see where properties might make life easier, if used in the correct way.

I'm not really sure what he means by this. What do you guys suggest? What does MS suggest?

+18  A: 

I think you should consider a bit of non-programming advice

When in Rome, do as the Romans do.

.NET has properties ingrained into all of it's libraries, designers and code generators. It is almost an inseperable part of the platform. If you choose to use get/set accessors instead of normal properties you will be creating a library which has a very different appearance than what every other programmer is expecting.

Using get/set accessors will only increase the chances that you create code which is incompatible with various tools. For instance, there are many tools out there which special case properties and fields and provide special features for them. You will have an equivalent construct but no tool support.

JaredPar
"Using get/set accessors will only increase the chances that you create code which is incompatible with various tools." - indeed, and for a very broad definition of "tools"! Think of data binding in WinForms/ASP.NET/WPF, for example...
Pavel Minaev
A: 

I love properties. In the "Design Guidelines for Class Libraries" they recommend using Properties (particularly for state that is independent of other state).

Arnshea
A: 

The best way to think about this is by drawing comparisons with real life objects. Let us say you are a person in real life needs to be designed. What would you call some of your attributes in terms of color of hair, height and width etc? Once you start thinking on these lines, it will be more helpful as every one has their own way to think.

CodeToGlory
+6  A: 

I don't know who Rob Miles is, but if your quote is accurate, then I've already lost respect for him.

Everything in .NET uses properties. If your code does not use properties, then it will be about the only piece of code that does not.

John Saunders
If I had to guess, the quote comes from a book written at or before the release of 1.0 (ie. 7+ years ago). At that time, if you were a die-hard Java programmer converting over, it would have been more understandable.
Jonathan
A: 

If you don't use Properties, you loose a very prominent feature that I consider to be extremely useful in .NET: Binding. You can only bind to/from properties in .NET so if you use get/set accessors, you are not playing nicely with others.

Brian Genisio
+2  A: 
myObject.Property++;

versus:

myObject.SetProperty(myObject.GetProperty() + 1);

Yeah..

Explicit getter/setter methods should be used when non-trivial calculation/processing is done, so where an actual action is performed.

JulianR
+6  A: 

I wrote that part of the book as a Java programmer moving over to C# a few years ago. I think I'll revisit the text in the next version and make it a bit clearer.

If you want to make sure that the user of your class is aware that some code is going to run when they use a thing, then putting it into a method makes this explicit. Furthermore, using methods allows you to return error conditions without having to throw exceptions. There is no perfomance hit, since the compiler converts properties into methods anyway.

I use properties a lot in the code I write though, particularly for things like state. I was never really advocating not using properties, more making sure that you use the right thing in the right situation.

Rob Miles
Wow the author himself. Welcome to Stackoverflow ;-)
Lucas McCoy
Jonathan's comment in John Saunders answer really hit the nail on the head.
Lucas McCoy
Knowing when to use properties and when to use methods is a good thing to teach. Indeed, Microsoft make it pretty clear when you should use each. However, it appeared you were advocating using get/set methods instead of properties, which would just be perverse in C# - maybe some clarification is needed if that wasn't your intention?
Dan Diplo