Re:A Guide to Naming Variables
by philwelch on Friday May 13, 2016 @12:45PM
(#11691452)
Yeah, the problem with outlawing "retval" in particular is that there's no good variable name in this situation:
What the hell should I call retval? If I call it sum, that's redundant with the method name. Also, while it might work in Java, it would be more dangerous in a language like Ruby where the equivalent code:
would, due to lexical scoping, make it impossible to call the method recursively.
[ Parent
| on HN ]
Re:A Guide to Naming Variables
by munificent on Friday May 13, 2016 @01:17PM
(#11691700)
Call it "result":
- "val" in "retval" carries no meaning. Everything stored in a variable is a value.
- "ret" is an annoying abbreviation for "return" that makes it harder read.
- "return" focuses on an implementation detail—that the function produces its value using the language's "return" statement. It doesn't matter how the result is emitted, just that it is.
If a function is simple enough that the variable storing the result doesn't need a more specific name, "result" is a good default. You could call it "sum" here, but that shadows the function itself—a common problem for result variables—and doesn't really tell us anything we don't already know. We know we're inside a "sum" function.
"result" was also included in the list. I honestly don't see anything wrong with either.
I usually go for "result", but that was also on OP's list. I wish it was a convention to use "r" for retval variables.
Re:A Guide to Naming Variables
by vanderZwan on Saturday May 14, 2016 @03:53AM
(#11695012)
Problem with that (for me) is that I use that for radius all the time, as in r * sin( a * t )