gman
03/14/2019, 5:22 PMthis
or self
class Rect {
int width;
int height;
int area() {
return width + height; // implicit
}
}
vs
class Rect {
int width;
int height;
int area() {
return this.width + this.height; // explicit
}
}
I feel like I prefer explicit over implicit. IMO it makes the code more readable since I can look at the only the line in question a little easier. In the implicit case when area()
is defined many lines below I'd have no idea if width
and height
are variables in a more outer scope, global variables, or member variables. In the explicit case I immediately know they are member variables. That seems like a win. It also means one less argument over naming conventions. Microsoft use to use m_variable
. Google uses variable_
. That would all disappear if the language required explicitness like python and JavaScript for example.
What are some negatives? I can see one being typing but if for all other reasons explicit is better than implicit then some language could just shorten the syntax. _.height
for example.David Piepgrass
03/14/2019, 7:33 PMthis.
a lot, so you'll catch me writing let foo = this.foo
in JS which just adds noise.
Instead I'd propose that globals should be specially qualified instead. Ruby's convention of qualifying both $globals
and @members
is good too. And it doesn't necessarily have to be the programmer doing the qualification - the editor could colorize or add a sigil to indicate the scope instead.Iridian
03/14/2019, 8:45 PMthis.
one doesn't need to think.
Code is written less often than it is read, and I believe this applies even when I'm working on the codebase alone. I'm not a poor typist.Dan Cook
03/14/2019, 10:51 PMwtaysom
03/15/2019, 12:02 AMIridian
03/15/2019, 1:02 AMDan Cook
03/15/2019, 4:43 AMfunction foo(x) {
...
return x;
}
That "x" is not short for "foo.x". There isn't a "this" that holds x, it's just the x in the current lexical scope. And you're not going to confuse the x in foo(123) with the x in foo(456), because both calls create a different "instance" of that scope.
Closures are about holding onto values in that scope, rather than attaching them to some "this" object. For example:
function plus(x) {
return function(y) {
return x + y;
}
}
var plus2 = plus(2);
var plus4 = plus(4);
plus2(5); // result is 7
plus4(5); // result is 9
Plus takes some x, and returns a function that adds that x to some y. Each call to plus activates a new "instance" of that scope, with its own value for x, and returns a new function that uses that x.
When you want to create multiple functions for one set values, you can return an object that has them as properties. This might be where it gets confusing, because it looks like member access:
function foo(x, y) {
function sum() { return x + y; }
function diff() { return x - y; }
function inc() { x = x + 1; }
return { sum, diff, inc };
}
var a = foo(1,2);
var b = foo(3,4);
a.sum(); a.diff(); // 3, -1
b.sum(); b.diff(); // 7, -1
a.inc();
a.sum(); a.diff(); // 4, 0
b.sum(); b.diff(); // 7, -1
Iridian
03/15/2019, 5:12 AMDan Cook
03/15/2019, 5:17 AMIridian
03/15/2019, 5:22 AMIridian
03/15/2019, 5:26 AMDaniel Garcia
03/15/2019, 5:35 AMBosmon
03/15/2019, 12:16 PMshalabh
03/15/2019, 8:25 PMDavid Piepgrass
03/15/2019, 8:49 PMBosmon
03/15/2019, 11:05 PMIridian
03/15/2019, 11:48 PMDavid Piepgrass
03/15/2019, 11:54 PMBosmon
03/16/2019, 9:21 PMEdward de Jong / Beads Project
03/17/2019, 4:35 AMDan Cook
03/17/2019, 5:28 PMEdward de Jong / Beads Project
03/18/2019, 7:08 AMBosmon
03/18/2019, 2:48 PMDavid Piepgrass
03/22/2019, 6:18 PM