Software Design/Avoid a variable called "result"

Checklist questions:
 * The variable returned from a function has a more meaningful name than just  (,  ,  , etc.)?
 * Is it possible to return early from a function instead of creating a "result" variable?

Why
Calling a variable  is equivalent to not calling a variable at all because "result" doesn't bear any meaning. The benefits of semantic variable naming are forgone.

When reading through the function down to the final statement like, people have to remember what type of value the function should return instead of just seeing that. The function's name may not embed the full information about the returned type: for example, a function  doesn't encode in its name whether the returned discount is a percent or a value between 0 and 1 because the name is already very long. In this case, a reader should consult the function's documentation every time they want to recall what type of value the function should return. If the documentation doesn't specify that or is absent, this semantic information is completely lost in the code. Thus, a return variable called  contributes to cognitive load. The code is less obvious and makes readers navigate more.

When the result variable has a semantic name it may also reduce the probability of making a mistake in the function, for example, by assigning a value in the wrong units to the variable. Consider this function: It would be easier to spot the bug in this function if the variable was called  and the first line was

Why not
When a function is short, or when the result type doesn't have units, semantic naming of the result variable makes little or no difference. On the other hand, in the presence of a variable called, it's harder to make a mistake by accidentally returning another variable from the function, especially if calling variables to be returned from functions   is a convention which is followed consistently in the codebase. See practice Call function's return variable "result".

Combining the approaches
A compromise solution is to call functions' result variables, for example,   in   function above.

Removing a result variable altogether
Sometimes, a  variable can be eliminated by returning early from a function. Consider this function: It doesn't make much difference whether the result variable is called  or   here. However, the variable can be eliminated altogether to make the function shorter and simpler: A  variable is often a sign that such simplication is possible.

Checkstyle (Java)
LocalVariableName and LocalFinalVariableName modules:

Related practices

 * Name a variable semantically