Re:Get rid of boolean function parameters
by greggman on Monday June 15, 2015 @10:06AM
(#9719320)
So I get that
Is unclear but so are `a` and `b`
or
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
[ Parent
| on HN ]
I'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.
Re:Get rid of boolean function parameters
by chrisheller on Monday June 15, 2015 @10:50AM
(#9719658)
I'm not sure if the transposition was intended, but your comment shows another reason why using a calculated value here is good.
Doesn'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.)Yeah, and Objective-C and now Swift inherited that same convention.
Objective-C:
[self frob:param1 withObject:param2];
Swift: self.frob(param1, withObject:param2)after going through and starting to learn Swift, I really wish
self.frob(param1, withObject:param2)
could be written as self.[frob:param1 withObject:param2]Personally, 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);