2

The mantra "Keep high cohesion and low coupling" (or some variant) is frequently tossed around. However, I find that it frequently conflicts with "Don't repeat yourself."

For example, I think we can all agree that it's a bad thing to re-implement std::string or use C-strings, but doesn't including std::string create another dependency and therefore increase coupling?

For another example, take std::stringstream. It inherits from iostream, which inherits from istream and ostream, which inherit from ios, which inherits from ios_base. Over all these derivations, it inherits a lot of functionality - enough to make re-implementing by hand a very bad idea. It also pulls the <ios> and <istream> headers even though only <sstream> was included, thereby increasing coupling.

How can one keep coupling low without reinventing the wheel for each module?

EDIT: If the two concepts cannot coexist, which one should be favored?

Maxpm
  • 24,113
  • 33
  • 111
  • 170
  • A specific practical example will probably lead to better answers, and less theorizing. – tenfour Jul 03 '11 at 22:00
  • Could someone explain the close vote? The question is very clear. – Maxpm Jul 03 '11 at 22:05
  • it fits the __not constructive__ description: "This question is not a good fit to our Q&A format. We expect answers to generally involve facts, references, or specific expertise; this question will likely solicit opinion, debate, arguments, polling, or extended discussion." – Pablo Fernandez Jul 03 '11 at 22:09
  • @Pablo I don't see how it will solicit "opinion, debate, arguments, polling, or extended discussion." While there may be multiple good answers, you can definitely draw the line between them and bad answers. It *is* answerable, and it *is* an actual problem. – Maxpm Jul 03 '11 at 22:13
  • I disagree. Two concepts being contradictory in part is not __an actual problem__ IMHO. Anyway, this is kind of a democracy and it still takes a few more votes to close this down. – Pablo Fernandez Jul 03 '11 at 22:16
  • @Pablo It's certainly a very confusing problem for me, and obviously other people have found away around it. I don't see a better way to get to the bottom of this. I could ask in chat, but what would be the point? This is a Q/A site, after all. – Maxpm Jul 03 '11 at 22:19

1 Answers1

3

Please go through http://www.artima.com/intv/dry.html on DRY,
particularly "Most people take DRY to mean you shouldn't duplicate code. That's not its intention. The idea behind DRY is far grander than that".

Adding to this, in the example you discussed , std::string and your system are not tightly coupled as you are not relying/using any internal info of std::string. Any (internal) changes to std::string will not effect your system.

Tatvamasi
  • 2,537
  • 19
  • 14
  • Good link. I found the section on orthogonality especially enlightening, because it clarified that *unrelated* things should not know about each other. In my example of ``, it makes sense for it to know about the other headers. If it required something ridiculous, say, ``, *then* it would be bad coupling. – Maxpm Jul 04 '11 at 00:59
  • @Maxpm are you talking if std::string is not properly designed, with loose coupling ? or usage of std::string ? If it is usage, I am sure there is no need for you to worry about the implementation details of std::string. std::string might depend on lot of other functionalities to implement all its API's. (it might even depend Rocket Launching functionalities ... with proper reason). – Tatvamasi Jul 04 '11 at 01:46
  • I'm talking about `std::stringstream`, not `std::string`. (I changed the original question to have a better example.) Anyway, I'm not suggesting that it's poorly designed, but rather using it as a real-world case of what I'm talking about. – Maxpm Jul 04 '11 at 02:47