Result considered harmless #1
Replies: 2 comments 2 replies
-
|
It makes sense for situation like in your example. Here are some situations where it could decrease readability:
We can argue that within a small function body,
|
Beta Was this translation helpful? Give feedback.
-
|
In the context of your short power function, result is very readable. Technically, you could have used power instead of result, but that would shadow the outer scope and hopefully spark genuine outrage in your reviewers. Using the clean code convention of short (potentially even one character) variable names for very short scopes, you could use pow or p. I rather like p, because it would make the function even tinier, but I don't object to result here. Would using p make it less readable? I expect the answer depends entirely on the reader. It would be amazing if we could squeeze every solution into a tidy Clean Code box, but I haven't figured out how to do that yet. I do love how these little dilemmas encourage discussion. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
In a couple of recent code reviews, reviewers have flagged my choice of "result" as a variable name as a violation of clean code principles. As a clean code advocate and practitioner, I understand the reasoning behind this critique. We want names that are clear and unambiguous, and a name like "result" is certainly vague. But I want to make the case for using the name "result" for a specific purpose -- as the return value of a function.
Consider a Python
powerfunction that computesnraised to theepower:The name of the function already conveys what it does, and what its return value is expected to be. What additional information could be conveyed by naming the variable something other than "result?" Maybe "power_value" or "power_result?" I claim that such names actually introduce ambiguity. Since the name of the "power" function already indicates that it returns a power, what would "power_value" or "power_result" mean? Something slightly different?
In contrast, if we accept the name "result" to represent the return value of a function, then there is no ambiguity.
In fact, the Nim language treats "result" as a special name that allows an implied return value:
Anyway, this situation should be rare. Ideally, we would try to avoid reliance on mutable variables and follow functional paradigms wherever possible:
But in the rare cases where we need a mutable variable to calculate the return value, I claim that the name "result" is not only acceptable but preferable.
Other thoughts?
Beta Was this translation helpful? Give feedback.
All reactions