When should you encourage code duplication?

time to read 2 min | 210 words

Another story from class, today I was reviewing some of the student's code, and we run into this one:

image

The student is one of the keenest persons I have ever seen on removing code duplication (painful experience, I understand). But in this case, I had stopped and started using clipboard inheritence to duplicate the code. No, I have no gone insane.

image

The issue is in this case, removing code duplication has cause a violation of the Single Responsibility Principal. That is a bad idea. These two views had distinct responsabilities, they just happened to have identical implementations at the moment.

That doesn't means that you are free to duplicate code in the name of SRP, in this case, I would usually have something like this:

image

Where the implementation would live, and it would be forwarded from the views implementations.

In this case, it wasn't necessary, because shortly thereafter, I had to modify the IReturnBookView significantly :-)