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 in开发者_开发百科creasing 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?
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.
精彩评论