This is what I call a \"combined\" getter/setter method (from jQuery): var foo =
ID: 646651 • Letter: T
Question
This is what I call a "combined" getter/setter method (from jQuery):
var foo = $("<div>This is my HTML</div>"),
myText;
myText = foo.text(); // myHTML now equals "This is my HTML" (Getter)
foo.text("This is a new value"); // The text now equals "This is a new value")
This is the same logic with separate (theoretical) methods:
var foo = $("<div>This is my HTML</div>"),
myText;
myText = foo.getText(); // myHTML now equals "This is my HTML" (Getter)
foo.setText("This is a new value"); // The text now equals "This is a new value")
My Question:
When designing a library like jQuery, why would you decide to go the first route and not the second? Isn't the second approach clearer and easier to understand at a glance?
Explanation / Answer
Purely speculating here, but I would be inclined to believe the jQuery developers who used a lot of the functional styling in structuring jQuery clearly have a leaning towards the functional paradigm.
Given that, in the functional paradigm there is a strong culture of reducing redundancy, and it could be argued that the second approach has unnecessary redundancy. The first approach may not be clear to someone used to all the common imperative languages, but it is unarguably doing more with less by having only one method instead of two. This may be dangerous, but it is a common tool in the functional paradigm and something one quickly gets used to.
Practice in the functional paradigm and you quickly get over that desire towards ceremony and learn to appreciate the ability to do more with less. Though this is recognizably a subjective choice based on preference in the same way many people have preferences regarding whitespace management. In this way I'm not saying one way is better, rather that they are things people get used to, which is likely why jQuery has the approach it does.
This would be why I believe the jQuery developers did this and is in the general why someone might take this approach.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.