Skip to content

Merge Assignment and Augmented Assignment

Sourcery refactoring id: merge-assign-and-aug-assign

Description:

Replaces an assignment and an augmented assignment with a single assignment.

Before:

other_value = 33
number = 42
number += other_value

After:

other_value = 33
number = 42 + other_value

Explanation:

When we mutate a variable multiple times without reading or writing its value in between, it's more readable and more efficient to change its value only once. This way, it's clearer which values this variable can have at various points.

This refactoring works with all 4 augmented assignment operators:

  • +=
  • -=
  • *=
  • /=