7

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?

Community
  • 1
  • 1
dance2die
  • 35,807
  • 39
  • 131
  • 194
  • What? How does the button change when Save or Close changes? What class do you think implements Save and Close? How does a change to those functions have any impact on the button? – S.Lott Apr 02 '09 at 14:09
  • What I meant by "change" was that, when the underlying implementation of either "save" or "close" changes; Not when UI of the button have to change. – dance2die Apr 02 '09 at 14:19
  • Bad example. The function with the code of the button has a _sequence_ of two actions: "Save()" and "Close()". This doesn't violate anything. Any function can be a sequence, an iteration, or a choice. – Daniel Daranas Apr 02 '09 at 14:27
  • Implementation of that button would be if(Save()) Close();. What needs to change if either of them changes? – Damien_The_Unbeliever Apr 02 '09 at 14:27
  • @Daniel & Damien: What if outlook had a "SaveAndClose()" method that does both saving and closing; What if there was no functions like "Save()" or "Close()"? – dance2die Apr 02 '09 at 14:54
  • @Sung then the code would have a bad, or at least a bit awkward, design. "Save" and "Close" are two distinct abstractions and the designers should be able to represent them with suitable, individual functions. – Daniel Daranas Apr 02 '09 at 15:19
  • @Daniel: It was just an overly simplified example of what the code can look like. – dance2die Apr 02 '09 at 16:53
  • @Sung I really don't get your point. – Daniel Daranas Apr 03 '09 at 10:19
  • @Daniel My supposition for the question was that Outlook might have a function like "SaveAndClose()", which does two things without "two distinct abstractions". – dance2die Apr 03 '09 at 12:36
  • Would this be an example of an SRP violation. I needed to add a new product and return the new product list. I combined my add and getlist methods into one function so I could make only one call to the server. Was I correct? – Ken Jan 11 '19 at 16:48

1 Answers1

23

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.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 1
    Actually, it applies to the UI, also. But this example shows a UI thing that is implemented by some Control and/or Model things under the hood. The question conflates UI changes with Model/Control changes; which doesn't make much sense -- each was built following SRP. – S.Lott Apr 02 '09 at 14:11
  • 1
    Yes, I agree with you. After re-reading my first sentence I realized that I am, in fact, incorrect. I think I will leave it there as your comment won't make much sense if I remove it. I appreciate your incisive comment and I don't want it to appear out of context. :) – Andrew Hare Apr 02 '09 at 14:14
  • @Andrew: By your comment, you mean, SRP should never(yeah bad choice of word) be violated and if it were to be violated, code structure need to change instead? – dance2die Apr 02 '09 at 14:20
  • I think "never" is just the right word. Often times the usage of "never" is very hyperbolic but in this case it isn't. *Any* violation of SRP can be refactored to remove the violation with minimal effort. I guess what I am trying to say is "Yes". :) – Andrew Hare Apr 02 '09 at 14:28
  • @Marked as Answer: Thanks Andrew. your explanation was clear in both answer and comments. – dance2die Apr 02 '09 at 14:31
  • +1: SRP is **never** violated -- that's precisely why a GUI is split into Model, View and Control. – S.Lott Apr 02 '09 at 17:36
  • 1
    I am guilty as charged for violating SRP left & right. Time to refactor! – dance2die Apr 02 '09 at 21:13