Subj : Re: What the bleep is Coupling? (was: Polymorphism sucks) To : comp.programming,comp.object From : Chris Sonnack Date : Fri Jul 01 2005 04:27 pm topmind writes: >>>>> Copy-and-paste... >>>> >>>> It may appear to reduce coupling, but [...] it creates an >>>> invisible (to the source) web of coupling that needs to >>>> be documented and maintained by the developer. >>> >>> Yes, but that is a nebulous form of "coupling". >> >> Which is the worst kind! Much harder to maintain. > > I meant in the context of a metric for "good code" or "good > design". So did I. C'n'P is a terrible form of coding IF ITS PURPOSE IS THE DUPLICATION OF LINKED CODE. In other words, if you know you have to do the same thing in two places, and you do it by duplicating code, that's a mistake. A big mistake. >>> Robert Martin implied that "coupling" was sure-shot metric >>> that by itself would guide decisions. >> >> No, that was your willful misinterpretation of what he said. > > "Willful"? That is a rather strong accusation that frankly > offends me. I swear to all known dieties there was no willful > misinterpretation on my part. Then I withdraw the comment and apologize. It seemed willful to me, because it appears to me you're more interested in preaching the evils of trees and the wonder of tables at the expense of reality. In the world in which most of us live, there are no evil tools, and no silver bullets. Tools serve a purpose, which is to help us get the job done. In *my* world, and I can well imagine in the worlds of most other programmers, hierarchical organization has a proven track record. To quote a common response to another "evil tools" foolishness, you'd have to pry them out of my cold dead hands. >> There are no sure-fire metrics, the Universal Answer to any >> computing question is always, "It Depends." > > Robert implied it was a "universal answer", not me. I don't know Robert, but I have read him for years. I VERY much doubt he thinks or said that. >> Coupling is usually to be avoided as much as possible. > > I thought Robert agreed that some coupling is good and some is bad. > Coupling is what allows one to treat things as a unit. Putting things > into a suitcase is coupling, encapsulation is coupling, modules are > coupling, files are coupling, database records are coupling, arrays are > coupling, etc. When programmers talk about "coupling" they usually mean coupling between *separate* units. Encapsulation, modules, files, records and arrays are not separate units, and are not examples of coupling. Coupling is: I have a class "Foo" and a class "Bar" (separate units), and Foo needs to know stuff about Bar. Therefore, Foo is coupled to Bar. This can be disasterous if someone else controls Bar's design. Coupling is unavoidable, the trick is to minimize or control it. In most languages, if you have a function Snork(int x, int y), all calls to Snork() are coupled in that they need to pass a pair of appropriate ints in the right order. (Some languages help decouple by providing the ability to pass arguments by name. Thus, you can at least avoid the ordering coupling.) Functions coupling is *controlled* by publishing to the compiler information about Snork(). That way, if a caller changes, or if Snork() changes, the compiler screams. > Well, here are some open questions on "coupling" that seem to > generate differing answers: > > 1. Is all coupling bad? Answer depends on how you define coupling. If you use your highly inclusive definition listed above, then no, obviously not. If you use what most programmers mean by coupling then the answer becomes, "usually, but it depends". Remember, nothing is all bad, not even GOTOs. > 2. If not #1, then how does one tell good coupling from bad coupling? The reason coupling is "bad" is because it introduces dependancies that must be managed. The higher the management cost, the worse the coupling. > 3. Can all coupling be objectively measured, such as counting > cross-references in programming code, or is it a kind of rough > conceptual idea? (If not, "all", then what are the "kinds"?) It's a conceptual idea, but cross-references can give you an index to certain forms of coupling. You need to do more than just list them to determine if they are under control, unavoidable or worth refactoring. -- |_ CJSonnack _____________| How's my programming? | |_ http://www.Sonnack.com/ ___________________| Call: 1-800-DEV-NULL | |_____________________________________________|_______________________| .