How to measure coupled code

opinion
Aug 6, 20254 mins

There is in fact a way to describe the type and severity of coupling that can occur in your code.ย Itโ€™s called connascence.

shutterstock 1152151433 web of ropes and a big knot against a blue sky
Credit: ChocoPie / Shutterstock

Back in March, I wrote about dependency injection. I explained why you should be injecting your dependencies instead of creating them on the fly. I argued that dependency injection was a great technique for decoupling your code.ย It is!

We all seem to realize that coupled code is bad and that we should work to keep our code decoupled.ย And we all have had those terrible experiences where you change some code in a module that does order processing, and suddenly the credit report is all screwed up.

But what the heck does it mean, exactly, to have โ€œcoupled codeโ€?ย How do we tell?ย How do we know if our code is lightly coupled, medium coupled, or heavily coupled?ย 

We all know that we should try to reduce coupling and make different parts of code less dependent on each other so that changes in one part donโ€™t affect functionality in another.ย  Heavy coupling leads to fragile systems and hidden landmines that go off when we least expect them.ย 

Connascence? What?

Well, there is a word for this notionโ€”connascence.ย The term was formalized and popularized by Meilir Page-Jones in his 1995 book What Every Programmer Should Know About Object-Oriented Design.ย In that book, Page-Jones took the notion of coupling a step further by coming up with a way to measure it.ย 

Connascence is when two parts of code are so connected that if you change one, you have to change the other to keep things working. A simple example of connascence is adding a parameter to an existing method.ย If you change the signature of a method, you have to change the code everywhere you call that method.ย 

Itโ€™s a basic notion, one that you probably understand without formally considering it.ย However, the closer you look at connascence, the more interesting (and complex) it gets. Connascence becomes a measure of the type and severity of coupling that can occur in your code.ย 

Another way to think of it is that coupling is graded on a scale. There are different degrees and severities of coupling, and connascence is the measure of how extensively and how strongly code is coupled together. Code has to be coupled to work, but you want to couple your code with a low level of connascence.ย 

How about an example? Consider the following code.


function saveUser(name: string, age: number, isAdmin: boolean) {
ย  // ...write to DB
}

You might not think that this code is coupled to much of anything, but it is.ย First, the user of this code needs to know the types of the parameters as well as their order. She also needs to know what isAdmin means.ย These may seem like nitpicks, but imagine a function that had more parameters or more mysterious names.ย These couplings are subtle, but they are there.ย 

Loosening the couples

How about we clean things up a bit?


interface User {
  name: string;
  age: number;
  role: 'admin' | 'user';
}

function saveUser(user: User) {
  // ...write to DB
}

This is slightly better. The caller needs to understand or figure out only one thing, the User interface.ย There is no order of parameters to worry about, and the notion of isAdmin is much clearer (and expandable) as role.ย 

Page-Jones created an entire taxonomy of connascence to describe these kinds of code relationships and help us manage them. Connascence also helps us think about two crucial dimensions: how strong the coupling is, and how far apart the coupled pieces live. Coupling between two routines in the same object is normal. But when coupling spans modules or services, it becomes a serious problem.

Connascence gives you a sharper vocabulary to describe coupling, and a new way to look at your code. Next week, weโ€™ll dive into the taxonomy and start naming the different kinds of coupling youโ€™re already living with.

Nick Hodges

Nick has a BA in classical languages from Carleton College and an MS in information technology management from the Naval Postgraduate School. In his career, he has been a busboy, a cook, a caddie, a telemarketer (for which he apologizes), an office manager, a high school teacher, a naval intelligence officer, a software developer, a product manager, and a software development manager. In addition, he is a former Delphi Product Manager and Delphi R&D Team Manager and the author of Coding in Delphi. He is a passionate Minnesota sports fanโ€”especially the Timberwolvesโ€”as he grew up and went to college in the Land of 10,000 Lakes. He currently lives in West Chester, PA.

More from this author