I recently attended a C++ talk on SOLID principles and the presenter provided std::string
as an example that violates SRP. I wasn't quite able to comprehend why that was the case. From what I understand, std::string
isn't a paragon for STL data structures, unlike std::vector
etc., since it duplicates some free functions from the <algorithm>
library like find
, replace
(with no customized specialization [Please correct me]), etc. but, in my opinion, it is still a cohesive bunch of functions to help manipulate strings. One possible argument I could think against it demonstrating SRP was that std::string
allocates as well as manipulates strings, which could lead to issues mixing it with other instantiations of std::basic_string
but I'm not sure if that's an apt defect in the context of SRP. Can someone please help elaborate on why it violates SRP?
1 Answer
SRP isn't a well-defined thing. Bob Martin, the guy who came up with it is on his 5th or so attempt at defining it and it's still mostly just confusing with very little concrete points or even code.
So it all depends on who you ask. To put it differently: both sides could be argued. Not just for string
, but for mostly everything.
-
5That's why you should take everything Bob Martin says as suggestions, not edicts. Commented Feb 28, 2022 at 21:30
-
6What's terrible about Bob Martin's SRP is that the David Parnas article that inspired SRP is much clearer. Page 24: "We propose instead that one begins with a list of difficult design decisions or design decisions which are likely to change. Each module is then designed to hide such a decision from the others" kilthub.cmu.edu/articles/journal_contribution/… Commented Feb 28, 2022 at 22:15
malloc
andstrchr
, after all? What's wrong withchar*
? Different languages, different perspectives.