Last Revision — April 19, 2022
2 Min Read
---
Change Preventers
Responsibility
Within a Class
- Shotgun Surgery (family)
Martin Fowler in book (1999): "Refactoring: Improving the Design of Existing Code"
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.
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.
The class has too much responsibility.
class ReportModifier:
def get_report(self, report_name):
...
return report
def modify_report(self, report, new_entry):
...
return modified_report
def run(self, report_name, new_entry):
report = self.get_report(report_name)
return self.modify_report(report, new_entry)
report_modifier = ReportModifier(...)
modified_report = report_modifier.run('raport.csv', 'Parsed')
class ReportReader:
def get_report(self, report_name):
...
return report
class ReportModifier:
def modify_report(self, report, new_entry):
...
return modified_report
report_reader = ReportReader(...)
report = report_reader.get_report('raport.csv')
report_modifier = ReportModifier(...)
modified_report = report_modifier.modify_report(report, 'Parsed')