3 ms·
So I get that calc_formula(a, b, false) Is unclear but so are `a` and `b` ctx.arc(100, 200, 300, 0, Math.PI*2, false) or gl.texImage2D(gl.TEXT
by greggman 11y ago
So I get that
calc_formula(a, b, false)
Is unclear but so are `a` and `b`
ctx.arc(100, 200, 300, 0, Math.PI*2, false)
or
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 100, 200, 0,
gl.RGBA, gl.UNSIGNED_BYTE, null);
How are those examples any more self documenting? I don't see anything special about boolean anymore than the other arguments. If you really want your code to be clear (and I'm not saying I do this) you'd need to either use a language that allows naming every argument or else put every argument in a variable with a descriptive name
var centerX = 100;
var centerY = 200;
var radius = 300;
var arcStartRadians = 0;
var arcEndRadians = Math.PI * 2;
var counterClockwise = false;
ctx.arc(centerX, centerY, radius,
arcStartRadians, arcEndRadians,
counterClockwise);
- paulmd 11y agoI'll say that I do that (define constants to variables/consts/#DEFINE/etc and then use them as arguments), and I do it for exactly the reasons you describe. I also try to make magic numbers as self-explanatory as possible - rather than (say) use the number 84600 I'll write 60 * 60 * 24 for when I want the number of seconds per day. It's way easier to remember what's going on when you come back a year later, and all but the simplest compilers will be smart enough to bash it down to constant parameters if that's possible. Yeah, more lines of code, but I think readability trumps terseness (within reason). Some functions just use a bunch of parameters and it's not really the extra line to name the parameter that's causing the issue. Also if you're using an editor that does code folding you could just fold that block away.
- chrisheller 11y agoI'm not sure if the transposition was intended, but your comment shows another reason why using a calculated value here is good.
- cnvogel 11y agoPersonally, I tend to put the function arguments' names from the API documentation in comments if its kind of obscure or I guess that I'll forget if I need to revisit this piece of code. As in... texImage2D(100, 200, 300, 0 /*start*/, Math.PI*2 /*end*/, /*ctrClockwise*/ false);
- JadeNB 11y agoDoesn't Smalltalk have a convention whereby the name of the function specifies the purpose of the variables, like arcWithCenterX: withCenterY: or similar? (I vaguely recall this, but can't find any examples online.)
- jurip 11y agoYeah, and Objective-C and now Swift inherited that same convention. Objective-C: [self frob:param1 withObject:param2]; Swift: self.frob(param1, withObject:param2)
- protomyth 11y agoafter going through and starting to learn Swift, I really wish self.frob(param1, withObject:param2) could be written as self.[frob:param1 withObject:param2]