Skip to content

Use Assigned Variable

Sourcery refactoring id: use-assigned-variable

Description:

Uses a variable that was previously defined in the function instead of repeating what was defined in the variable

Before:

wardrobe = {"blue_hat": 1, "red_hat": 3}
for item in wardrobe:
    count = wardrobe[item]
    add_to_total(wardrobe[item])

After:

wardrobe = {"blue_hat": 1, "red_hat": 3}
for item in wardrobe:
    count = wardrobe[item]
    add_to_total(count)

Explanation:

Where possible, it is preferable to re-use local variables that have been assigned to. These variables will often have a more descriptive name that can aid in code comprehension, and additionally re-use can reduce duplication.