There is an abstract class called AbstractAgent and currently there are 27 classes who are extending this class.
I have started developing some agents and all my 5 agents are extending AbstractAgent class.
Now I observed that there is a getFilePath() which I am copy-pasting in all my 5 agents. Then I realized that out of existing 27 classes also there were many classes who had this method. I thought it would be a good idea to put this method in the base class AbstractAgent and let everybody use this method. But I dont want all existing classes to change their code I have changed the method name so that going forward anyone can use it.
The person who did my code review suggested me not to touch AbstractAgent class because it is already being used by existing clients and put this method is some utility class.
I am not convinced by his argument. Anybody want to pitch in their thoughts.
If the
getFilePath()method is exactly the same for all ofAbstractAgent‘s subclasses, then by all means go ahead and pull it up to the abstract class (use your IDE’s refactoring tools), delete it from the subclasses then run unit tests (you do have unit tests, right?) and check that everything is working fine.If there are differences between some implementations of
getFilePath(), but most of them are identical, it’s still a good idea to pull the most common implementation up to the abstract class, delete it from the subclasses that use it and override the method in the subclasses in those cases where the implementation differs.Now, if there’s just too much variability between the different implementations of the method, leave the code base untouched.
Defining a static method in a utility class (say,
FileUtil) makes sense only if the method is not directly related to the functionality of the Agent classes, or if the method could be of use in some other part of the object hierarchy, not directly related to agents.