4 ms·
I noticed a variable named retval in a few slides and was irritated for a moment. Maybe because I stopped using variable names like "retval", "data" or even "r
by mtrn 11y ago
I noticed a variable named retval in a few slides and was irritated for a moment.
Maybe because I stopped using variable names like "retval", "data" or even "result" - because they convey so little meaning.
- logicallee 11y ago>or even "result" - because they convey so little meaning. How can anyone write a sin function like: result = sin(degrees*PI/180); return result when you OBVIOUSLY should be writing: sin_of_degrees_times_pi_divided_by_180 = sin(number_of_degrees_in_degrees*PI/180) return sin_of_degrees_times_pi_divided_by_180 One is unreadable garbage. (Result? WTF does that mean)? While the other is clean, maintainabe code. Anyone can tell at a glance what sin_of_degrees_times_pi_divided_by_180 holds. But result? In a function that calculates sin values? You might as well label your variables OX00001, OX000002 and so forth.
- jessaustin 11y agoThis is a weak example, because obviously it should be a one-liner: return sin(degrees*PI/180);
- catmanjan 11y agoTrue, and really degrees*PI/180 should be in it's own function "toRadians(degrees)".
- logicallee 11y agoI disagree, and think people should use lots and lots of temporary variables. variables like "retval", "data" or even "result", as quoted in GP. I have zero qualms about writing two lines bool passes = /*stuff goes here*/ if (passes) { } whereas it is trivial that you could fold this into one line. the ONLY thing you're adding is the term "passes". Which most certainly is meaningful to the human reading it. Compare: if (!(i%2)) with bool isodd = i%2; if (!isodd) which test do you think you'll accidentally flip the parity of? Likewise I find "retval", "data", etc, to be perfectly fine variable placeholders while you treat the thing for a few lines.
- jessaustin 11y agoIn particular situations "wasted" vars like this are helpful. (although I agree with sibling comment that informatively-named functions are usually a better way to communicate meaning than extra vars) If this is your general rule, however, your code will be like twice as long as it needs to be.
- logicallee 11y agoi'll take twice as long over twice as wide any day of the week :)
- daemin 11y agoNot really, sometimes you want to inspect the value that is to be returned before it is. So it makes sense to use the multi-line statements above, as otherwise everyone will change it each time. It will also allow you to put a breakpoint on the return statement and have it print the value out or for it to be used in other debugging. This is especially useful when the function call is used within a more complicated formula.