Divergent Change — Code Smells Catalog Skip to content

Divergent Change

Change Preventers Responsibility Code SmellDesign Smell Within Class

A class that changes for database reasons on Monday, calculation reasons on Wednesday, and display reasons on Friday. Same file, different reasons, and the merge conflicts pile up every sprint.

2 min read 1 source

Overview

If adding a simple feature makes the developer change many seemingly unrelated methods inside a class, that indicates the Divergent Change code smell. Simply put, the class has irrelevant methods in it [1]. As an example, suppose that someone needs to modify class A due to a change in the database but then has to modify the same class A due to a change in the calculation formula [2].

The difference between Divergent Change and Shotgun Surgery is that the Divergent Change addresses the issue within a class, while the Shotgun Surgery between classes.

Causation

Over time, a class tries to do more and more things and has many responsibilities. The fact that the class already has two or more different types of decisions implemented (for example, finding an object and doing something with object [3]) was overlooked and left unrefactored.

Problems

Single Responsibility Principle Violation

The class has too much responsibility.

📋
Duplication

Example

1class ReportModifier:
2    def get_report(self, report_name):
3        ...
4        return report
5
6    def modify_report(self, report, new_entry):
7        ...
8        return modified_report
9
10    def run(self, report_name, new_entry):
11        report = self.get_report(report_name)
12        return self.modify_report(report, new_entry)
13
14
15report_modifier = ReportModifier(...)
16modified_report = report_modifier.run('raport.csv', 'Parsed')
PYTHON

Refactoring

  • Extract Superclass
  • Extract Subclass
  • Extract Class
  • Extract Function
  • Move Function

Sources

Browse All 56 Smells