Connascence —Part II: Types of connascence
Tags: Code Quality, Frontend, Backend, Clean Code, Connascence

Connascence of Name

I want to start with the most desirable type of connascence — connascence of Name — and tell you why it is best of all the other types.

Connascence of Name is when two components must agree on the name of something.

In the example below Age is mentioned in the class Customer and is referenced later on line 8. If you’d want to change the property Age in line 3 into something else, you’d have to change the reference on line 8 (and also anywhere else) where it is referenced.

Connascence of Name

It’s easy to see why this type of connascence is the most desirable of all the other types. Modifying connascence of name is literally a no brainer. Modern IDEs can do it automatically for you. That’s why you should always strive for this kind of connascence. Because in this state the code is least coupled and easiest to change.

Connascence of Type

The next connascence is connascence of Type. It means that two components must agree on the type of something.

In the example below the function sayHello takes a parameter and returns a string. But on line 10 you see that you pass in a number to our sayHello function. And usually you don’t want to mix different types with each other.

Connascence of Type with close Locality

In programming languages, e.g. JavaScript, that doesn’t support strong types, it’s easy to do it accidentally. Now you could say “Hey, I’m a Java or C++ developer. These kind of code wouldn’t even compile on my platform. Does it mean that connascence of Type doesn’t apply to people who write in a strong typed language?”

Good question. Let’s see at the next example: Below we have another example for connascence of type. This time it’s a database call creating a Payment-table with a column Amount of type integer. In the code, however, we reference to them as a Payment class with the attribute Amount of type decimal.

Connascence of Type with far way Locality

You have to modify either the database or the part of your code in order for the whole system to be correct again. So, it happens with with strong typed languages as well.

Connascence of Meaning

Next in line is connascence of Meaning. It means that two components must agree on the interpretation of a value.

Connascence of Meaning

What does the value 123.45 actually represent? All you know is that it probably should represent money. But in what currency? Euro? Dollar? Yen? Potatoes? Sheep? Ok, the last two ones was a joke. But the point is: We don’t know.

You probably have encountered that problem already, but called the “magical number” problem. This is also another cool thing about connascence: It gives existing problems a name and puts it into a context.

Connascence of position

The next connascence is connascence of position. This means that two components must be adjacent or occur in a particular order. The following example shows a function that takes 3 parameters. Calling this function means you have to put the arguments in a certain order.

This function is a connascence of position with 3 degrees, because of the 3 parameters that must occur in the same order.

Up till here you may think, connascence is only applicable to programming langues. This example, however, shows it is also applicable to HTML and CSS:

The HTML code on the left shows a nested structure of a section that contains a header and content . The CSS code on the right shows that the text color of the first child of the section should be red. The HTML code is of connascence of position because the correct display of elements is dependent on the position of the HTML elements. Changing the position of the HTML elements, e.g. by switching the position of content and header , would result in a wrong display of elements.

This example shows connascence can be applied not only to strong typed and loosely typed languages but also to mark up languages, like HTML and CSS as well. It basically covers frontend to backend.

Connascence of Algorithm

This connascence means that two components must agree on a particular algorithm in order to work correctly. Examples are encryption, e.g. SHA-2, SHA512, etc. If the sender changes it’s encryption algorithm, the receiver has also to change it’s decryption algorithm.

Connascence of Execution Order

You encounter connascence of Execution Order whenever the execution of two components is important for the code to deliver the correct result. Typical examples are locking and unlocking of resources. Or open and closing of a stream. It’s easy to see why the order of execution is important for the code to work correctly: You can only unlock or close after you have lock respectively open a resource. This kind of connascence is most prevalent in procedural programming languages but far less prevalent in functional languages.

Connascence of Time

Connascence of Timing is when two component must agree on the timing of execution. This usually happens with concurrent elements. The negative example would be race condition.

Connascence of Value

This means that several values have to change together for the code to work correctly. The following function returns the value 50 in line 72 and expects the same value in line 66 . Changing the value of the function also means changing the value in the assertion.

Connascence of Identity

Connascence of Identity means that two components have to reference the same entity. This happens when you obtain an object from a database in different parts of your code and want to update that object. The update must be in consistent once it is put back again in the database.