|
Gary Wheeler wrote: it also ignores the fact that C# properties are merely syntactic sugar provided by the compiler around method calls, which makes the whole discussion rather pointless
Sure, but if you talk about Properties outside of technical implementation details then the discussion applies.
Gary Wheeler wrote: As always, the key is knowing which part of the blade is sharp
I prefer to carry a large box of band-aids. Far more interesting.
cheers
Chris Maunder
|
|
|
|
|
I agree with you, these are at best overly prescriptive and arguably just wrong.
In my mind, a property should be a description of the state of an object. Whether that's state that's been set explicitly by previously assigning the property, implicitly by the interaction of other property setters or methods, or state that is emergent from the internal workings of the object, doesn't really matter. A property getter should be asking the question 'what is your state', whereas a method call is an instruction to do something, and any result from a function or stateful method should be a return value related to what you did, not just a reflection of object state.
Some of his points I definitely agree with: a property getter should not change the state of the object, and it should be fast. For this reason I also agree with him that you should be careful with load-on-demand properties; sometimes there's a good reason to do it, but one should seriously consider whether code that may take long enough it's worth lazy-loading its result should actually be a method call instead. Load-on-demand properties can be a lovely convenience, particularly when working with classes that cover database or file access, and I have used them quite a bit in that context, but they should always come with a health warning.
I'm definitely with you on the exception thing, though. If an object is in an invalid state and you ask it a question about its state, it's reasonable for an exception to be thrown. It's generally a good design principle to make it very difficult to get an object into an invalid state, by making property setters and state-changing methods throw if the thing you've asked them to do is invalid, but sometimes it is unavoidable (e.g. if you're covering a network dependency and the connection was dropped).
I don't have a problem with returning lists, arrays or mutable types from properties, as long as it's documented or well known among the relevant people whether it's okay to mess with them or not. As long as the class doesn't actually cause a major crash if you break its internal state assumptions, allowing the user to go down the road of 'garbage in, garbage out' by abusing your API is fine. Coders are intelligent people, we don't need to make everything idiot proof.
|
|
|
|
|
BobJanova wrote: It's generally a good design principle to make it very difficult to get an object into an invalid state, by making property setters and state-changing methods throw if the thing you've asked them to do is invalid
That's a good point: if you throw exceptions on the setter then (in theory) you will never, ever run across the situation of have a bad value from a getter. Assuming you stick to Prescription 1: A property cannot change "by itself".
cheers
Chris Maunder
|
|
|
|
|
Chris Maunder wrote: Properties should be stable: that is, the value of a property shouldn't change “on its own". Rather, changes in properties are the results of calling property setters, or some other action changing the state of the object
If by this he means properties should be read only then I can see that. Can't say I'd always do it, but I can see the reasoning.
Chris Maunder wrote: Property getters should always succeed: they should never throw an exception. Return some reasonable default value if the property cannot be logically computed right now because the object is in a bad state.
Gotta agree with this. Exceptions should not be abused and throwing on in a property getter seems way too much like overkill.
Jeremy Falcon
|
|
|
|
|
Chris Maunder wrote: What are your thoughts? Where am I going wrong in my thinking here?
You're not doing anything wrong. Which is why I stopped myself from doing a deep dive (we would have written the same thing!) and simply made the flagrant comment that his post was drivel. Seemed simpler.
Marc
|
|
|
|
|
Your efficiency, simplicity, and "do not reinvent the wheel" mentality are an inspiration to us all.
cheers
Chris Maunder
|
|
|
|
|
I agree with the premise that properties should never throw exceptions, and should instead return a reasonable default value if an error condition presents itself. If you want to throw an exception based on a property value, you should do it from the method that is using the property.
In most circumstances, an empty get/set is all that is needed. However, there are exceptions (pun - LOL). I abhor the all-or-nothing XML serialization/deserialization mechanism built into .Net, and wrote my own extension methods that can handle faulty data that returns reasonable default values for expected fields. Yeah, it takes longer to write the code, but I have a LOT more control over what's happening. As a result, pretty much all of my data-based objects contain a property called AsXElement , and a constructor overload that accepts an XElement . If there's a problem with the expected data, no exception is thrown, and the code proceeds as expected.
public class MyObject
{
public string Text { get; set; }
public XElement AsXElement
{
get
{
XElement value = new XElement("ElementName",
new XAttribute("text", this.Text));
return value;
}
set
{
if (value != null)
{
this.Text = value.GetAttribute("text", "NO DATA");
}
}
}
public MyObject(XElement value)
{
this.AsXElement = value;
}
}
Of course, different platforms require different approaches. WPF and Silverlight almost require code in property get/set, especially if they're bound to a control, and or the property's value is considered due to the use of the INotifyPropertyChanged interface.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
John Simmons / outlaw programmer wrote: pun - LOL
I don't get it.
cheers
Chris Maunder
|
|
|
|
|
Quote: I abhor the all-or-nothing XML serialization/deserialization mechanism built into .Net I can understand that; it can be a PITA to get what you want. I use it quite a bit, and have become accustomed to it. My previous experience was using MSXML via COM in C++, so I obviously have a high tolerance for pain when it comes to XML handling.
John Simmons / outlaw programmer wrote: the property's value is considered due to the use of the INotifyPropertyChanged interface Ah. I missed that one.
Software Zen: delete this;
|
|
|
|
|
Chris Maunder wrote: What are your thoughts? I disagree on both points. Makes you wonder how much real code the author has written.
/ravi
|
|
|
|
|
A recently hired HR employee was on the elevator on her first day after training. A man walked on the elevator wearing flip-flops. Wanting to show her newfound authority she casually mentioned, as only H.R. can do:
"You know, Flip Flops are against company dress code"
and the man replied
"That is the benefit to owning the company"
Programming questions in the lounge.
J/k
I hate properties but I have reluctantly been using them because all of the nice generated tools and such require them. Ie, data-binding, serialization, etc. It is really a love-hate relationship but yes, generally speaking one call to get should be the same no matter what so long as set was never called.
Not to be the guy that is an apologist. But just be glad .NET DateTime isn't like the Java version. It fills the need.
|
|
|
|
|
This[^] campaign reminds me of you both. I remember you two have posted few recipes in your profile blog page.
I think 1000+ recipes(with yours) for £1 is a good deal. PDF.
|
|
|
|
|
Agreed.
And a good idea!
---------------------------------
Obscurum per obscurius.
Ad astra per alas porci.
Quidquid latine dictum sit, altum videtur .
|
|
|
|
|
... mmm ... 100 million US$ of the CIA's and Jeff Bezos' money, Josephson junctions high on liquid-helium at 20 millikelvin: who cares if its only 3600 times faster than your average sod's desktop: [^].
Critic Scott Aaronson of MIT keeps dissing the whole thing as a marketing circus: "I don’t care if the Messiah has come to Earth on a flaming chariot, not to usher in an age of peace but simply to spend $10 million on D-Wave’s new Vesuvius chip" [^].
The kind of gear Anand Sharkey dreams about, I guess.
“I speak in a poem of the ancient food of heroes: humiliation, unhappiness, discord. Those things are given to us to transform, so that we may make from the miserable circumstances of our lives things that are eternal, or aspire to be so.” Jorge Luis Borges
|
|
|
|
|
I wonder what sort of programming language you use to write software that keeps "bits" in an indeterminate state and entangles them in a way that resolves a meaningful result, while at the same time supporting multiple simultaneous operations while they're in the Q-state.
Marc
|
|
|
|
|
Yeah, and what kind of programmer can code in that language ? Dyslexics, schizophrenics, high-Asperger savants ?
“I speak in a poem of the ancient food of heroes: humiliation, unhappiness, discord. Those things are given to us to transform, so that we may make from the miserable circumstances of our lives things that are eternal, or aspire to be so.” Jorge Luis Borges
|
|
|
|
|
That would be a question for Boole's wife.
Will Rogers never met me.
|
|
|
|
|
Is this a statement about Karnaugh knowledge?
Software Zen: delete this;
|
|
|
|
|
Do they still teach Karnaugh maps to these kids?
Will Rogers never met me.
|
|
|
|
|
I don't know. I learned Karnaugh maps in my digital logic classes, er, 30 years ago. Today, it wouldn't surprise me at all if you could type a logic expression as a Google search string and get a simplification back as a result. I imagine Mathematica at least can do that sort of thing.
Software Zen: delete this;
|
|
|
|
|
VB, obviously!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
If you look at Chris Maunders question above, then quantum computing solves his issue: its a property AND a method!
|
|
|
|
|
For several months now, one of our testers has been pushing to get the OK and Cancel buttons switched in every single dialog in our application (roughly 200 dialogs). His only reasoning for this is that the way we do it (OK in bottom right corner, Cancel to the left of it) is the opposite of what Microsoft does throughout Windows(Cancel in bottom right corner, OK to the left of it).
That is his one and only reason. He fails to acknowledge that switching it will annoy the hell out of every single person that uses our software (thousands of people).
The next time he brings it up I might punch him in the face.
The United States invariably does the right thing, after having exhausted every other alternative. -Winston Churchill
America is the only country that went from barbarism to decadence without civilization in between. -Oscar Wilde
Wow, even the French showed a little more spine than that before they got their sh*t pushed in.[^] -Colin Mullikin
|
|
|
|
|
I'd just defer to the requirements... or business owners... surely nobody else wants QA inventing requirements based on a wim
|
|
|
|
|
This particular tester seems to think that he is a God among men when it comes to interfaces...
The United States invariably does the right thing, after having exhausted every other alternative. -Winston Churchill
America is the only country that went from barbarism to decadence without civilization in between. -Oscar Wilde
Wow, even the French showed a little more spine than that before they got their sh*t pushed in.[^] -Colin Mullikin
|
|
|
|