Keep Coupling Loose

I love reading the book Code Complete, by Steve McConnell. The following comes from it.

Coupling describes how tightly a class or routine is related to other classes or routines. The goal is to create classes and routines with small, direct, visible, and flexible relations to other classes and routines, which is knowsn as ‘loose coupling’.

Good coupling between modules is loose enough that one module can easily be used by other modules.

Try to create modules that depend little on other modules. Make them detached, as business associates are, rather than attached, as Siamese twins are.

Coupling Criteria
Here a several criteria to use in evaluating coupling between modules:

Size: Sze refers to the number of connections between modules. WIth coupling, small is beautiful because it’s less work to connect other modules to a module that has a smaller interface. A routine that takes one parameter is more loosely coules to modules that call it than a routine that takes six parameters.

Visibility: it refers to the prominence of the connection between two modules.

Passing data in a parameter list is making an obvious connection and is therefore good. Modifying global data so that another module can use that data is a sneaky connection and is therefore bad.

Flexibility: It refers to how easily you can change the connections between modules. Ideally, you want osmething more the USB connector on your computer than like bare wire and a soldering gun.
The more easily other modules can call a module, the more loosely coupled it is, and that’s good because it’s more flexible and maintainable. In creating a system structure, break up the program along the lines of minimal interconnectedness.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: