What is a precise issue with allowing getters?When are Getters and Setters JustifiedWhat to do with private fields with getters and setters in JavaObject Oriented Programming: getters/setters or logical namesWhat should be allowed inside getters and setters?Getters and Setters unclear exampleHow do you avoid getters and setters?Private setters and gettersHow is encapsulation broken by getters/setters, even when using MVC modelIs there a better way to debug while avoiding getters/setters?OOP in Java - What can getters be used for?
Make me a minimum magic sum
Should I simplify my writing in a foreign country?
Determine if a grid contains another grid
How to remap repeating commands i.e. <number><command>?
Is Iron Man stronger than the Hulk?
In "Avengers: Endgame", what does this name refer to?
Page count conversion from single to double-space for submissions
Looking for sci-fi book based on Hinduism/Buddhism
about academic proof-reading, what to do in this situation?
Can the Tidal Wave spell trigger a vampire's weakness to running water?
What do you call a painting on a wall?
Gerrymandering Puzzle - Rig the Election
Motion-trail-like lines
Should homeowners insurance cover the cost of the home?
What is a common way to tell if an academic is "above average," or outstanding in their field? Is their h-index (Hirsh index) one of them?
How can I get people to remember my character's gender?
Dangerous workplace travelling
Dihedral group D4 composition with custom labels
Simple Derivative Proof?
What is a precise issue with allowing getters?
Who filmed the Apollo 11 trans-lunar injection?
Switch Function Not working Properly
Is space itself expanding or is it just momentum from the Big Bang carrying things apart?
GitLab account hacked and repo wiped
What is a precise issue with allowing getters?
When are Getters and Setters JustifiedWhat to do with private fields with getters and setters in JavaObject Oriented Programming: getters/setters or logical namesWhat should be allowed inside getters and setters?Getters and Setters unclear exampleHow do you avoid getters and setters?Private setters and gettersHow is encapsulation broken by getters/setters, even when using MVC modelIs there a better way to debug while avoiding getters/setters?OOP in Java - What can getters be used for?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am not looking for an opinion about semantics but simply for a case where having getters sensibly used is an actual impediment. Maybe it throws me into a never-ending spiral of relying on them, maybe the alternative is cleaner and handles getters automatically, etc. Something concrete.
I've heard all the arguments, I've heard that they're bad because they force you into treating objects as data sources, that they violate an object's "pure state" of "don't give out too much but be prepared to accept a lot".
But absolutely no sensible reason for why a getData
is a bad thing, in fact, a few people argued that it's a lot about semantics, getters as fine per-se, but just don't name them getX
, to me, this is at least funny.
What is one thing, without opinions, that will break if I use getters sensibly and for data that clearly the object's integrity doesn't break if it puts it out?
Of course that allowing a getter for a string that's used to encrypt something is beyond dumb, but I'm talking about data that your system needs to function. Maybe your data is pulled through a Provider
from the object, but, still, the object still needs to allow the Provider
to do a $provider[$object]->getData
, there's no way around it.
Why I'm asking: To me, getters, when used sensibly and on data that is treated as "safe" are god-sent, 99% of my getters are used to identify the object, as in, I ask, through code Object, what is your name? Object, what is your identifier?
, anyone working with an object should know these things about an object, because nearly everything about programming is identity and who else knows better what it is than the object itself? So I fail to see any real issues unless you're a purist.
I've looked at all the StackOverflow questions about "why getters / setters" are bad and though I agree that setters are really bad in 99% of the cases, getters don't have to be treated the same just because they rhyme.
A setter will compromise your object's identity and make it very hard to debug who's changing the data, but a getter is doing nothing.
object-oriented
add a comment |
I am not looking for an opinion about semantics but simply for a case where having getters sensibly used is an actual impediment. Maybe it throws me into a never-ending spiral of relying on them, maybe the alternative is cleaner and handles getters automatically, etc. Something concrete.
I've heard all the arguments, I've heard that they're bad because they force you into treating objects as data sources, that they violate an object's "pure state" of "don't give out too much but be prepared to accept a lot".
But absolutely no sensible reason for why a getData
is a bad thing, in fact, a few people argued that it's a lot about semantics, getters as fine per-se, but just don't name them getX
, to me, this is at least funny.
What is one thing, without opinions, that will break if I use getters sensibly and for data that clearly the object's integrity doesn't break if it puts it out?
Of course that allowing a getter for a string that's used to encrypt something is beyond dumb, but I'm talking about data that your system needs to function. Maybe your data is pulled through a Provider
from the object, but, still, the object still needs to allow the Provider
to do a $provider[$object]->getData
, there's no way around it.
Why I'm asking: To me, getters, when used sensibly and on data that is treated as "safe" are god-sent, 99% of my getters are used to identify the object, as in, I ask, through code Object, what is your name? Object, what is your identifier?
, anyone working with an object should know these things about an object, because nearly everything about programming is identity and who else knows better what it is than the object itself? So I fail to see any real issues unless you're a purist.
I've looked at all the StackOverflow questions about "why getters / setters" are bad and though I agree that setters are really bad in 99% of the cases, getters don't have to be treated the same just because they rhyme.
A setter will compromise your object's identity and make it very hard to debug who's changing the data, but a getter is doing nothing.
object-oriented
add a comment |
I am not looking for an opinion about semantics but simply for a case where having getters sensibly used is an actual impediment. Maybe it throws me into a never-ending spiral of relying on them, maybe the alternative is cleaner and handles getters automatically, etc. Something concrete.
I've heard all the arguments, I've heard that they're bad because they force you into treating objects as data sources, that they violate an object's "pure state" of "don't give out too much but be prepared to accept a lot".
But absolutely no sensible reason for why a getData
is a bad thing, in fact, a few people argued that it's a lot about semantics, getters as fine per-se, but just don't name them getX
, to me, this is at least funny.
What is one thing, without opinions, that will break if I use getters sensibly and for data that clearly the object's integrity doesn't break if it puts it out?
Of course that allowing a getter for a string that's used to encrypt something is beyond dumb, but I'm talking about data that your system needs to function. Maybe your data is pulled through a Provider
from the object, but, still, the object still needs to allow the Provider
to do a $provider[$object]->getData
, there's no way around it.
Why I'm asking: To me, getters, when used sensibly and on data that is treated as "safe" are god-sent, 99% of my getters are used to identify the object, as in, I ask, through code Object, what is your name? Object, what is your identifier?
, anyone working with an object should know these things about an object, because nearly everything about programming is identity and who else knows better what it is than the object itself? So I fail to see any real issues unless you're a purist.
I've looked at all the StackOverflow questions about "why getters / setters" are bad and though I agree that setters are really bad in 99% of the cases, getters don't have to be treated the same just because they rhyme.
A setter will compromise your object's identity and make it very hard to debug who's changing the data, but a getter is doing nothing.
object-oriented
I am not looking for an opinion about semantics but simply for a case where having getters sensibly used is an actual impediment. Maybe it throws me into a never-ending spiral of relying on them, maybe the alternative is cleaner and handles getters automatically, etc. Something concrete.
I've heard all the arguments, I've heard that they're bad because they force you into treating objects as data sources, that they violate an object's "pure state" of "don't give out too much but be prepared to accept a lot".
But absolutely no sensible reason for why a getData
is a bad thing, in fact, a few people argued that it's a lot about semantics, getters as fine per-se, but just don't name them getX
, to me, this is at least funny.
What is one thing, without opinions, that will break if I use getters sensibly and for data that clearly the object's integrity doesn't break if it puts it out?
Of course that allowing a getter for a string that's used to encrypt something is beyond dumb, but I'm talking about data that your system needs to function. Maybe your data is pulled through a Provider
from the object, but, still, the object still needs to allow the Provider
to do a $provider[$object]->getData
, there's no way around it.
Why I'm asking: To me, getters, when used sensibly and on data that is treated as "safe" are god-sent, 99% of my getters are used to identify the object, as in, I ask, through code Object, what is your name? Object, what is your identifier?
, anyone working with an object should know these things about an object, because nearly everything about programming is identity and who else knows better what it is than the object itself? So I fail to see any real issues unless you're a purist.
I've looked at all the StackOverflow questions about "why getters / setters" are bad and though I agree that setters are really bad in 99% of the cases, getters don't have to be treated the same just because they rhyme.
A setter will compromise your object's identity and make it very hard to debug who's changing the data, but a getter is doing nothing.
object-oriented
object-oriented
edited 5 hours ago
coolpasta
asked 5 hours ago
coolpastacoolpasta
30711
30711
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can't write good code without getters.
The reason why isn't because getters don't break encapsulation, they do. It isn't because getters don't tempt people to not bother following OOP and putting methods with the data they act on together. They do. No you need getters because of boundaries.
The ideas of encapsulation and keeping methods together with the data they act on simply don't work when you run into a boundary that keeps you from moving a method and so forces you to move data.
It's really that simple. If you use getters when there is no boundary you end up having no real objects. Everything starts to tend to the procedural. Which works as well as it ever did.
True OOP isn't something you can spread everywhere. It only works within those boundaries.
Those boundaries aren't razor thin. They have code in them. That code can't be OOP. It can't be functional either. No this code has our ideals stripped from it so it can deal with harsh reality.
Michael Fetters called this code fascia after that white connective tissue that holds sections of an orange together.
This is a wonderful way to think about it. It explains why it's ok to have both kinds of code together. Without this perspective many new programmers cling to their ideals hard, then have their hearts broken and give up on these ideals when they hit their first boundary.
The ideals only work in their proper place. Don't give up on them just because they don't work everywhere. Use them where they work. That place is the juicy part that the fascia protects.
A simple example of a boundary is a collection. This holds something and has no idea what it is. How could a collection designer possibly move the behavioral functionality of the held object into the collection when they have no idea what it's going to be holding? You can't. You're up against a boundary. Which is why collections have getters.
Now if you did know, you could move that behavior, and avoid moving state. When you do know, you should. You just don't always know.
Some people just call this being pragmatic. And it is. But it's nice to know why we have to be pragmatic.
add a comment |
Getters violate the Hollywood Principle ("Don't call us, we'll call you")
The Hollywood Principle (aka Inversion of Control) states that you don't call into library code to get things done; rather, the framework calls your code. Because the framework controls things, broadcasting its internal state to its clients is not necessary. You don't need to know.
In its most insidious form, violating the Hollywood Principle means that you're using a getter to obtain information about the state of a class, and then making decisions about which methods to call on that class based on the value that you obtain. it's violation of encapsulation at its finest.
Using a getter implies that you need that value, when you actually don't.
You might actually need that performance improvement
In extreme cases of lightweight objects that must have the maximum possible performance, it's possible (though extremely unlikely) that you can't pay the very small performance penalty that a getter imposes. This won't happen 99.9 percent of the time.
I understand and I'll fly with your truth, that I don't need to know.. But I've reached a spot where I need to. I have aGenerator
object that loops through all myItems
objects, then callsgetName
from eachItem
to do something further. What is the issue with this? Then, in return, theGenerator
spits out formatted strings. This is within my framework, for which I then have an API on top of that people can use to run whatever the users provide but without touching the framework.
– coolpasta
4 hours ago
What is the issue with this?
None that I can see. That's essentially what amap
function does. But that's not the question you asked. You essentially asked "Are there any conditions under which a getter might be inadvisable." I replied with two, but that doesn't mean you abandon setters altogether.
– Robert Harvey♦
4 hours ago
As such, per your own review, do you believe that, assuming my getters are strictly only so that higher "ingester" objects can interview theseItems
to further provide data from the framework to the API, they are...valid? As in, this is a valid approach to retrieving data from objects?
– coolpasta
4 hours ago
1
You're asking the wrong guy that question. I'm a pragmatist; I do whatever best suits my specific programs, and don't put much stock in "principles" unless they serve my purposes.
– Robert Harvey♦
4 hours ago
I'm starting to go that path too. I've tested and tested, opened my API, fed it the worst, messed up internal objects, everything is predictable and at best, a module of my framework fails, in which case it tells the user, moves on and that's it. I really dislike dabbling in semantics, but I thought that there is a case that a lot of people are missing where it could turn into a monster problem later on. Something concise, which you provided, well, at least the second, because the first one is, to me, "let's do it because it's popular".
– coolpasta
4 hours ago
|
show 2 more comments
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "131"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f391428%2fwhat-is-a-precise-issue-with-allowing-getters%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can't write good code without getters.
The reason why isn't because getters don't break encapsulation, they do. It isn't because getters don't tempt people to not bother following OOP and putting methods with the data they act on together. They do. No you need getters because of boundaries.
The ideas of encapsulation and keeping methods together with the data they act on simply don't work when you run into a boundary that keeps you from moving a method and so forces you to move data.
It's really that simple. If you use getters when there is no boundary you end up having no real objects. Everything starts to tend to the procedural. Which works as well as it ever did.
True OOP isn't something you can spread everywhere. It only works within those boundaries.
Those boundaries aren't razor thin. They have code in them. That code can't be OOP. It can't be functional either. No this code has our ideals stripped from it so it can deal with harsh reality.
Michael Fetters called this code fascia after that white connective tissue that holds sections of an orange together.
This is a wonderful way to think about it. It explains why it's ok to have both kinds of code together. Without this perspective many new programmers cling to their ideals hard, then have their hearts broken and give up on these ideals when they hit their first boundary.
The ideals only work in their proper place. Don't give up on them just because they don't work everywhere. Use them where they work. That place is the juicy part that the fascia protects.
A simple example of a boundary is a collection. This holds something and has no idea what it is. How could a collection designer possibly move the behavioral functionality of the held object into the collection when they have no idea what it's going to be holding? You can't. You're up against a boundary. Which is why collections have getters.
Now if you did know, you could move that behavior, and avoid moving state. When you do know, you should. You just don't always know.
Some people just call this being pragmatic. And it is. But it's nice to know why we have to be pragmatic.
add a comment |
You can't write good code without getters.
The reason why isn't because getters don't break encapsulation, they do. It isn't because getters don't tempt people to not bother following OOP and putting methods with the data they act on together. They do. No you need getters because of boundaries.
The ideas of encapsulation and keeping methods together with the data they act on simply don't work when you run into a boundary that keeps you from moving a method and so forces you to move data.
It's really that simple. If you use getters when there is no boundary you end up having no real objects. Everything starts to tend to the procedural. Which works as well as it ever did.
True OOP isn't something you can spread everywhere. It only works within those boundaries.
Those boundaries aren't razor thin. They have code in them. That code can't be OOP. It can't be functional either. No this code has our ideals stripped from it so it can deal with harsh reality.
Michael Fetters called this code fascia after that white connective tissue that holds sections of an orange together.
This is a wonderful way to think about it. It explains why it's ok to have both kinds of code together. Without this perspective many new programmers cling to their ideals hard, then have their hearts broken and give up on these ideals when they hit their first boundary.
The ideals only work in their proper place. Don't give up on them just because they don't work everywhere. Use them where they work. That place is the juicy part that the fascia protects.
A simple example of a boundary is a collection. This holds something and has no idea what it is. How could a collection designer possibly move the behavioral functionality of the held object into the collection when they have no idea what it's going to be holding? You can't. You're up against a boundary. Which is why collections have getters.
Now if you did know, you could move that behavior, and avoid moving state. When you do know, you should. You just don't always know.
Some people just call this being pragmatic. And it is. But it's nice to know why we have to be pragmatic.
add a comment |
You can't write good code without getters.
The reason why isn't because getters don't break encapsulation, they do. It isn't because getters don't tempt people to not bother following OOP and putting methods with the data they act on together. They do. No you need getters because of boundaries.
The ideas of encapsulation and keeping methods together with the data they act on simply don't work when you run into a boundary that keeps you from moving a method and so forces you to move data.
It's really that simple. If you use getters when there is no boundary you end up having no real objects. Everything starts to tend to the procedural. Which works as well as it ever did.
True OOP isn't something you can spread everywhere. It only works within those boundaries.
Those boundaries aren't razor thin. They have code in them. That code can't be OOP. It can't be functional either. No this code has our ideals stripped from it so it can deal with harsh reality.
Michael Fetters called this code fascia after that white connective tissue that holds sections of an orange together.
This is a wonderful way to think about it. It explains why it's ok to have both kinds of code together. Without this perspective many new programmers cling to their ideals hard, then have their hearts broken and give up on these ideals when they hit their first boundary.
The ideals only work in their proper place. Don't give up on them just because they don't work everywhere. Use them where they work. That place is the juicy part that the fascia protects.
A simple example of a boundary is a collection. This holds something and has no idea what it is. How could a collection designer possibly move the behavioral functionality of the held object into the collection when they have no idea what it's going to be holding? You can't. You're up against a boundary. Which is why collections have getters.
Now if you did know, you could move that behavior, and avoid moving state. When you do know, you should. You just don't always know.
Some people just call this being pragmatic. And it is. But it's nice to know why we have to be pragmatic.
You can't write good code without getters.
The reason why isn't because getters don't break encapsulation, they do. It isn't because getters don't tempt people to not bother following OOP and putting methods with the data they act on together. They do. No you need getters because of boundaries.
The ideas of encapsulation and keeping methods together with the data they act on simply don't work when you run into a boundary that keeps you from moving a method and so forces you to move data.
It's really that simple. If you use getters when there is no boundary you end up having no real objects. Everything starts to tend to the procedural. Which works as well as it ever did.
True OOP isn't something you can spread everywhere. It only works within those boundaries.
Those boundaries aren't razor thin. They have code in them. That code can't be OOP. It can't be functional either. No this code has our ideals stripped from it so it can deal with harsh reality.
Michael Fetters called this code fascia after that white connective tissue that holds sections of an orange together.
This is a wonderful way to think about it. It explains why it's ok to have both kinds of code together. Without this perspective many new programmers cling to their ideals hard, then have their hearts broken and give up on these ideals when they hit their first boundary.
The ideals only work in their proper place. Don't give up on them just because they don't work everywhere. Use them where they work. That place is the juicy part that the fascia protects.
A simple example of a boundary is a collection. This holds something and has no idea what it is. How could a collection designer possibly move the behavioral functionality of the held object into the collection when they have no idea what it's going to be holding? You can't. You're up against a boundary. Which is why collections have getters.
Now if you did know, you could move that behavior, and avoid moving state. When you do know, you should. You just don't always know.
Some people just call this being pragmatic. And it is. But it's nice to know why we have to be pragmatic.
edited 1 hour ago
answered 2 hours ago
candied_orangecandied_orange
56k17106194
56k17106194
add a comment |
add a comment |
Getters violate the Hollywood Principle ("Don't call us, we'll call you")
The Hollywood Principle (aka Inversion of Control) states that you don't call into library code to get things done; rather, the framework calls your code. Because the framework controls things, broadcasting its internal state to its clients is not necessary. You don't need to know.
In its most insidious form, violating the Hollywood Principle means that you're using a getter to obtain information about the state of a class, and then making decisions about which methods to call on that class based on the value that you obtain. it's violation of encapsulation at its finest.
Using a getter implies that you need that value, when you actually don't.
You might actually need that performance improvement
In extreme cases of lightweight objects that must have the maximum possible performance, it's possible (though extremely unlikely) that you can't pay the very small performance penalty that a getter imposes. This won't happen 99.9 percent of the time.
I understand and I'll fly with your truth, that I don't need to know.. But I've reached a spot where I need to. I have aGenerator
object that loops through all myItems
objects, then callsgetName
from eachItem
to do something further. What is the issue with this? Then, in return, theGenerator
spits out formatted strings. This is within my framework, for which I then have an API on top of that people can use to run whatever the users provide but without touching the framework.
– coolpasta
4 hours ago
What is the issue with this?
None that I can see. That's essentially what amap
function does. But that's not the question you asked. You essentially asked "Are there any conditions under which a getter might be inadvisable." I replied with two, but that doesn't mean you abandon setters altogether.
– Robert Harvey♦
4 hours ago
As such, per your own review, do you believe that, assuming my getters are strictly only so that higher "ingester" objects can interview theseItems
to further provide data from the framework to the API, they are...valid? As in, this is a valid approach to retrieving data from objects?
– coolpasta
4 hours ago
1
You're asking the wrong guy that question. I'm a pragmatist; I do whatever best suits my specific programs, and don't put much stock in "principles" unless they serve my purposes.
– Robert Harvey♦
4 hours ago
I'm starting to go that path too. I've tested and tested, opened my API, fed it the worst, messed up internal objects, everything is predictable and at best, a module of my framework fails, in which case it tells the user, moves on and that's it. I really dislike dabbling in semantics, but I thought that there is a case that a lot of people are missing where it could turn into a monster problem later on. Something concise, which you provided, well, at least the second, because the first one is, to me, "let's do it because it's popular".
– coolpasta
4 hours ago
|
show 2 more comments
Getters violate the Hollywood Principle ("Don't call us, we'll call you")
The Hollywood Principle (aka Inversion of Control) states that you don't call into library code to get things done; rather, the framework calls your code. Because the framework controls things, broadcasting its internal state to its clients is not necessary. You don't need to know.
In its most insidious form, violating the Hollywood Principle means that you're using a getter to obtain information about the state of a class, and then making decisions about which methods to call on that class based on the value that you obtain. it's violation of encapsulation at its finest.
Using a getter implies that you need that value, when you actually don't.
You might actually need that performance improvement
In extreme cases of lightweight objects that must have the maximum possible performance, it's possible (though extremely unlikely) that you can't pay the very small performance penalty that a getter imposes. This won't happen 99.9 percent of the time.
I understand and I'll fly with your truth, that I don't need to know.. But I've reached a spot where I need to. I have aGenerator
object that loops through all myItems
objects, then callsgetName
from eachItem
to do something further. What is the issue with this? Then, in return, theGenerator
spits out formatted strings. This is within my framework, for which I then have an API on top of that people can use to run whatever the users provide but without touching the framework.
– coolpasta
4 hours ago
What is the issue with this?
None that I can see. That's essentially what amap
function does. But that's not the question you asked. You essentially asked "Are there any conditions under which a getter might be inadvisable." I replied with two, but that doesn't mean you abandon setters altogether.
– Robert Harvey♦
4 hours ago
As such, per your own review, do you believe that, assuming my getters are strictly only so that higher "ingester" objects can interview theseItems
to further provide data from the framework to the API, they are...valid? As in, this is a valid approach to retrieving data from objects?
– coolpasta
4 hours ago
1
You're asking the wrong guy that question. I'm a pragmatist; I do whatever best suits my specific programs, and don't put much stock in "principles" unless they serve my purposes.
– Robert Harvey♦
4 hours ago
I'm starting to go that path too. I've tested and tested, opened my API, fed it the worst, messed up internal objects, everything is predictable and at best, a module of my framework fails, in which case it tells the user, moves on and that's it. I really dislike dabbling in semantics, but I thought that there is a case that a lot of people are missing where it could turn into a monster problem later on. Something concise, which you provided, well, at least the second, because the first one is, to me, "let's do it because it's popular".
– coolpasta
4 hours ago
|
show 2 more comments
Getters violate the Hollywood Principle ("Don't call us, we'll call you")
The Hollywood Principle (aka Inversion of Control) states that you don't call into library code to get things done; rather, the framework calls your code. Because the framework controls things, broadcasting its internal state to its clients is not necessary. You don't need to know.
In its most insidious form, violating the Hollywood Principle means that you're using a getter to obtain information about the state of a class, and then making decisions about which methods to call on that class based on the value that you obtain. it's violation of encapsulation at its finest.
Using a getter implies that you need that value, when you actually don't.
You might actually need that performance improvement
In extreme cases of lightweight objects that must have the maximum possible performance, it's possible (though extremely unlikely) that you can't pay the very small performance penalty that a getter imposes. This won't happen 99.9 percent of the time.
Getters violate the Hollywood Principle ("Don't call us, we'll call you")
The Hollywood Principle (aka Inversion of Control) states that you don't call into library code to get things done; rather, the framework calls your code. Because the framework controls things, broadcasting its internal state to its clients is not necessary. You don't need to know.
In its most insidious form, violating the Hollywood Principle means that you're using a getter to obtain information about the state of a class, and then making decisions about which methods to call on that class based on the value that you obtain. it's violation of encapsulation at its finest.
Using a getter implies that you need that value, when you actually don't.
You might actually need that performance improvement
In extreme cases of lightweight objects that must have the maximum possible performance, it's possible (though extremely unlikely) that you can't pay the very small performance penalty that a getter imposes. This won't happen 99.9 percent of the time.
answered 5 hours ago
Robert Harvey♦Robert Harvey
168k44389603
168k44389603
I understand and I'll fly with your truth, that I don't need to know.. But I've reached a spot where I need to. I have aGenerator
object that loops through all myItems
objects, then callsgetName
from eachItem
to do something further. What is the issue with this? Then, in return, theGenerator
spits out formatted strings. This is within my framework, for which I then have an API on top of that people can use to run whatever the users provide but without touching the framework.
– coolpasta
4 hours ago
What is the issue with this?
None that I can see. That's essentially what amap
function does. But that's not the question you asked. You essentially asked "Are there any conditions under which a getter might be inadvisable." I replied with two, but that doesn't mean you abandon setters altogether.
– Robert Harvey♦
4 hours ago
As such, per your own review, do you believe that, assuming my getters are strictly only so that higher "ingester" objects can interview theseItems
to further provide data from the framework to the API, they are...valid? As in, this is a valid approach to retrieving data from objects?
– coolpasta
4 hours ago
1
You're asking the wrong guy that question. I'm a pragmatist; I do whatever best suits my specific programs, and don't put much stock in "principles" unless they serve my purposes.
– Robert Harvey♦
4 hours ago
I'm starting to go that path too. I've tested and tested, opened my API, fed it the worst, messed up internal objects, everything is predictable and at best, a module of my framework fails, in which case it tells the user, moves on and that's it. I really dislike dabbling in semantics, but I thought that there is a case that a lot of people are missing where it could turn into a monster problem later on. Something concise, which you provided, well, at least the second, because the first one is, to me, "let's do it because it's popular".
– coolpasta
4 hours ago
|
show 2 more comments
I understand and I'll fly with your truth, that I don't need to know.. But I've reached a spot where I need to. I have aGenerator
object that loops through all myItems
objects, then callsgetName
from eachItem
to do something further. What is the issue with this? Then, in return, theGenerator
spits out formatted strings. This is within my framework, for which I then have an API on top of that people can use to run whatever the users provide but without touching the framework.
– coolpasta
4 hours ago
What is the issue with this?
None that I can see. That's essentially what amap
function does. But that's not the question you asked. You essentially asked "Are there any conditions under which a getter might be inadvisable." I replied with two, but that doesn't mean you abandon setters altogether.
– Robert Harvey♦
4 hours ago
As such, per your own review, do you believe that, assuming my getters are strictly only so that higher "ingester" objects can interview theseItems
to further provide data from the framework to the API, they are...valid? As in, this is a valid approach to retrieving data from objects?
– coolpasta
4 hours ago
1
You're asking the wrong guy that question. I'm a pragmatist; I do whatever best suits my specific programs, and don't put much stock in "principles" unless they serve my purposes.
– Robert Harvey♦
4 hours ago
I'm starting to go that path too. I've tested and tested, opened my API, fed it the worst, messed up internal objects, everything is predictable and at best, a module of my framework fails, in which case it tells the user, moves on and that's it. I really dislike dabbling in semantics, but I thought that there is a case that a lot of people are missing where it could turn into a monster problem later on. Something concise, which you provided, well, at least the second, because the first one is, to me, "let's do it because it's popular".
– coolpasta
4 hours ago
I understand and I'll fly with your truth, that I don't need to know.. But I've reached a spot where I need to. I have a
Generator
object that loops through all my Items
objects, then calls getName
from each Item
to do something further. What is the issue with this? Then, in return, the Generator
spits out formatted strings. This is within my framework, for which I then have an API on top of that people can use to run whatever the users provide but without touching the framework.– coolpasta
4 hours ago
I understand and I'll fly with your truth, that I don't need to know.. But I've reached a spot where I need to. I have a
Generator
object that loops through all my Items
objects, then calls getName
from each Item
to do something further. What is the issue with this? Then, in return, the Generator
spits out formatted strings. This is within my framework, for which I then have an API on top of that people can use to run whatever the users provide but without touching the framework.– coolpasta
4 hours ago
What is the issue with this?
None that I can see. That's essentially what a map
function does. But that's not the question you asked. You essentially asked "Are there any conditions under which a getter might be inadvisable." I replied with two, but that doesn't mean you abandon setters altogether.– Robert Harvey♦
4 hours ago
What is the issue with this?
None that I can see. That's essentially what a map
function does. But that's not the question you asked. You essentially asked "Are there any conditions under which a getter might be inadvisable." I replied with two, but that doesn't mean you abandon setters altogether.– Robert Harvey♦
4 hours ago
As such, per your own review, do you believe that, assuming my getters are strictly only so that higher "ingester" objects can interview these
Items
to further provide data from the framework to the API, they are...valid? As in, this is a valid approach to retrieving data from objects?– coolpasta
4 hours ago
As such, per your own review, do you believe that, assuming my getters are strictly only so that higher "ingester" objects can interview these
Items
to further provide data from the framework to the API, they are...valid? As in, this is a valid approach to retrieving data from objects?– coolpasta
4 hours ago
1
1
You're asking the wrong guy that question. I'm a pragmatist; I do whatever best suits my specific programs, and don't put much stock in "principles" unless they serve my purposes.
– Robert Harvey♦
4 hours ago
You're asking the wrong guy that question. I'm a pragmatist; I do whatever best suits my specific programs, and don't put much stock in "principles" unless they serve my purposes.
– Robert Harvey♦
4 hours ago
I'm starting to go that path too. I've tested and tested, opened my API, fed it the worst, messed up internal objects, everything is predictable and at best, a module of my framework fails, in which case it tells the user, moves on and that's it. I really dislike dabbling in semantics, but I thought that there is a case that a lot of people are missing where it could turn into a monster problem later on. Something concise, which you provided, well, at least the second, because the first one is, to me, "let's do it because it's popular".
– coolpasta
4 hours ago
I'm starting to go that path too. I've tested and tested, opened my API, fed it the worst, messed up internal objects, everything is predictable and at best, a module of my framework fails, in which case it tells the user, moves on and that's it. I really dislike dabbling in semantics, but I thought that there is a case that a lot of people are missing where it could turn into a monster problem later on. Something concise, which you provided, well, at least the second, because the first one is, to me, "let's do it because it's popular".
– coolpasta
4 hours ago
|
show 2 more comments
Thanks for contributing an answer to Software Engineering Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f391428%2fwhat-is-a-precise-issue-with-allowing-getters%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown