Simply put, because adding a Type3Request
object in your code forces you to edit existing code. Every time you change existing code, you should test all the related modules in your application to ensure that no regression bugs appear in your code. In javascript or other loosely-typed languages, it is very easy to overwrite global data from an if branch. In most strongly-typed languages you can generally avoid this issue, but there is still a possibility of overwriting local data. This means that every time you change this executeRequests
method, you have to run all possible requests in your application to ensure that you have not introduced a new issue. I want to make clear that I said all requests, not all requests types. This is potentially thousands of tests to run even if you have only a few request types.
On the other side, keeping this code inside different classes as a polymorphic method ensures that it cannot changes data from the executeRequests method as well as its owner class. This enables you to test only calls related to the type that you are changing instead with the same confidence level. In other words, it reduces the amount of bugs in your code.
By using polymorphism, all the "specific code" that needs to be executed for this request will not only be encapsulated into the Type3Request
object, but will actually come with this object. There is no other code file to change in order to add a new request type.
This means that you can provide new request types dynamically as plugins, simply by adding a dll in a folder and without recompiling the existing code. If you do no want to go that far, it still gives you the ability to better separate your workload between all team members by avoiding merges to that executeRequests
method.
It also makes it simpler to test code by keeping tightly coupled business logic together. Instead of having to get your request to go through the entire execution process just to test this type specific code, you can just call the method directly from the request object and assert the results.
All of this ensures that if an architecture change happens in your project, you know that there is only one place to look for request related code: the related request type class. If you let this code leak into other classes, you might miss some instances of this code when estimating the complexity of changes or even worse, leave it behind after a large refactor which might trigger bugs.