4 ms·
I don’t want to come off too harsh, so I’ll preface that I respect the author and mean all of what follows constructively. With that said, this seems like a re
by eyelidlessness 6y ago
I don’t want to come off too harsh, so I’ll preface that I respect the author and mean all of what follows constructively.
With that said, this seems like a recipe for debugging nightmares. Getters and setters already make life difficult, hiding statefulness and complexity in a way that’s not apparent at the call site (you don’t even know it’s a call site!). Adding magic where sometimes a value is computed and sometimes it’s not amplifies those problems.
Maybe my FP is showing, but all of this could be much more easily accomplished with plain value type data structures and functions. Computed values can be derived either at construction time (ideal, fewer nullish properties and less chance of passing incomplete values off to something that expects them to be complete) or by passing the structure to a function (or calling a method if you must, but here `this` is just an implicit argument to allow access to other properties of the structure).
Replacing a computed value is as simple as constructing a new value structure with the value set explicitly.
All of that is a lot easier to reason about (in terms of just reading the source to see what it does), almost certainly has better performance characteristics, and definitely makes concurrency less complex.
These principles work very well in a general FP environment, but they can be applied to great benefit even in more imperative environments like JS.
- astrobe_ 6y agoWithout the "getter syntactic sugar": let lea = { name:"Lea verou", getId:function() { return this.name.toLowerCase().replace(/\W+/g, "-");} }; lea.getId() // lea-verou lea.getId=function() { return this.name; } lea.getId() // Lea Verou "syntactic sugar causes cancer of the semicolon" -- A. Perlis That said, I don't use JS and I agree it still looks like a bad idea, although this is not too heretic in a prototype-based-like language.
- eyelidlessness 6y agoI’m on mobile so forgive any syntax mistakes, but I’m talking more along the lines: const createLea = (data) => ({ ...data, id: ( data.id ?? data.name .toLowerCase() .replace(/\W+/g, '-') ), }); No mutation, no getters (function or implicit), dead simple to use, dead simple to reassign (just call the function again with the same data but provide a new `id` and super easy to understand at a glance (easier with types, but I digress). > prototype-based-like language One of the benefits of the approach I describe is you’re no longer working in a prototype based language. I mean, of course you are, but you don’t have to care. Edit: even on mobile I couldn’t stand the formatting. Edit 2: since I mentioned performance in my original comment, I’d add that there are two benefits here (one real, one possible with JITs): getters and setters (and anything with Object.defineProperty) perform worse than regular values; if you program in this style, the JIT has an opportunity to recognize that your values are generally immutable and optimize object construction e.g. with the spread operator as persistent data structures the same way FP languages do.
- drivingmenuts 6y agoJust seems like a bad idea - violating the idea of a getter and increasing complexity. Why have a getter in the first place? The proper place for this mess is the setter.
- eyelidlessness 6y agoWhy have either? Why even set a value more than once except as an optimization?
- drivingmenuts 6y agoI misspoke about the getter (was focused on getting dinner). I should stop at saying the purpose of the getter is to return the value. Using it as the setter as well confuses the purpose for the hidden property just smells wrong. This is my subjective opinion.
- rektide 6y agoseem like you have some Fear Uncertainty & Doubt (FUD) about this language feature. rather than cut off this feature & not use it, do you have any thoughts for how an ide or debugger might present some up front information about the nature of said objects? how can we reduce the risk of using the language via tooling? it'd be nice in my opinion to not retreat from the language's flexible, powerful, useful expressiveness?
- eyelidlessness 6y agoI think you’ve misunderstood my reaction. I’m certain, and I have no doubt. I’m not motivated by fear, I’m motivated by experience. These kinds of dynamic features that mimic static features are harmful to code comprehension and maintenance. Since you asked, the tooling is pretty goodish with VSCode if you use typescript, but still misleading for dependencies (getters are just treated as readonly properties, setters are indistinguishable from writeable properties).
- rektide 6y ago> I don’t want to come off too harsh I feel like your original points were kind of ok. But this attitude of superiority & certainty, this conviction that these language features are never the right answer? > I’m not motivated by fear, I’m motivated by experience. In my experience, teams & individuals can have a lot of bad sad mad anecdotes they make for themselves. But other people can find & use the same tools & techniques very stably, successfully, & to good effect. I don't fully believe you, that you have this experience. That you've earned this ability to trash & insult a rather-wide piece of JavaScript & it's use. In a complete & total way, you've said no, that you alone are the only important voice & that you have total knowledge. This isn't only harsh... it's other unfavorable things too. Meddling with descriptors is absolutely something that can go wrong, and it's very hard to know it really for sure is a good case- often you'd be better without. But there are also many good cases where these techniques & knowledge of them is absolutely divine, enables really nice solutions. If you need to instrument some existing 3rd party objects? Fantastic, absolutely fantastic.
- 6y ago
- taion 6y agoInterestingly enough, in Python, the minimal, official style guide of PEP 8 actively suggests using properties (the equivalent of the JS getter/setter syntax) for simple things: https://www.python.org/dev/peps/pep-0008/#designing-for-inheritance https://www.python.org/dev/peps/pep-0008/#designing-for-inhe... (See the section that starts with "For simple public data attributes, it is best to expose just the attribute name, without complicated accessor/mutator methods.") I think in practice this ends up being mostly a function of what's idiomatic in the ecosystem. The most common JS style guide recommends _against_ using getters and setters, so it would be surprising for something in JS to do so. The most common Python style guide recommends _for_ using properties, so the syntactical near-equivalent ends up being something "expected".
- rubyn00bie 6y agoThey also note, in the same section: > Note 2: Try to keep the functional behavior side-effect free, although side-effects such as caching are generally fine. Personally, and I'm happy to to be wrong, I can't think of any situation where mutating data in a getter is a good idea... in any language ever. Every single time I've caught myself thinking about it, if I take a step back, what I'm about to do is hot garbage and thankfully my hot-garbage-sense caught on.
- fulafel 6y agoI think the context is important here - it's in the "Designing for Inheritance" section. If you're doing that, you're already pretty far in the OO thicket which a lot of Python users prefer to avoid. Actively encouraging properties is not generally a widely held view.