There's a lot of advice out there that you shouldn't expose your fields publically, and instead use trivial properties. I see it over & over.
I understand the arguments, but I don't think it's good advice in most cases.
Does anyone have an example of a time when it really mattered? When writing a trivial property made something important possible in the future (or when failing to use one got them in to real trouble)?
EDIT: The DataBinding argument is correct, but not very interesting. It's a bug in the DataBinding code that it won't accept public fields. So, we have to write properties to work around that bug, not because properties are a wise class design choice.
EDIT: To be clear, I'm looking for real-world examples, not theory. A time when it really mattered.
EDIT: The ability to set a breakpoint on the setter seems valuable. Designing my code for the debugger is unfortunate: I'd rather the debugger get smarter, but given the debugger we have, I'll take this ability. Good stuff.
informal. : to save someone : to help someone get out of a dangerous or difficult situation. You really saved my bacon by helping out yesterday.
Definition of 'to save someone's bacon' If someone or something saves your bacon, they get you out of a dangerous or difficult situation.
Bacon is an example of how people preserved meat without refrigeration. It's soaked in salt, either a liquid brine, or packed in granulated salt, then smoked. Bacon will last a long time with no refrigeration.
According to definition #4, a “bacon,” short for “chaw-bacon,” was actually a pejorative! It referred to the fact that swine's flesh was a meat primarily consumed by England's rural population. Thus, calling someone a bacon meant they were “a rustic clown”: a peasant, a bumpkin, a slack-jawed yokel. The nerve!
It may be hard to make code work in an uncertain future, but that's no excuse to be lazy. Coding a property over a field is convention and it's pragmatic. Call it defensive programming.
Other people will also complain that there's a speed issue, but the JIT'er is smart enough to make it just about as fast as exposing a public field. Fast enough that I'll never notice.
Some non-trivial things that come to mind
get
versus set
accessibility (e.g. public get, internal set)You only type an extra 13 characters for correctness. That hardly seems like speculative generality. There is a semantic difference, and if nothing else, a property has a different semantic meaning and is far more flexible than a public field.
public string Name { get; set; } public string name;
I do recall one time when first using .net I coded a few classes as just fields, and then I needed to have them as properties for some reason, and it was a complete waste of time when I could have just done it right the first time.
So what reasons do you have for not following convention? Why do you feel the need to swim upstream? What has it saved you by not doing this?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With