Jimmy Clark wrote:Encapsulating object data with JavaBean get/set methods is good and should be implemented. Many Java-based frameworks follow this paradigm as well.
In the end it is a design decision, for programmers and engineers under my supervision JavaBean get/set methods are mandatory in class design.
However, there are rare situations to this design style where it is better to expose one or two data variables publicly. It depends.
Jimmy Clark wrote:Exactly how many "web" articles have you read about this topic?
Debbie Dawson wrote:The general consensus I have gotten speaking to senior-type OOP developers and the articles I have read from the same lot seem to almost always say the same thing... "Getters and Setters are in direct conflict with the basic principle of Encapsulation in OOP."
Bear Bibeault wrote:[ That makes no sense. ]. Please take careful note of who wrote the article to be sure to never read anything else that he has written.
David Newton wrote:
Debbie Dawson wrote:The general consensus I have gotten speaking to senior-type OOP developers and the articles I have read from the same lot seem to almost always say the same thing... "Getters and Setters are in direct conflict with the basic principle of Encapsulation in OOP."
That's [ contrary to my experience ] - and contradictory to encapsulation.
What *are* issues are things like not following the Law of Demeter (or, as I prefer, the Strongly-Worded Suggestion of Demeter), or returning specific implementations instead of interfaces, or not returning immutable versions or copies of underlying objects. Those are very different things.
It's also very important to remember a couple other points: Java is *not* a great example of an OOPL, and practicality often outweighs purity.
Classes *should* be self-contained, and we should program Java like it's a functional language. But it isn't.
Debbie Dawson wrote:The article I posted doesn't say to never use them, but it clearly shows why it is a poor choice.
The Article wrote:It seems natural, then, to mimic a struct by building class definitions with virtually no methods and nothing but public fields.
The Article wrote:it's okay for a method to return an object in terms of an interface that the object implements because that interface isolates you from changes to the implementing class
Ernest Friedman-Hill wrote:A class should have "get" methods for precisely those properties that would be useful to read, and "set" methods only for those that would be useful and safe to set. Should these, or do these, correspond one-to-one with the instance variables of the class? Generally not. You'll read more than you can set, and some properties don't correspond to a single member variable but rather some combination.
If you want to be a "seasoned OOP veteran", program in OOPLs for a decade or two, learn more than one OOP paradigm, use better OOPLs than Java and PHP.
Code fiendishly: how's your registration app going?
Have you coded it in a half-dozen ways yet? Until you do, why bother arguing about what the "best" way is?
See what works, and what doesn't.
Read all the articles you want--but if you're going to hand-wave with an article and "general consensus" don't be surprised when people are more amused than engaged. (And this is nothing--try this on LtU
But I'd genuinely enjoy watching you defend PHP as a language there.)
Debbie Dawson wrote:The article I posted doesn't say to never use them, but it clearly shows why it is a poor choice.
No, it really doesn't: it rails against exposing internal details (sort of, see below)--but this is the result of stupid programmers, not getters/setters.
The Article wrote:It seems natural, then, to mimic a struct by building class definitions with virtually no methods and nothing but public fields.
Maybe yet *another* decade before this article was written it seemed natural? Or maybe this was true of non-OOP devs coming to Java?
Even then--Java is not the only OOPL. Some OOPLs don't even *have* a concept of data hiding--like Smalltalk. Smalltalk is probably *the* prototypical OOPL. So this article is approaching the problem from a singular, narrow focus at the outset.
Then there's this section on when it's "okay" to use getters/setters...
The Article wrote:it's okay for a method to return an object in terms of an interface that the object implements because that interface isolates you from changes to the implementing class
So... I could return a Collection, right? It's an interface.
Which gives me direct access to backing object, which I can keep a reference to elsewhere, modify, do whatever I want with. Which could cause no *end* of troubles in the class itself.
That violates encapsulation, and the Strongly-Worded Suggestion of Demeter. That he doesn't even *mention* Demeter is a *BIIIIIG* reason to treat the article with suspicion--he leaves out the *most* important topic, and in fact obfuscates its importance by claiming that by exposing an interface that accessors are suddenly "ok".
The article is an argument for functional programming, not against getters/setters, and does a poor job. But it did what it was designed to do.
Ernest Friedman-Hill wrote:Alan Holub can be quite a character, and he definitely has taken some extreme stances over the years in his articles. Note that around the same time, he wrote another JavaWorld article called "'extends' is Evil".
I haven't even read the article that we're discussing here (maybe I read it when it came out, but who remembers?) but I'll give you my viewpoint: There's nothing whatsoever intrinsically wrong with accessor/mutator methods, but the notion of always adding get/set methods for each member variable -- which some people do automatically -- is crazy, and perhaps that's the "evil" Alan's getting at.
Back when he was writing, there were still IDEs that would automatically create those methods by default for any member variable you added, and that's ridiculous.
A class should have "get" methods for precisely those properties that would be useful to read, and "set" methods only for those that would be useful and safe to set. Should these, or do these, correspond one-to-one with the instance variables of the class? Generally not. You'll read more than you can set, and some properties don't correspond to a single member variable but rather some combination.
Debbie Dawson wrote:
Same reason I don't jump on the first plane for Europe when I want to make my first trip there and see certain things a certain way. "Cowboy Coding" never got anyone - who is serious - very far in life...Have you coded it in a half-dozen ways yet? Until you do, why bother arguing about what the "best" way is?
What is that??And this is nothing--try this on LtU
I never said PHP was a "real" OO language. I just asked why you find Java so superior.
What is the "Strongly-Worded Suggestion of Demeter"??
Jimmy Clark wrote:Debbie, the JavaBean get/set method style was created for simple "data" classes.
David Newton wrote:The less you code the less you'll learn. Period. *START CODING*. Who *cares* if you "cowboy code" 500 lines of code?! Your purpose should be to start figuring out what works. IMO you should have implemented this two or three times already. I don't understand why you won't.
I don't understand your attempts to learn how to program without programming.
I don't understand your reluctance to experiment. I don't understand your reluctance to throw away your work--at this stage of the game it is what should, and will, happen.
We started talking about this, what, three weeks ago? You want to become a seasoned OO programmer? Program.
What is that??And this is nothing--try this on LtU
I never said PHP was a "real" OO language. I just asked why you find Java so superior.
Java is an abomination--PHP just happens to be worse.
What is the "Strongly-Worded Suggestion of Demeter"??
Google Law of Demeter.
Do you really believe you can shortcut the process? I don't.
That's the definition of "seasoned": one with a lot of experience. How do you get experience? Consistent effort over time.
Jimmy Clark wrote:Debbie, the JavaBean get/set method style was created for simple "data" classes. It should not be applied to complex classes that contain processing algorithms or any type of behavior/functionality.
David Newton wrote:
Jimmy Clark wrote:Debbie, the JavaBean get/set method style was created for simple "data" classes.
That's not really true; JavaBeans were created to allow tooling and other beans to use and manipulate objects, and to foster application composition. The relative simplicity or complexity of the components was never a consideration--this was one of the selling points of JavaBeans: a complex application or component could be manipulated and plugged in the same way as a simple one.
http://www.oracle.com/technetwork/java/javase/tech/index-jsp-138795.html
Debbie Dawson wrote:Why might Getters/Setters be bad in my own words?
Hmmm...
I think the main thing is that you are *supposedly* supposed to keep "data" and "behavior" close together. Well designed classes have the "right data" and the "right actions" in the same class. If you have a class "Order", you would ideally not only have a properties called "subTotal" and "salesTaxRate" but a method called "calculateSalesTax()" and "calculateOrderTotal()".
You would want to avoid having and using "getSubTotal()", "getSalesTaxRate()", then reading said values into another space (be it a class or wide-open-range), calculating the "orderTotal" and then using "setOrderTotal()" back in the original "Order" class. (Such a scenario is how we used to do things in BASIC!!)
I think that is the #1 scenario that I took from the article and comments I've seen online.
Does that mean that you never need to Get/Set properties in an object? No. But I think the author's points were:
1.) Keep "data" and "actions" as close together as possible (read "encapsulation")
2.) When an Object cannot complete something on its own, pass the ENTIRE OBJECT to an Object that can, versus cherry-picking Attributes and treating them like global variables.
If you have coffee (Attribute) in a Cup (Object), and you want to warm up your coffee, you do not pour the coffee (get) into your hands (rebel environment) and put it in the microwave (another Object) and then when it is warm transfer it back (set) to your Cup?! "In the real world" you would simply take the coffee (Attribute) in the Cup (Object) and place it in the Microwave (another Object) so that you can Warm Up (Microwave Method) Coffee (Cup Attribue) in your Cup (Object), right?!
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
|