473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Coupling of objects

It has be often mentioned that the coupling among objects should be avoided.

What're the major cases that objects are closed coupled?

How to avoid such cases?

Many thanks!
Jul 22 '05 #1
4 1478
dover wrote:
It has be often mentioned that the coupling among objects should be avoided.

Define coupling. All authors seem to "know it when they see it".
What're the major cases that objects are closed coupled?
I (authoritatively) define coupling as "A must change only because B
changed."

Put another way, I associate my definition with a transition. Staring at a
static instance of A and B won't tell if a given change, C, will reveal
coupling. Attempting the change will reveal it.

The other kind of association is "coherency". That essentially means "good
coupling". I define it as "A and B share legitimate reasons to change."
How to avoid such cases?


Write lots of unit tests. Write them before writing the tested code, and
make them fail before making the tested code pass. Then refactor, testing
every few edits, until your design contains no duplicated definitions of
behavior.

Those forces overwhelmingly crush out all possible coupling from your
design. Under such a system you honestly needn't fret about coupling; it
becomes a non-issue, like bugs.

For example:

int main()
{

Source aSource("a b\nc, d");

string
token = aSource.pullNextToken(); assert("a" == token);
token = aSource.pullNextToken(); assert("b" == token);
token = aSource.pullNextToken(); assert("c" == token);
token = aSource.pullNextToken(); assert("d" == token);
token = aSource.pullNextToken(); assert("" == token);
// EOT!
}

That's just one puny test, lacking a test rig such as CppUnit. But it
already proves a very important aspect of the class Source's coupling.

You can construct a Source object using only main() and a string argument.
You are not required to construct or deploy or call or instantiate or
register or jump-thru-hoops any other objects, just to get a useful Source
going.

The complete application that code grew into appears here:

http://www.c2.com/cgi/wiki?MsWindowsResourceLint

I would be interested to learn if anyone thought any of my classes in there
were coupled. Or incoherent.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #2
"dover" <do*****@close.com> wrote in message news:<P4*******************@bgtnsc05-news.ops.worldnet.att.net>...
It has be often mentioned that the coupling among objects should be avoided.

What're the major cases that objects are closed coupled?
1. inheritance
e.g., class A : public class B {};

2. friendship
e.g., class B; class A { friend class B; };

3. substantive use of class B in the interface of class A
e.g. A::foo(B b);

4. substantive use of class B in the implementation of class A
e.g., A::foo() { B b; }
How to avoid such cases?
See Lakos.
Many thanks!


/david
Jul 22 '05 #3
"Phlip" <ph*******@yahoo.com> wrote in message news:<iO*****************@newssvr17.news.prodigy.c om>...

[snip]
I (authoritatively) define coupling as "A must change only because B
changed."
s/change/re-compile/

[snip]
How to avoid such cases?


Write lots of unit tests. Write them before writing the tested code, and
make them fail before making the tested code pass. Then refactor, testing
every few edits, until your design contains no duplicated definitions of
behavior.


To me, this approach encourages you to design to the unit test suite
rather than to the problem domain. I have never had a good experience
writing tests before I write the interface, but I guess it's good to
experiment.
Those forces overwhelmingly crush out all possible coupling from your
design. Under such a system you honestly needn't fret about coupling; it
becomes a non-issue, like bugs.

For example:

int main()
{
Source aSource("a b\nc, d");
string token;
token = aSource.pullNextToken();
assert("a" == token);


FWIW, I find it useful to define and use ASSERT (or redefine assert())
so that it doesn't stop the program execution at each failed
assertion. Otherwise, you may have to run the test driver several
times to solve one class of error.

/david
Jul 22 '05 #4
David Rubin wrote:
Phlip wrote:

[snip]
I (authoritatively) define coupling as "A must change only because B
changed."
s/change/re-compile/


That is an artifact of how C++'s compilation model weds most kinds of
coupling to the recompile system. Put another way, you can build very large
systems in C++ without excess recompiles if you take care to follow good OO
design principles such as "program to the interface, not to the
implementation".

But you can still couple A and B unfairly, but A _doesn't_ recompile when B
changes. It just breaks.
[snip]
How to avoid such cases?
Write lots of unit tests. Write them before writing the tested code, and
make them fail before making the tested code pass. Then refactor, testing every few edits, until your design contains no duplicated definitions of
behavior.


To me, this approach encourages you to design to the unit test suite
rather than to the problem domain. I have never had a good experience
writing tests before I write the interface, but I guess it's good to
experiment.


One designs them both, together, in tiny increments. One doesn't write many
test cases up front.
Those forces overwhelmingly crush out all possible coupling from your
design. Under such a system you honestly needn't fret about coupling; it
becomes a non-issue, like bugs.

For example:

int main()
{
Source aSource("a b\nc, d");
string token;
token = aSource.pullNextToken();
assert("a" == token);


FWIW, I find it useful to define and use ASSERT (or redefine assert())
so that it doesn't stop the program execution at each failed
assertion.


Yes! ASSERT() should invoke a breakpoint expanded in the calling function,
not deep in the C++ Standard Library.
Otherwise, you may have to run the test driver several
times to solve one class of error.


No! If you run the tests every 1~10 edits, and predict the results of each
run, then any unexpected failure is cause to simply Undo the most recent
edits.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: crichmon | last post by:
What's a good definition for coupling? I see that term used alot. crichmon
4
by: Alex Stevens | last post by:
Well, It's 4:10 on Friday Afternoon and I am now in a state of turmoil....... I've just spoken with a trainer (who's opinion would be in far higher regard than mine), and he's just told me that...
19
by: Charles Law | last post by:
Take a solution with a project hierarchy along the lines of an n-tier system, so that we have a data layer, business layer and presentation layer. The presentation layer is coupled to the business...
1
by: Sush | last post by:
Can anybody explain me what is control and data coupling with respect to C code? Also, how to do control and data coupling analysis?
3
by: cgineste | last post by:
/* Hello everyone, I am trying to impose some strong coupling between different objects. This, to a large extent, directly challenge the principle of encapsulation in C++. Note that the...
8
by: Charles Law | last post by:
This is a sort of pattern question, but relating to how components are coupled in a three-tier system. I have a presentation layer, business layer and data access layer, the first is the EXE,...
2
by: Ronald S. Cook | last post by:
In trying to figure out how to make function MedicineClass.ApplyMedicinePriceChanges perform better, I ran Code Analysis and got the following: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.