Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do you violate SRP (Single Reponsibility Principle)?

SRP(PDF version; HTML version) states that

There should never be more than one reason for a class to change

When you take a look at Outlook, Calendar Event window, it has "Save and Close" button.

alt text

So when the functionalities of either or both Save or Close changes, that button should change. It obviously violates SRP.

This functionality both saves time and convinient since that is what most of users expect to do when they save an appoint on a calendar.

But now, my question is, when else do you violate SRP other than when the feature need to be usuable in Outlook?

like image 716
dance2die Avatar asked Apr 02 '09 14:04

dance2die


1 Answers

SRP does not apply to UIs. Keep in mind that even though the button is called "Save and Close" there can undoubtedly be two separate methods or types to handle the saving and closing functionality. The button simply ties those two separate pieces of functionality together.

If you feel the need to violate SRP then you need to re-evaluate your approach. Any SRP violation can be refactored into a new method or type that exposes the composite functionality by means of composition of the two pieces.

like image 116
Andrew Hare Avatar answered Oct 21 '22 03:10

Andrew Hare