There are five types of static connascence. Recognizing them and understanding them can help you see more deeply into your code and how it works. Many and perhaps all of them will be familiar to you, though I bet you never really considered them to be “coupling.”
Connascence of name
Connascence of name occurs when two things have to agree about the name of something. This is the weakest form of connascence, or the loosest form of coupling, and the one that we should aspire to limit ourselves to. If you declare a procedure such as
function DoSomething(): void {
// Code that does something
}
Then you have to call it using its name, in this case DoSomething
. Once you choose the name, you are stuck with it. Changing the name of something requires changes elsewhere. If you want to change the name of the procedure, you need to change it everywhere that you’ve called it. That seems obvious, and of course this level of connascence is unavoidable. In fact, it’s desirable. It’s the lowest level of coupling we can have. If we could limit our coupling to connascence of name, we’d be doing extremely well.
Connascence of type
Connascence of type occurs when two entities have to agree on the type of something. The most obvious example is the parameters of a method. If you declare a function as follows:
class SomeClass {
processWidget(aWidget: Widget, aAction: WidgetActionType): boolean {
// Function logic goes here
return false; // Example return value
}
}
Then any calling code must pass a Widget
and a WidgetActionType
as parameters of the processWidget
function and must accept a boolean
as a result type.
Connascence of type isn’t quite as weak as connascence of name, but it is still considered a weak and acceptable level of connascence. Indeed, you couldn’t get along without it, could you? You have to be able to call a method of a class to get anything done, and it’s not very burdensome to ensure that your types match. Most code won’t even work properly if you don’t couple code via connascence of type. The compilers of a typed language won’t even compile code that isn’t coupled with connascence of type.
Connascence of meaning
Connascence of meaning occurs when components must agree on the meaning of particular values. Connascence of meaning most often occurs when we use “magic numbers,” that is, when we use specific values that have meaning in multiple places.