-
-
Notifications
You must be signed in to change notification settings - Fork 864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JavaScript getter/setters (S|Get keyword) #49
Comments
Awesome ! |
Hey, @jenbutongit. Thanks for proposing this!
I can't agree with this. The example explicitly features a standalone function, and not a class method, to highlight that it's not bound to OO principles. Getters/setters are specific language features, and while those are also functions, I trust common sense to differentiate between them and plain functions.
Agree on this one. The I would still keep it a standalone function, detached from the getters/setters of a class (neither
Yeah, once again because you're looking at the function as a practical example while it's value is purely conceptual here. That's a drawback of the current example, we should fix that.
Your examples are fantastic! 🎉 I would be all hands for changing the existing get/set examples to yours. Would you like to open a pull request with that change, please? |
Regarding getters/setters, I have really limited knowledge of programming languages, so I'm not confident to say if that's a generic pattern present everywhere or the one that heavily manifests in OO languages like Java. This would define how relevant their inclusion in the guidelines would be to all developers. I think when you're naming a getter/setter, the Avoid context duplication should help you rather well. If you're declaring a getter, there's no need to name your getter method starting with |
@kettanaito RE: getters/setters, most (probably all) OO languages will have some manifestation of getters and setters. In Java they would just be get/set in swift will work in the same way as JavaScript IIRC. And agreed on context duplication. I’ll have to have a reread of everything and open up a PR (it’s been a while since I opened this!). |
No worries! Take your time :) Thank you for looking into this. |
👋 hey, this is a great repo/cheatsheet, thanks for this!
A couple points on your get/set examples, in short, they are confusing when compared with each other and conflicts with OO principles.
"Accesses data immediately (i.e. shorthand getter of internal data)."
this suggests that getFruitCount is part of either a class or object, which has the property
fruits
with type array (or other iterable).with the example above to get the number of fruits in the bowl, you can do either
bowl.getFruitCount()
orbowl.fruitCount
. If you find you are using getFruitCount often in your code, or you are passing it into another function, it can get unreadable/extraneous quite quickly.I think this example should either match your get example, or be completely different so it's not confusing, since get/set on an object is thought of to be a matching pair. Currently, I don't see a "benefit" of using this set function over a simple
fruits = randomNumber()
orfruits = 5
especially with the current scoping. If you were to match the getter method (get keyword), the example would look like this.the key words, in js and other languages usually allow for those sorts of protections against setting (and getting a private field without exposing the field's value directly).
To avoid this confusion, I think you should either explain getters/setters vs a function with get/set in the name and provide an example of both, or more simply, use a pure function as an example.
The text was updated successfully, but these errors were encountered: