Skip to content

Merge Assignment and Augmented Assignment

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


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


other_value = 33
number = 42
number += other_value


other_value = 33
number = 42 + other_value


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:

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