473,799 Members | 2,927 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1497
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 (authoritativel y) 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.pullNex tToken(); assert("a" == token);
token = aSource.pullNex tToken(); assert("b" == token);
token = aSource.pullNex tToken(); assert("c" == token);
token = aSource.pullNex tToken(); assert("d" == token);
token = aSource.pullNex tToken(); 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******* ************@bg tnsc05-news.ops.worldn et.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*******@yaho o.com> wrote in message news:<iO******* **********@news svr17.news.prod igy.com>...

[snip]
I (authoritativel y) 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.pullNex tToken();
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 (authoritativel y) 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.pullNex tToken();
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
2725
by: crichmon | last post by:
What's a good definition for coupling? I see that term used alot. crichmon
4
3324
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 the way I expected to write an tiered application is tight-coupled and should be written loosely coupled.......... I am kind of new to multi-tier application programming, although I have good experience of classes, and objects.
19
2900
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 layer, and the business layer is coupled to the data layer. So far so good. Suppose the data layer raises an event, and it passes Me (the sender) as an object, and e (MyEventArgs, a descendent of EventArgs) to the layer above (the business...
1
3254
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
1945
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 form of dependencies that I am trying to devise is between instances of classes, not between classes themselves. Hence, I have already looked into issues of multiple inheritance, friends and
8
1758
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, whilst the other two are implemented as DLLs. The EXE also references a DLL that contains various user controls, one of which is based on a grid control. The coupling is currently like this:
2
11927
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: ****************************************************************************************** Warning 1677 CA1506 : Microsoft.Maintainability : 'MedicineClass.ApplyMedicinePriceChanges(ProgramMedicinePrice, String, Guid, Decimal)' is coupled with 34 different types from 9...
0
10482
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10225
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.