11 ms·
I 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 h
by donatj 10y ago
I 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.