General OOP Question
-
I have a quick question about OO theory versus practise. I am sure that the theory of OOP states that object attributes (properties) can only be modified through methods (operations). Encapsulation dictates that all the data associated with a class is hidden from view, and can only be changed through public methods. This is something that’s always confused me – How can we get away with declaring public class variables that can be directly accessed and changed? Or is this just a way to make life easier for coders? In the constructor of an object, should we pass through all the parameters in the constructor() method, or should we specify the properties directly using public variables? I know this may sound basic, but sometimes I revisit OO principles, just to confirm my understanding. Thanks, John. www.silveronion.com[^]
-
I have a quick question about OO theory versus practise. I am sure that the theory of OOP states that object attributes (properties) can only be modified through methods (operations). Encapsulation dictates that all the data associated with a class is hidden from view, and can only be changed through public methods. This is something that’s always confused me – How can we get away with declaring public class variables that can be directly accessed and changed? Or is this just a way to make life easier for coders? In the constructor of an object, should we pass through all the parameters in the constructor() method, or should we specify the properties directly using public variables? I know this may sound basic, but sometimes I revisit OO principles, just to confirm my understanding. Thanks, John. www.silveronion.com[^]
John Honan wrote: How can we get away with declaring public class variables that can be directly accessed and changed? In my opinion I would never do this. If I want the user to be able to set a value internally to my class then I provide them with a property to do so. This way I have total control over making sure that the value they pass in is within a valid range and meets all the criteria that I need it to meet before I use it. I also have the ability to reject the property assignment if I want to, maybe for example because of some internal state machine. John Honan wrote: In the constructor of an object, should we pass through all the parameters in the constructor() method, or should we specify the properties directly using public variables? I always struggle with this one also. IMHO it depends greatly upon what your class is used for and how it works internally. If the class will be insatiated then values pulled from properties purely as a utility type class then I try to keep everything in the constructor and provide read-only properties to get the data out. If the class is going to be an object that will hang around a bit then I try to provide several constructors, one with all the parameters, and one overloaded with just a few parameters that are required to get it running in a default mode. I also then provide several read/write properties to set the remainder of the properties latter if needed. On a personal note, I always have a problem with class constructers that have a parameter list a mile long. For me, if it goes much over 4 or 5 parameters then I use a structure to pass in the parameters.
Paul Watson wrote: "At the end of the day it is what you produce that counts, not how many doctorates you have on the wall." George Carlin wrote: "Don't sweat the petty things, and don't pet the sweaty things." Jörgen Sigvardsson wrote: If the physicists find a universal theory describing the laws of universe, I'm sure the asshole constant will be an integral part of that theory.
-
I have a quick question about OO theory versus practise. I am sure that the theory of OOP states that object attributes (properties) can only be modified through methods (operations). Encapsulation dictates that all the data associated with a class is hidden from view, and can only be changed through public methods. This is something that’s always confused me – How can we get away with declaring public class variables that can be directly accessed and changed? Or is this just a way to make life easier for coders? In the constructor of an object, should we pass through all the parameters in the constructor() method, or should we specify the properties directly using public variables? I know this may sound basic, but sometimes I revisit OO principles, just to confirm my understanding. Thanks, John. www.silveronion.com[^]
John Honan wrote: How can we get away with declaring public class variables that can be directly accessed and changed? This is something that has kept bugging me too. My reply might sound stupid (I'm only a student) but here goes: What you say is basically right, but then, it's better than making the variables publicly accessible. If you consider the other extreme, you won't be able to modify any values at all. John Honan wrote: Or is this just a way to make life easier for coders? From my limited knowledge, that is exactly what I keep telling myself. If you find a convincing answer, do tell me though. :) Vikam. "There's probably a Nish-like alien answering VB questions on a CP forum as we speak." - adamUK in The Lounge, discussing aliens and parallel universes. "Do not give redundant error messages again and again." - A classmate of mine, while giving a class talk on error detection in compiler design.
-
John Honan wrote: How can we get away with declaring public class variables that can be directly accessed and changed? This is something that has kept bugging me too. My reply might sound stupid (I'm only a student) but here goes: What you say is basically right, but then, it's better than making the variables publicly accessible. If you consider the other extreme, you won't be able to modify any values at all. John Honan wrote: Or is this just a way to make life easier for coders? From my limited knowledge, that is exactly what I keep telling myself. If you find a convincing answer, do tell me though. :) Vikam. "There's probably a Nish-like alien answering VB questions on a CP forum as we speak." - adamUK in The Lounge, discussing aliens and parallel universes. "Do not give redundant error messages again and again." - A classmate of mine, while giving a class talk on error detection in compiler design.
You can have static variables, properties, and methods in a class, and you can set it up so that the class acts no differently than a standard module would.
"Do unto others as you would have them do unto you." - Jesus
"An eye for an eye only makes the whole world blind." - Mahatma Gandhi