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
The class has too much responsibility.
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')Refactoring
- Extract Superclass
- Extract Subclass
- Extract Class
- Extract Function
- Move Function
Sources
- ORIGIN1999 · ISBN 978-0201485677