Click here to Skip to main content
15,910,121 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Property or a method. This seems wrong. Pin
User 5924120-May-14 20:13
User 5924120-May-14 20:13 
GeneralRe: Property or a method. This seems wrong. Pin
Jörgen Andersson20-May-14 20:22
professionalJörgen Andersson20-May-14 20:22 
GeneralRe: Property or a method. This seems wrong. Pin
Chris Maunder21-May-14 4:13
cofounderChris Maunder21-May-14 4:13 
GeneralRe: Property or a method. This seems wrong. Pin
Jörgen Andersson21-May-14 4:41
professionalJörgen Andersson21-May-14 4:41 
GeneralRe: Property or a method. This seems wrong. Pin
V.20-May-14 21:11
professionalV.20-May-14 21:11 
GeneralRe: Property or a method. This seems wrong. Pin
Gary Wheeler21-May-14 0:28
Gary Wheeler21-May-14 0:28 
GeneralRe: Property or a method. This seems wrong. Pin
Chris Maunder21-May-14 4:15
cofounderChris Maunder21-May-14 4:15 
GeneralRe: Property or a method. This seems wrong. Pin
BobJanova21-May-14 1:11
BobJanova21-May-14 1:11 
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.
GeneralRe: Property or a method. This seems wrong. Pin
Chris Maunder21-May-14 4:17
cofounderChris Maunder21-May-14 4:17 
GeneralRe: Property or a method. This seems wrong. Pin
Jeremy Falcon21-May-14 1:11
professionalJeremy Falcon21-May-14 1:11 
GeneralRe: Property or a method. This seems wrong. Pin
Marc Clifton21-May-14 2:05
mvaMarc Clifton21-May-14 2:05 
GeneralRe: Property or a method. This seems wrong. Pin
Chris Maunder21-May-14 4:18
cofounderChris Maunder21-May-14 4:18 
GeneralRe: Property or a method. This seems wrong. Pin
#realJSOP21-May-14 2:22
professional#realJSOP21-May-14 2:22 
GeneralRe: Property or a method. This seems wrong. Pin
Chris Maunder21-May-14 4:20
cofounderChris Maunder21-May-14 4:20 
GeneralRe: Property or a method. This seems wrong. Pin
Gary Wheeler21-May-14 4:21
Gary Wheeler21-May-14 4:21 
GeneralRe: Property or a method. This seems wrong. Pin
Ravi Bhavnani21-May-14 2:37
professionalRavi Bhavnani21-May-14 2:37 
GeneralThis question reminds me of the anecdote Pin
Ennis Ray Lynch, Jr.21-May-14 3:49
Ennis Ray Lynch, Jr.21-May-14 3:49 
GeneralNagy & DD Pin
thatraja20-May-14 17:36
professionalthatraja20-May-14 17:36 
GeneralRe: Nagy & DD Pin
Dalek Dave20-May-14 21:46
professionalDalek Dave20-May-14 21:46 
GeneralD-Wave Quantum Computer: does true/false super-fast, but, can it do "maybe" ? Pin
BillWoodruff20-May-14 14:27
professionalBillWoodruff20-May-14 14:27 
GeneralRe: D-Wave Quantum Computer: does true/false super-fast, but, can it do "maybe" ? Pin
Marc Clifton20-May-14 15:17
mvaMarc Clifton20-May-14 15:17 
GeneralRe: D-Wave Quantum Computer: does true/false super-fast, but, can it do "maybe" ? Pin
BillWoodruff20-May-14 15:43
professionalBillWoodruff20-May-14 15:43 
GeneralRe: D-Wave Quantum Computer: does true/false super-fast, but, can it do "maybe" ? Pin
Roger Wright20-May-14 18:33
professionalRoger Wright20-May-14 18:33 
GeneralRe: D-Wave Quantum Computer: does true/false super-fast, but, can it do "maybe" ? Pin
Gary Wheeler21-May-14 4:25
Gary Wheeler21-May-14 4:25 
GeneralRe: D-Wave Quantum Computer: does true/false super-fast, but, can it do "maybe" ? Pin
Roger Wright22-May-14 17:28
professionalRoger Wright22-May-14 17:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.