Inline-Immediately-Returned-Variable
Documentation Index
Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt
Use this file to discover all available pages before exploring further.
Sourcery rule id: inline-immediately-returned-variable
Section titled “Sourcery rule id: inline-immediately-returned-variable”Description
Section titled “Description”Inline variable that is immediately returned
Before
Section titled “Before”const var = 1;return var;return 1;Explanation
Section titled “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.