Skip to content

Inline-Immediately-Returned-Variable

Sourcery rule id: inline-immediately-returned-variable

Description

Inline variable that is immediately returned

Before

const var = 1;
return var;

After

return 1;

Explanation

Something that we often see in people's code is assigning to a result variable and then immediately returning it.

Returning the result directly shortens the code and removes an unnecessary variable, reducing the mental load of reading the function.

Where intermediate variables can be useful is if they then get used as a parameter or a condition, and the name can act like a comment on what the variable represents. In the case where you're returning it from a function, the function name is there to tell you what the result is, so the variable name is unnecessary.