9 ms·
The world would be a much better place if self(Python) and this(Javascript ES6 classes) were implicit.
by mschuetz 10y ago
The world would be a much better place if self(Python) and this(Javascript ES6 classes) were implicit.
- donatj 10y agoI disagree heartedly. Implicit code leads to easy to write hard to read. I'd much rather have where you are getting a value from be very explicit to not cause hard to see bugs as well as making the code easier to understand from a fragment.
- keymone 10y agofrom all the code i've seen javascript developers have more troubles with explicit "this" and bindings than ruby developers have with implicit self.
- dagw 10y agoJavascript 'this' binding behaves in quite unexpected ways if you're coming from something like python on ruby, which is a major source of trouble for people.
- masklinn 10y agoDo people really use implicit selfs in ruby? Note that @foo is not an implicit self.
- pmontra 10y agoself.attribute = attribute in an ActiveRecord class when the left side is a field of a table and the right side is a variable. A different name for the variable fixes it without using self.
- masklinn 10y ago> self.attribute = attribute in an ActiveRecord class when the left side is a field of a table and the right side is a variable That seems to be an example of not using an implicit self. > A different name for the variable fixes it There's nothing to fix, it works just fine.
- matthewmacleod 10y agoLiterally all the time. In fact, our coding style demands that we avoid using instance variables in preference of adding `attr_accessor` to explicitly encourage the use of implicit self.
- keymone 10y agoevery time you call a method without explicit receiver you're using implicit self
- mschuetz 10y agoI find it unnecessarely bloats code and in most cases makes it harder to read due to redundant text, e.g. # 1 def length(self): return math.sqrt(self.x*self.x + self.y*self.y + self.z*self.z) # 2 def length(self): return math.sqrt(self.x**2 + self.y**2 + self.z**2) # 3 def length(): return math.sqrt(x*x + y*y + z*z) I prefer version 3 by far. Unfortunately, Javascript decided to take the same path with ES6 classes which forces you to use this in the body. Fortunately, it does not force you to use this in the argument list.
- methyl 10y agoFor JS, you can do function length() { const { x, y, z } = this return math.sqrt(x * x + y * y + z * z) } I think it's pretty short, readable and explicit. EDIT: formatting
- fnl 10y agoAnd make every one-liner a two-liner?
- Tarean 10y agoIn rust you can use pattern matching in arguments: struct Foo { x: f64, y: f64, z: f64 } fn length(Foo {x, y, z}: Foo) -> f64 { (x*x + y*y + z*z).sqrt() } ...Doesn't work with `self` because that would be implicit again, though. Anyway, I think that it is a minor inconvenience that isn't important for oneliners and extremely helpful when reading larger functions.
- masklinn 10y ago> Doesn't work with `self` because that would be implicit again, though. AFAIK it doesn't work on self because the [&[mut ]]self parameter is more or less a keyword determining ownership interaction with the call subject. The UFCS RFC would have made it sugar for `self: [&[mut ]]Self` (IIRC) but I believe that floundered.
- kmm 10y agoI would have liked a shorthand, like prefixing `@`. E.g, `self.member` would be `@member`. It's a lot less to type.
- twic 10y agoPoint of order: this comment has been downvoted into greyness. My understanding is that we should downvote when a comment is low-value, not when we disagree with it, but i suspect the latter has happened here. Could anyone who has or is tempted to downvote this instead make a comment to present their critique?
- dagw 10y agoDon't complain about down votes, especially after only 15 minutes. There is inherent randomness in votes and it only takes one or two votes to grey out a comment. Most of the time these things work themselves out within an hour or two. In this case the comment was black within 12 minutes of your post.
- hawski 10y agoFor example in C++ where it's implicit most code styles require adding prefix "m_" or something similar to indicate that the variable is a class member. So then I expect the same would happen in Python and JavaScript. I don't think it would be better that way.
- gpderetta 10y agoExactly. Implicit 'this' also complicates the lookup rules. explicit 'this' should have been a requirement in c++ (and a reference, not a pointer). edit: Also, it is already required for dependent names in templates.
- cmdrfred 10y ago"Explicit is better than implicit." https://www.python.org/dev/peps/pep-0020/ https://www.python.org/dev/peps/pep-0020/
- mschuetz 10y ago"Beautiful is better than ugly." https://www.python.org/dev/peps/pep-0020/ https://www.python.org/dev/peps/pep-0020/